作为附件发送时 PHPExecel 文件为空

PHPExecel file is empty when send as in attachment

使用 phpexcel 创建了一个 excel 文件,该文件与所有数据一起保存在文件夹中。同一文件不能作为邮件附件发送。我还想上传表格中的 excel 以便进行更改。 请指教。这是代码。

    <?php

     //include PHPExcel library
     require_once "Classes/PHPExcel.php";
     require_once "Classes/PHPExcel/IOFactory.php";

     if (!empty($_POST['submit'])) {
     //give a filename
     $dtime     = date('Y-m-d H-i-s');
     $dtimeFile = date('Y-m-d-H-i-s');
     date_default_timezone_set('Asia/Singapore');
     $filename = 'myexcel'.$dtimeFile.'.xls';
     $path     = __DIR__;

     //set headers to download file
     header('Content-Type: application/vnd.ms-excel');
     header('Content-Disposition: attachment;filename='.$filename);

     // create new PHPExcel object
     $objTpl = new PHPExcel;
     // set default font
     $objTpl->getDefaultStyle()->getFont()->setName('Calibri');
     // set default font size
     $objTpl->getDefaultStyle()->getFont()->setSize(8);
     // create the writer
     $objWriter = PHPExcel_IOFactory::createWriter($objTpl, "Excel5");

     // writer already created the first sheet for us, let's get it
     $objSheet = $objTpl->getActiveSheet();
     // rename the sheet
     $objSheet->setTitle('My Personal Details');

    // let's bold and size the header font and write the header
    // as you can see, we can specify a range of cells, like here: cells from A1 to A4
    $objSheet->getStyle('A1:C1')->getFont()->setBold(true)->setSize(12);
    $objSheet->getStyle('A2:C2')->getFont()->setSize(12);

     // write header
    $objSheet->getCell('A1')->setValue('Name');
$objSheet->getCell('B1')->setValue('Email');
$objSheet->getCell('C1')->setValue('Location');

// we could get this data from database, but for simplicty, let's just write it
$objSheet->getCell('A2')->setValue(stripslashes($_POST['name']));
$objSheet->getCell('B2')->setValue(stripslashes($_POST['email']));
$objSheet->getCell('C2')->setValue(stripslashes($_POST['location']));

// // autosize the columns
// $objSheet->getColumnDimension('A')->setAutoSize(true);
// $objSheet->getColumnDimension('B')->setAutoSize(true);
// $objSheet->getColumnDimension('C')->setAutoSize(true);
// $objSheet->getColumnDimension('D')->setAutoSize(true);

$objWriter->save('php://output');

$to      = "cloudinnovates@hotmail.com";
$subject = $filename;
$from    = "shabs0@hotmail.com";
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"1a2a3a\"";

$message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
."--1a2a3a\r\n";

$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."Attached is the file number \"".$filename."\"\r\n\r\n"
."--1a2a3a\r\n";

$file      = $path.$filename;
$file_size = filesize($file);
$handle    = fopen($file, "r");
$content   = fread($handle, $file_size);
fclose($handle);

$message .= "Content-Type: application/vnd.ms-excel; name=\"".$filename."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
."\r\n"
.chunk_split(base64_encode($content))
."--1a2a3a--";

// Send email
//
$success = mail($to, $subject, $message, $headers);
if (!$success) {
    echo "Mail to ".$to." failed .";
} else {
    echo "Success : Mail was send to ".$to;
}

}
  //*************** upload file ***************//
  if (!empty($_POST['btn-upload'])) {
   $file   = 'file';
   $Reader = PHPExcel_IOFactory::createReaderForFile($file);
   $Reader->setReadDataOnly(true);// set this, to not read all excel properties, just data
   $objXLS = $Reader->load($file);
   $value  = $objXLS->getSheet(0)->getCell('A1')->getValue();
   $objXLS->disconnectWorksheets();
   unset($objXLS);
  }

 ?> 

我不熟悉 PHP Excel 也很长时间没有使用 PHP 了,但是您的代码中有些地方让我印象深刻。

你为文件名初始化了一个变量

$filename = 'myexcel'.$dtimeFile.'.xls';

并尝试像这样读取文件的内容

$file      = $path.$filename;
$file_size = filesize($file);
$handle    = fopen($file, "r");
$content   = fread($handle, $file_size);
fclose($handle);

但您似乎并未真正将 Excel sheet 保存到该文件。相反,您似乎将其写入当前 PHP 页面的输出流。

$objWriter->save('php://output');

设置 content-disposition header

header('Content-Disposition: attachment;filename='.$filename);

告诉浏览器打开此 PHP 页面如何处理 PHP 页面返回的数据,但不将文件保存到您的本地服务器,以便它可以附加到该服务器上的电子邮件。

在附加到电子邮件之前,您需要将 Excel 文件保存到本地服务器上的某个位置。