【问题标题】:Excel generate and send email using phpexcel library in CodeigniterExcel 使用 Codeigniter 中的 phpexcel 库生成和发送电子邮件
【发布时间】:2018-05-30 12:05:57
【问题描述】:

我曾尝试以 excel 格式生成报告并使用 PHPExcel 库发送电子邮件。我需要在 excel 文件中进行一些 html 对齐,所以我已经将我的输出作为 html 下载到我的本地文件夹中。但是我无法将 html 文件转换为 excel 文件,当我们尝试下面的代码时,生成的 excel 文件只显示 html 文件名而不是 html 表格内容。

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

【问题讨论】:

  • 不确定我的问题是否正确。我猜你的 mysql 表中有数据,你想生成一个 excel 报告并通过电子邮件发送?

标签: php codeigniter-3 phpexcel


【解决方案1】:

答案:

$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";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-06
    • 2017-12-07
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多