日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

php 生成excel空白,phpexcel库在localhost上运行良好,但在服务器中生成空白的excel文件...

發(fā)布時(shí)間:2025/3/11 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 生成excel空白,phpexcel库在localhost上运行良好,但在服务器中生成空白的excel文件... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這是我的代碼,在本地主機(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)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。