[已解決]
註解掉就可以儲存在centos7
// if (PHP_SAPI == 'cli')
// die('This example should only be run from a Web Browser');
各位前輩你們好 想請教大家phpexcel問題
問題: PHPExcel無法直接儲存在Centos7底下,下指令ls,沒有任何.xlsx檔案
小弟在公司撰寫自動報表程式。
目前使用PHPExcel + PHPMailer 兩個套件。
PHPMailer有成功發送信件給自己的Gmail信箱
指令 php /var/www/html/xxx.php
然後小弟寫個簡單的phpexcel程式,有成功下載到Windows底下,打開也可以顯示。
問題是我想直接儲存在Centos7底下,可是偏偏只能下載到Windows底下,上網Googole也無法解決,只好來這邊發問題。
指令 php /var/www/html/test_excel.php
這是我的程式碼
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once '/var/www/html/PHPExcel-1.8.0/Classes/PHPExcel.php';
$excelfilename = 'xx工業公司.xlsx';
$excelSheetname = '職員基本資料';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $excelfilename);
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
// Set document properties
$objPHPExcel->getProperties()->setCreator("JinHau")
->setLastModifiedBy("JinHau")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', '姓名')
->setCellValue('B2', '工號')
->setCellValue('C2', '部門')
->setCellValue('D2', '分機');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A3', 'JinHau')
->setCellValue('B3', 'L100803')
->setCellValue('C3', '資訊室')
->setCellValue('D3', '#361');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($excelSheetname);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// 可以正常下載到Windows和顯示
$objWriter->save('php://output');
// 修改路徑
// $objWriter->save('/var/www/html/' . $excelfilename);
exit;
我的環境
OS centos7
Apache 2.4.6
php 7.0.33
PHPExcel-1.8.0
PHPMailer-5.2.26
Office excel 2007