Excel使用Codeigniter中的phpexcel库生成和发送电子邮件

时间:2018-05-30 12:05:58

标签: php codeigniter-3 phpexcel

我尝试报告以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);

1 个答案:

答案 0 :(得分:0)

答案:

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