通过邮件生成并发送excel文件

时间:2012-08-09 05:09:35

标签: php excel

  

可能重复:
  Generate and email excel file as an attachment - Error message: unable to read file

我想生成excel文件并通过邮件发送。

这是我的代码。文件生成成功但电子邮件包含大小为0k的文件。当我打开它时,它将显示无法读取文件的消息

//to generate EXCEL files:

require_once('documentation/Excel_Writer/Writer.php'); 

if($_POST['submit'] == "Email as EXCEL"){

 // get values

  $dated1=$res_hist[0]['dated'];
  $subject1=$res_hist[0]['businesscategory'];
  $hisstartdate=$dateto;
  $hisenddate=$datefrom;

//format of date//
  $date = $hisstartdate;
               $history= date(  "j F Y", strtotime( $date ) );
               $date1 = $hisenddate;
               $history1= date(  "j F Y", strtotime( $date1 ) );
    //print_r($date);die;          

    //$path="businesshistoryxls/";

        $workbook = new Spreadsheet_Excel_Writer();
        //print_r($workbook);die;
        $format_bold =& $workbook->addFormat();
        $format_normal =& $workbook->addFormat();
        $format_bold->setBold(1);
        $format_normal->setBold(0);
        $worksheet =& $workbook->addWorksheet();

        $worksheet->write(0, 0, "TransactionID", $format_bold);
        $worksheet->write(0, 1, "Date", $format_bold);
        $worksheet->write(0, 2, "Type", $format_bold);
        $worksheet->write(0, 3, "Transaction Details", $format_bold);
        $worksheet->write(0, 4, "Currency", $format_bold);
        $worksheet->write(0, 5, "Amount", $format_bold);
        $worksheet->write(0, 6, "Balance", $format_bold);

        $sqlza = "Select * from BUSINESSTRANSACTION where isACTIVE=1 and BusinessID=".$_SESSION['businessID']." order by dated DESC";       
       $resza = getXbyY($sqlza, "array");
       $rowsza = count($resza);
      // print_r($resza);die;
        if($rowsza>0)
        {
        for($i=0;$i<$rowsza;$i++)
            {
                $worksheet->write($i+1, 0, $resza[$i]['transactionID'], $format_bold);
                $worksheet->write($i+1, 1, $resza[$i]['dated'], $format_bold);
                $worksheet->write($i+1, 2, $resza[$i]['transactionTYPE'], $format_bold);
                $worksheet->write($i+1, 3, $resza[$i]['transactionDETAILS'], $format_bold);
                $worksheet->write($i+1, 4, $resza[$i]['currency'], $format_bold);
                $worksheet->write($i+1, 5, $resza[$i]['amount'], $format_bold);
                $worksheet->write($i+1, 6, $resza[$i]['balance'], $format_bold);

            }
        }    
     $type=".xls";
    $xlsname=$path.''.$hisstartdate.''.to.''.$hisenddate.''.$type;
 $workbook->send($xlsname);
    //$worksheet->Output($xlsname, 'F');
$email_to = $res_pers[0]['email'];
//print_r($xlsname);die;
$email_from="admin@fastcashier.com";
$email_subject = "Transactions's History";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers.= 'From:'.$email_from."\r\n";
$email_message="Dear ".$username."<br><br>";
$email_message.="Please check the attachment.<br>";
$email_message .="<br/>";
$email_message .="";
$my_path =$_SERVER['DOCUMENT_ROOT']."/".$xlsname;
//print_r($my_path);die;
mail_attachment($email_from, $email_to, $email_subject, $email_message ,$my_path);
//print_r($mail_attachment);die;
unlink($xlsname);
$msgID=67;
        $workbook->close();
unset($_SESSION['Historyb']);
}

mail_attachment功能:

function mail_attachment($from , $to, $subject, $message, $attachment){

$fileatt = $attachment; // Path to the file                  
$fileatt_type = "application"; // File Type 
$start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
$fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the     attachment 

$email_from = $from; // Who the email is from 
$email_subject =  $subject; // The Subject of the email 
$email_txt = $message; // Message that the email has in it 

$email_to = $to; // Who the email is to

$headers = "From: ".$email_from;
//$headers .= "\nCc: payment@comtranslations.com";
//$headers .= "\nCc: ".$o3->email;
$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file); 
$msg_txt="";

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

$headers .= "\nMIME-Version: 1.0\n" . 
        "Content-Type: multipart/mixed;\n" . 
        " boundary=\"{$mime_boundary}\""; 

$email_txt .= $msg_txt;

$email_message .= "This is a multi-part message in MIME format.\n\n" . 
            "--{$mime_boundary}\n" . 
            "Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
           "Content-Transfer-Encoding: 7bit\n\n" . 
$email_txt . "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$email_message .= "--{$mime_boundary}\n" . 
              "Content-Type: {$fileatt_type};\n" . 
              " name=\"{$fileatt_name}\"\n" . 
              //"Content-Disposition: attachment;\n" . 
              //" filename=\"{$fileatt_name}\"\n" . 
              "Content-Transfer-Encoding: base64\n\n" . 
             $data . "\n\n" . 
              "--{$mime_boundary}--\n"; 


$ok = @mail($email_to, $email_subject, $email_message, $headers); 

if($ok) { 

} else { 
    die("Sorry but the email could not be sent. Please go back and try again!"); 
} 
}

1 个答案:

答案 0 :(得分:0)

查看Spreadsheet_Excel_Writer::close()的代码,看起来它会做一些最终的写入。请尝试在$workbook->close()之前致电mail_attachment