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