php 生成excel空白,phpexcel库在localhost上运行良好,但在服务器中生成空白的excel文件...
這是我的代碼,在本地主機(jī)上可以很好地使用數(shù)據(jù)庫(kù)中的數(shù)據(jù)生成一個(gè)excel文件,但在托管服務(wù)器中它會(huì)生成一個(gè)空白的excel文件:
// Starting the PHPExcel library
$this->load->library('PHPExcel');
//$this->load->library('PHPExcel/IOFactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
$objPHPExcel->setActiveSheetIndex(0);
// Field names in the first row
$fields = $query->list_fields();
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$col++;
}
//format the column sizes
$sheet = $objPHPExcel->getActiveSheet();
$cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
$cellIterator->setIterateOnlyExistingCells( true );
/** @var PHPExcel_Cell $cell */
foreach( $cellIterator as $cell ) {
$sheet->getColumnDimension( $cell->getColumn() )->setAutoSize( true );
}
//var_dump($query->result());
//die;
// Fetching the table data
$row = 2;
foreach($query->result() as $data)
{
$col = 0;
foreach($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
$col++;
}
$row++;
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
ob_clean();
$objWriter->save('php://output');
解決方法:
我認(rèn)為問(wèn)題與phpexcel無(wú)關(guān).我早些時(shí)候遇到過(guò)類似的問(wèn)題,后來(lái)發(fā)現(xiàn)CI的list_fields()函數(shù)在某些linux服務(wù)器中不起作用.您可以通過(guò)靜態(tài)放置字段名稱而不是使用此功能來(lái)檢查此方面.
標(biāo)簽:codeigniter,phpexcel,php
來(lái)源: https://codeday.me/bug/20191026/1937839.html
總結(jié)
以上是生活随笔為你收集整理的php 生成excel空白,phpexcel库在localhost上运行良好,但在服务器中生成空白的excel文件...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 哈希表查找失败的平均查找长度_你还应该知
- 下一篇: php 读文件返回字符串,PHP:fil