![java string[]转string_php 科学计数法 转 string_php string 转 int](http://www.javaidea.cn/d/file/p/2020/03-31/3f8f07e7b1391f5bb2849c5320973849.png)
PhpSpreadsheet导出Excel表,将长数字自动转换为科学计数法
public function down($data)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$lieCount = count($data['data'][0]);
# 全部设为自动列宽
for($i=65;$i< (65 +$lieCount);$i++){
$sheet->getColumnDimension(strtoupper(chr($i)))->setAutoSize(true);
}
# 最快捷设置数据
$sheet->fromArray($data['data']);
# 导出
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
}
通过这种方式,可以传递数据数组,然后快速将其导出到Excel表中.
![php 科学计数法 转 string_php string 转 int_java string[]转string](http://bloger.wang/uploads/2018/03/BD253F13-A4C9-947F-8FCB-15E057F5D4AD.jpg)
但是当遇到长整数时,它将转换为科学计数法,最终精度将丢失,所有精度都将转换为0
原因:
如果数字超过11位,Excel表格将以科学计数法显示. 如果要输入11位以上的数字,则必须在输入数字之前将单元格设置为文本或输入英文单引号(’). (使用英语输入法输入单引号)
![java string[]转string_php 科学计数法 转 string_php string 转 int](http://www.qingpingshan.com/uploads/allimg/171015/12420S262-10.jpg)
也就是说,在添加数据之前先遍历以添加符号
但是php 科学计数法 转 string,该方法不会在我们的程序中自动生效. 我们需要双击单元格以将其转换为文本.
导出后的效果是'11111111111111
![php 科学计数法 转 string_php string 转 int_java string[]转string](http://img.mp.itc.cn/upload/20161121/ae9fcc06d3c047f0bab50bdb7784d3f1_th.jpeg)
当然php 科学计数法 转 string,除了拼接字符串!以下几点对于测试很重要!
PhpSpreadsheet具有相同的前身PHPExcel,您可以通过setCellValueExplicit指定程序.
因此将原始程序转换为以下内容
![php 科学计数法 转 string_java string[]转string_php string 转 int](http://img1.mukewang.com/5afc1dd10001ac3603780767.jpg)
private $mustStringArray = [];
/**
* 将列强制设置成文本,避免长文本出现转科学计数法
* @param array $array
*/
function setMustString(Array $array)
{
$this->mustStringArray = $array;
}
/**
* 导出表格
* @todo 弹窗导出表格
* @param $data array 数组 可选:filename文件名,data数据(二维数组),
* @return bool
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function down($data)
{
if (empty($data)) return false;
$filename = !empty($data['filename']) ? $data['filename'] . ".xlsx" : date('Y-m-d H:i:s') . ".xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$lieCount = count($data['data'][0]);
for($i=65;$i< (65 +$lieCount);$i++){
$sheet->getColumnDimension(strtoupper(chr($i)))->setAutoSize(true);
}
// 以下代码基于fromArray改造
// 开始列和开始行数,默认全部
$startColumn = 'A';
$startRow = '1';
foreach ($data['data'] as $rowData) {
$currentColumn = $startColumn;
foreach ($rowData as $cellValue) {
if ($cellValue !== null) {
if (in_array($currentColumn, $this->mustStringArray)){
$sheet->getCell($currentColumn . $startRow)->setValueExplicit($cellValue,'s');
}else{
$sheet->getCell($currentColumn . $startRow)->setValue($cellValue);
}
}
++$currentColumn;
}
++$startRow;
}
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
}
$sheet->getCell($currentColumn . $startRow)->setValueExplicit($cellValue,'s');
第二个参数实际上是一个类的静态变量,然后我跟踪了它的代码并直接写了值〜
感兴趣的学生可以查看此类文件PhpOffice \ PhpSpreadsheet \ Cell \ DataType,还有其他几种类型的常量列表
// Data types
const TYPE_STRING2 = 'str';
const TYPE_STRING = 's';
const TYPE_FORMULA = 'f';
const TYPE_NUMERIC = 'n';
const TYPE_BOOL = 'b';
const TYPE_NULL = 'null';
const TYPE_INLINE = 'inlineStr';
const TYPE_ERROR = 'e';
请注明转载的原始文章,转载自: 暹罗博客-PhpSpreadsheet导出Excel表,将长数字自动转换为科学计数法()
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-210268-1.html
中国应该马上派战机和导弹严陈以待