Excel 在 Codeigniter 中使用 phpexcel 库生成和发送电子邮件
Excel generate and send email using phpexcel library in Codeigniter
我曾尝试以 excel 格式生成报告并使用 PHPExcel 库发送电子邮件。我需要在 excel 文件中进行一些 html 对齐,因此我将输出下载为 html 到我的本地文件夹中。但是我无法将 html 文件转换为 excel 文件,当我们尝试下面的代码时,生成的 excel 文件仅显示 html 文件名而不是 html table内容
$this->data['report1']=$this->report_m->get_report();
$this->data['report_details']=$this->report_m->get_report_details();
$this->load->library('PHPExcel/iofactory');
$html=$this->load->view('viewfilename',$data,TRUE);
$tmpfile = time().'.html';
file_put_contents($tmpfile, $html);
//by using above code html file created successfully
//method 1 for reading html
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($tmpfile);
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
$objWriter->save('excelfile.xlsx');
//method 2 for reading html
$objPHPExcel = new PHPExcel();
$excelHTMLReader = IOFactory::createReader('HTML');
$excelHTMLReader->loadIntoExisting($tmpfile, $objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle('report');
$writer = IOFactory::createWriter($objPHPExcel, 'Excel2007');
$writer->save("nameoffiledrrr.xls");
unlink($tmpfile);
答案:
$html=$this->load->view('sendmail_template',$data,TRUE);
include("simple_html_dom.php");
$rowRecords = str_get_html($html);
//echo $html;
$filename="Report-".$dtimeFile.".xls";
$path='./reports/';
$csv_handler = fopen ($path.$filename,'w');
fwrite ($csv_handler,$rowRecords);
fclose ($csv_handler);
$msg='Report';
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$file_path=base_url('reports/'.$filename);
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxxxxxxxx@gmail.com',
'smtp_pass' => 'xxxxxxxxxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$email = $result->to_addr;
$info = "info@xxxx.xx";
$infoname = "info";
$message = "PFA Report";
$this->email->set_mailtype("html");
$this->email->from($info, $infoname);
$this->email->to($email);
$subject = 'Report';
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach($file_path);
$r = $this->email->send();
if (!$r) {
echo "Failed to send email:" . $this->email->print_debugger(array("header"));
} else {
echo "Mail Sent";
}
我曾尝试以 excel 格式生成报告并使用 PHPExcel 库发送电子邮件。我需要在 excel 文件中进行一些 html 对齐,因此我将输出下载为 html 到我的本地文件夹中。但是我无法将 html 文件转换为 excel 文件,当我们尝试下面的代码时,生成的 excel 文件仅显示 html 文件名而不是 html table内容
$this->data['report1']=$this->report_m->get_report();
$this->data['report_details']=$this->report_m->get_report_details();
$this->load->library('PHPExcel/iofactory');
$html=$this->load->view('viewfilename',$data,TRUE);
$tmpfile = time().'.html';
file_put_contents($tmpfile, $html);
//by using above code html file created successfully
//method 1 for reading html
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($tmpfile);
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
$objWriter->save('excelfile.xlsx');
//method 2 for reading html
$objPHPExcel = new PHPExcel();
$excelHTMLReader = IOFactory::createReader('HTML');
$excelHTMLReader->loadIntoExisting($tmpfile, $objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle('report');
$writer = IOFactory::createWriter($objPHPExcel, 'Excel2007');
$writer->save("nameoffiledrrr.xls");
unlink($tmpfile);
答案:
$html=$this->load->view('sendmail_template',$data,TRUE);
include("simple_html_dom.php");
$rowRecords = str_get_html($html);
//echo $html;
$filename="Report-".$dtimeFile.".xls";
$path='./reports/';
$csv_handler = fopen ($path.$filename,'w');
fwrite ($csv_handler,$rowRecords);
fclose ($csv_handler);
$msg='Report';
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$file_path=base_url('reports/'.$filename);
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxxxxxxxx@gmail.com',
'smtp_pass' => 'xxxxxxxxxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$email = $result->to_addr;
$info = "info@xxxx.xx";
$infoname = "info";
$message = "PFA Report";
$this->email->set_mailtype("html");
$this->email->from($info, $infoname);
$this->email->to($email);
$subject = 'Report';
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach($file_path);
$r = $this->email->send();
if (!$r) {
echo "Failed to send email:" . $this->email->print_debugger(array("header"));
} else {
echo "Mail Sent";
}