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 解決問題

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM