如何使用PHPExcel将HTML表格导出到Excel? [英] How to export html table to excel using PHPExcel?

查看:917
本文介绍了如何使用PHPExcel将HTML表格导出到Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为在不同的浏览器中很难处理不同的标准,所以我放弃了尝试使用js或jQuery导出html表的尝试.我想知道是否可以将html中的表发布回服务器,并在服务器上生成一个.xls文件供用户下载.

Because it is hard to deal with different standards among different browsers, I give up trying to export html table using js or jQuery. I wonder if I can POST the table in html back to server and generate an .xls file on the server for user to download.

现在在使用PHPExcel的服务器端,我的代码如下:

Now on the server side using PHPExcel, my code is like this:

$filename = "DownloadReport";
$table = $_POST['table'];

ini_set('zlib.output_compression','Off'); 
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
//the folowing two lines make sure it is saved as a xls file
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename='.$filename);

$objReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $objReader->load($table);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

exit;

问题是我无法直接加载html表.我该如何处理?

The problem is I cannot load html table directly. How can I deal with that?

还有一个问题是,当我在php中设置标题时,单击该按钮时文件未下载.实际上,我可以查看POST响应的Header的所有属性,以及响应的内容(在FireBug中),这些属性都是正确的.

And one more question is that as I set the headers in php, the file is not downloading when I click the button. Actually I can view all the attributes of Header of the POST response, and also the content of response(in FireBug), which are all correct.

推荐答案

要将内容直接放入$objPHPExcel,您需要创建一个工作表,然后逐个单元格设置值,这不是您想要的.

To put contents directly into $objPHPExcel you would need to create a worksheet and then set values cell by cell, which is not what you want.

要插入整个HTML表,必须从HTMLReader

To insert a whole HTML table, you must read it from an HTMLReader

Writer下面的代码中,将用于将内容输出到文件中

In the code below the Writer will be used to output the content to file

$filename = "DownloadReport";
$table    = $_POST['table'];

// save $table inside temporary file that will be deleted later
$tmpfile = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($tmpfile, $table);

// insert $table into $objPHPExcel's Active Sheet through $excelHTMLReader
$objPHPExcel     = new PHPExcel();
$excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
$excelHTMLReader->loadIntoExisting($tmpfile, $objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle('any name you want'); // Change sheet's title if you want

unlink($tmpfile); // delete temporary file because it isn't needed anymore

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // header for .xlxs file
header('Content-Disposition: attachment;filename='.$filename); // specify the download file name
header('Cache-Control: max-age=0');

// Creates a writer to output the $objPHPExcel's content
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$writer->save('php://output');
exit;

这篇关于如何使用PHPExcel将HTML表格导出到Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆