我已尝试过像
这样的代码require_once dirname(__FILE__) . '/../../vendor/PHPExcel/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex()->setCellValue('A1', 'Hello world!');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$this->response->body( $objWriter->save('php://output'));
$this->response->send_file(TRUE, '01simple.xls');
exit;
答案 0 :(得分:0)
请你试试这个......
require_once dirname(__FILE__) . '/../../vendor/PHPExcel/Classes/PHPExcel.php';
$phpExcel = new PHPExcel();
$phpExcel->getActiveSheet()->setTitle("Title of my excel");
$phpExcel->setActiveSheetIndex(0);
$phpExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$filename = "file_name";
ob_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2007');
ob_end_clean();
$objWriter->save('php://output');
答案 1 :(得分:0)
使用此方法为我工作。将您的内容设置在一个变量中并分配。这里我提到了例子
require_once APPPATH.'libraries/reader/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$reportHtml = '<table cellpadding="0" cellspacing="0" class="table table-bordered" style="border:1px solid #f0f0f0;">
<thead>
<tr>
<th>Customer Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Invoice Id</th>
<th>Invoice Date</th>
<th>Due Date</th>
<th>Balance Amount</th>
<th>Total Amount</th>
</tr>
</thead>';
// Put the html into a temporary file
$tmpfile = time().'.html';
file_put_contents($tmpfile, $reportHtml);
// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($tmpfile);
// Pass to writer and output as needed
header("Cache-Control: private", false);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=test_1'.time().'.xlsx');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
$objWriter->save('php://output');
unlink($tmpfile);