简体   繁体   English

从Web服务器文件夹下载Excel文件

[英]Download an Excel file from web server folder

I am using PHPExcel to get Oracle SQL query results to an .xlsx file. 我正在使用PHPExcel将Oracle SQL查询结果获取到.xlsx文件。 I wrote my PHP code in node--18769.tpl.php and node--18769.tpl.xlsx file is downloading to webroot (/root/themes/bartik/templates) folder with result. 我在node--18769.tpl.php编写了我的PHP代码,并且node--18769.tpl.xlsx文件正在下载到具有结果的webroot(/ root / themes / bartik / templates)文件夹中。

My requirement: 我的要求:

  1. Can I rename node--18769.tpl.xlsx to report.xlsx ? 我可以将node--18769.tpl.xlsx重命名为report.xlsx吗?

  2. Is it possible to prepend UNIX TIMESTAMP to file name? 可以在UNIX TIMESTAMP前面加上文件名吗? (like 1442223364_report.xlsx) (例如1442223364_report.xlsx)

  3. How can I download report.xlsx to my local system once after the file is generated? 生成文件后,如何将report.xlsx下载到本地系统?

This is my code: 这是我的代码:

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__));

Code updated: 代码已更新:

$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 MYH: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;

在此处输入图片说明

This line 这条线

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

tells PHPExcel what filename to use when you save the file.... simply change it to 告诉PHPExcel保存文件时使用什么文件名...。只需将其更改为

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

If you want to download the file to local, then save to php://output and send the appropriate headers to the browser, exactly as described in the documentation and shown in the examples provided with PHPExcel 如果要将文件下载到本地,则保存到php://output并将适当的标头发送到浏览器,完全按照文档所述以及PHPExcel附带的示例所示

// 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM