我正在使用 PHPExcel 库here。但是,没有按预期工作。当我搜索时,有人建议将PHPEXcel_IOFactory
替换为IOFactory
。它下载文件,但无法打开。有什么建议吗?
这是我的图书馆代码:
Excel.php
require_once APPPATH.'/third_party/PHPExcel.php';
class Excel extends PHPExcel
{
public function __construct()
{
parent::__construct();
}
}
这是控制器代码:
#load our new PHPExcel library
$this->load->library('excel');
$data = $this->report_model->get_all_report_product_info();
$filename = "Output";
# Set the active Excel worksheet to sheet 0
$this->excel->setActiveSheetIndex(0);
$row_count = 2;
foreach ($data as $row) {
$this->excel->getActiveSheet()->setCellValue('A' . $row_count, $row->product_name);
$this->excel->getActiveSheet()->setCellValue('B' . $row_count, $row->product_sku);
$this->excel->getActiveSheet()->setCellValue('C' . $row_count, $row->customer_name);
$this->excel->getActiveSheet()->setCellValue('D' . $row_count, $row->site_url);
$row_count++;
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
header('Cache-Control: max-age=0');
// Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
$objWriter = IOFactory::createWriter($this->excel, 'Excel5');
// Write the Excel file to filename
$objWriter->save('php://output');
答案 0 :(得分:0)
$objWriter = IOFactory::createWriter($this->excel, 'Excel5');
“Excel5”是旧的电子表格格式(用于.XLS文件)。如果要创建XLSX文件,则需要使用“Excel2007”。