PhpOffice\PhpSpreadsheet导出excel长数字科学计算问题解决

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

static function export($filename,$title,$data){
        $spreadsheet = new Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();
        //设置工作表标题名称
        $worksheet->setTitle('Sheet1');
        foreach ($title as $tk=> $tv){
            $worksheet->setCellValueByColumnAndRow($tk+1, 1, $tv);
        }
        if($data){
            foreach ($data as $k=>$v){
                foreach ($v as $kk=>$vv){
                    //$worksheet->setCellValueByColumnAndRow($kk+1, $k+2, $vv);
                    $worksheet->setCellValueExplicitByColumnAndRow($kk+1,$k+2,$vv,DataType::TYPE_STRING);
                }
            }
        }
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
        header('Cache-Control: max-age=0');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }

主要是

$worksheet->setCellValueExplicitByColumnAndRow($kk+1,$k+2,$vv,DataType::TYPE_STRING); 起到作用
用户编辑点击时变成科学计算 设置单元格格式 选择自定义 设置0 解决问题

 

 

 
posted @ 2020-05-13 13:02  大智如蠢  阅读(640)  评论(1编辑  收藏  举报