我正在使用PHPExcel將Oracle SQL查詢結果獲取到.xlsx文件。我寫了我的PHP代碼node--18769.tpl.php
和node--18769.tpl.xlsx
文件正在下載到webroot(/ root/themes/bartik/templates)文件夾中。從Web服務器文件夾下載Excel文件
我的要求:
我可以重命名
node--18769.tpl.xlsx
到report.xlsx
?是否可以在文件名前加上
UNIX TIMESTAMP
? (如1442223364_report.xlsx)如何在生成文件後將
report.xlsx
下載到本地系統?
這是我的代碼:
require_once dirname(__FILE__) . '/Classes/PHPExcel.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set document properties // Add some data $query = "SELECT DISTINCT TITLE, PID, TYPE, SUM(DAYCOUNT) AS tot, ROUND(SUM(DAYCOUNT)/(SELECT SUM(DAYCOUNT) FROM REPORT_LIST_VIEW), 4) AS per FROM REPORT_LIST_VIEW WHERE DAYCOUNT > '0' GROUP BY TITLE, PID, TYPE ORDER BY tot DESC"; //print $query; exit; $res = db_query($query); $rowNumber = 1; while ($dataFetched = $res->fetchAssoc()) { $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$rowNumber, $dataFetched['title']) ->setCellValue('B'.$rowNumber, $dataFetched['tot']) ->setCellValue('C'.$rowNumber, $dataFetched['per']); $rowNumber++; } // Miscellaneous glyphs, UTF-8 $objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1); $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true); // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Page & Files Reports '); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setCellValue('A8',"Hello\nWorld"); $objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1); $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true); // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Save Excel 2007 file $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
代碼更新:
$objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Excel2007) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="01simple.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit;
感謝馬克。我更新了我的代碼。但是下載的'xlsx'文件已損壞。 – drup
在文本編輯器中打開文件。查找文件頂部的前導/尾部空白字符,BOM標頭或文件二進制流中的任何明文PHP消息 –
它包含網頁的html源代碼。 – drup