2015-09-14 127 views
0

我正在使用PHPExcel將Oracle SQL查詢結果獲取到.xlsx文件。我寫了我的PHP代碼node--18769.tpl.phpnode--18769.tpl.xlsx文件正在下載到webroot(/ root/themes/bartik/templates)文件夾中。從Web服務器文件夾下載Excel文件

我的要求:

  1. 我可以重命名node--18769.tpl.xlsxreport.xlsx

  2. 是否可以在文件名前加上UNIX TIMESTAMP? (如1442223364_report.xlsx)

  3. 如何在生成文件後將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;

enter image description here

回答

0

此行

$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); 

告訴PHPExcel當您將文件保存到使用什麼文件名....如果你要下載的文件下載到本地,然後保存到php://output併發送相應的它只是改變

$objWriter->save("WHATEVER_YOU_WANT_TO_CALL_THE_FILE.xlsx); 

the documentation,並在the examples中顯示,並提供了PHPExcel

+0

感謝馬克。我更新了我的代碼。但是下載的'xlsx'文件已損壞。 – drup

+0

在文本編輯器中打開文件。查找文件頂部的前導/尾部空白字符,BOM標頭或文件二進制流中的任何明文PHP消息 –

+0

它包含網頁的html源代碼。 – drup

0
// write the file 
$objWriter->save('Excel_report/'filename'); 

//Redirect output to a client’s web browser (Excel2007) 
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
header('Content-Disposition: attachment;filename="Contact.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'); 

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; 
+0

請添加說明 – zohar