b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

PhpSpreadsheet导出Excel表,将长数字自动转换为科学计数法

电脑杂谈  发布时间:2020-05-14 21:15:53  来源:网络整理

java string[]转string_php 科学计数法 转 string_php string 转 int

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

但是当遇到长整数时,它将转换为科学计数法,最终精度将丢失,所有精度都将转换为0

原因:

如果数字超过11位,Excel表格将以科学计数法显示. 如果要输入11位以上的数字,则必须在输入数字之前将单元格设置为文本或输入英文单引号(’). (使用英语输入法输入单引号)

java string[]转string_php 科学计数法 转 string_php string 转 int

也就是说,在添加数据之前先遍历以添加符号

但是php 科学计数法 转 string,该方法不会在我们的程序中自动生效. 我们需要双击单元格以将其转换为文本.

导出后的效果是'11111111111111

php 科学计数法 转 string_php string 转 int_java string[]转string

当然php 科学计数法 转 string,除了拼接字符串!以下几点对于测试很重要!

PhpSpreadsheet具有相同的前身PHPExcel,您可以通过setCellValueExplicit指定程序.

因此将原始程序转换为以下内容

php 科学计数法 转 string_java string[]转string_php string 转 int

    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

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 朱允炆
        朱允炆

        中国应该马上派战机和导弹严陈以待

      热点图片
      拼命载入中...