Html 如何使用PHPExcel将html表导出到excel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24048291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:57:53  来源:igfitidea点击:

How to export html table to excel using PHPExcel?

htmlexport-to-excelphpexcel

提问by Yongyiw

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.

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

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

现在在服务器端使用PHPExcel,我的代码是这样的:

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

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

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

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.

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

回答by Edson Horacio Junior

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

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

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

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

In the code below the Writerwill be used to output the content to file

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

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

回答by JJaki

html table in 2 sheets or more:

2 张或更多张的 html 表格:

    require_once "/PHPExcel.php";
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);
    $asheet = $objPHPExcel->getActiveSheet();

    $table = '<table></table>'; //put your table

    $tmpfile = tempnam(sys_get_temp_dir(), 'html');
    file_put_contents($tmpfile, $table);
    $excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
    $excelHTMLReader->loadIntoExisting($tmpfile, $objPHPExcel);
    unlink($tmpfile);

    // sheet 2
    $objPHPExcel2 = new PHPExcel();
    $objPHPExcel2->setActiveSheetIndex(0);
    $asheet2 = $objPHPExcel2->getActiveSheet();

    $table2 = '<table></table>'; //put your another table

    $tmpfile2 = tempnam(sys_get_temp_dir(), 'html');
    file_put_contents($tmpfile2, $table2);
    $excelHTMLReader2 = PHPExcel_IOFactory::createReader('HTML');
    $excelHTMLReader2->loadIntoExisting($tmpfile2, $objPHPExcel2);
    unlink($tmpfile2);

    //$objPHPExcel->addSheet($asheet2, 0);
    $objPHPExcel->addExternalSheet($asheet2, 0); //copy sheet2 in first objPHPExcel

    header("Content-Type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename=simple.xls");

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