在 PHP 中创建 XLS 文件的最佳方法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3133357/
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-25 08:48:18  来源:igfitidea点击:

What is the best way to create XLS file in PHP

phpcsvxls

提问by Metropolis

I am currently trying to figure out the best way to create a .xls file from PHP. It seems like to me that all I need to do is change the header content type to "application/vnd.ms-excel", and then output data in a comma separated format. Is this the best way to do this?

我目前正在尝试找出从 PHP 创建 .xls 文件的最佳方法。在我看来,我需要做的就是将标题内容类型更改为“application/vnd.ms-excel”,然后以逗号分隔的格式输出数据。这是最好的方法吗?

Thanks for any help!
Metropolis

谢谢你的帮助!
都会

EDIT

编辑

Would it be bad to just output the data as HTML? I already have a class that I would like to extend for this problem, so I would prefer to stay away from a third party solution. I just need to know what the best way to output the data would be in order to get the most out of that file format.

仅将数据输出为 HTML 会不会很糟糕?我已经有一个我想为这个问题扩展的类,所以我宁愿远离第三方解决方案。我只需要知道输出数据的最佳方式是什么,以便充分利用该文件格式。

After looking at PHPExcel it seems to be the best solution for this issue. I appreciate all of your answers and I would have given three correct answers if I could lol. But up votes will have to do :)

在查看 PHPExcel 之后,它似乎是这个问题的最佳解决方案。我感谢您的所有回答,如果我可以大声笑,我会给出三个正确的答案。但是投票必须要做:)

采纳答案by Mark Baker

Depends if you want a CSV file or an XLS file. An XLS file can include formatting information for the cells, as well as row/column locking, protections and other features that are impossible in a CSV file. Also, keep in mind that Excel does not correctly support UTF-8 encoded content when opening CSV files.

取决于您想要 CSV 文件还是 XLS 文件。XLS 文件可以包含单元格的格式信息,以及行/列锁定、保护和 CSV 文件中无法实现的其他功能。另外,请记住,Excel 在打开 CSV 文件时无法正确支持 UTF-8 编码的内容。

If you want a formatted XLS file, then you need a library such as PhpSpreadsheetthat can write a file with that formatting, or COM if you're server is running on Windows with Excel installed

如果你想要一个格式化的 XLS 文件,那么你需要一个库,比如PhpSpreadsheet,它可以用这种格式编写文件,或者 COM,如果你的服务器在安装了 Excel 的 Windows 上运行

回答by Pedro Gouveia

its pretty easy, because excel supports html code. So you just need to add a header in the top of the file.. and then echo your html code!

很简单,因为excel支持html代码。所以你只需要在文件顶部添加一个标题..然后回显你的html代码!

header ( "Content-type: application/vnd.ms-excel" );
header ( "Content-Disposition: attachment; filename=foo_bar.xls" );
echo "<table><tr><th>Header1</th></tr><tr><td>value1</td></tr></table>";

this will generate an excel file!

这将生成一个excel文件!

回答by Robert Groves

You could use one of the XML formatsthat Microsoft Office understands.

您可以使用Microsoft Office 理解的一种XML 格式

See Microsoft's OpenXML Developersite for details/specs.

有关详细信息/规范,请参阅 Microsoft 的OpenXML 开发人员站点。

There is a library on called PHPExcelthat you can look at to help you with this.

有一个名为PHPExcel的库,您可以查看它来帮助您解决这个问题。

Of course, this all depends on what you mean by:

当然,这一切都取决于您的意思:

"get the most out of that file format"

“充分利用该文件格式”

If you just have simple tables with no formatting, CSV files may be all you need; however, if you want to use more features of a spreadsheet, I would recommend taking a look at OpenXML.

如果您只有没有格式的简单表格,则可能只需要 CSV 文件;但是,如果您想使用电子表格的更多功能,我建议您查看 OpenXML。

回答by Scott Saunders

You can just output html and newer versions of Excel will read it. I don't know how much formatting you can do with it though.

您可以只输出 html 并且较新版本的 Excel 将读取它。我不知道你可以用它做多少格式化。

If you need data typing, rich formatting or formulas, I have had success with PHPExcel.

如果您需要数据输入、丰富的格式或公式,我在PHPExcel 上取得了成功。

My preference is to write out CSV files. CSV is a very easy format to write and an easy conversion from existing html table scripts. It also has the advantage of being readable by a wide variety of non-microsoft spreadsheet programs. If you can make CSV the file format of choice for your web application, you will reap rewards when you have to accept a spreadsheet as input. It is much, much easier to read and parse a CSV file than an Excel file.

我的偏好是写出 CSV 文件。CSV 是一种非常容易编写的格式,并且可以从现有的 html 表格脚本轻松转换。它还具有可被各种非微软电子表格程序读取的优点。如果您可以使 CSV 成为您的 Web 应用程序的首选文件格式,那么当您必须接受电子表格作为输入时,您将获得回报。读取和解析 CSV 文件比 Excel 文件要容易得多。

回答by illbzo1

Disclaimer: I work at Expected Behavior, the company that developed DocRaptor.

免责声明:我在开发 DocRaptor 的公司 Expected Behavior 工作。

That said, you can use DocRaptor to generate XLS files from HTML. DocRaptor is a Ruby on Rails project, but you can use it with PHP. Here's our PHP example:

也就是说,您可以使用 DocRaptor 从 HTML 生成 XLS 文件。DocRaptor 是一个 Ruby on Rails 项目,但您可以将它与 PHP 一起使用。这是我们的 PHP 示例:

DocRaptor PHP example

DocRaptor PHP 示例

Here's another link to DocRaptor's home page:

这是 DocRaptor 主页的另一个链接:

HTML to Excel with DocRaptor

使用 DocRaptor 将 HTML 转换为 Excel

回答by cam8001

Try the XLS file wrapper, ala this poston SO.

尝试使用XLS 文件包装器,就像这篇关于 SO 的帖子

回答by Davide Gualano

Take a look at this PEAR package: Spreadsheet Excel Writer

看看这个 PEAR 包:Spreadsheet Excel Writer

回答by Julius A

Yes, that is one possible way - if you need a plain CSV file that will open in Ms Excel.

是的,这是一种可能的方法 - 如果您需要一个可以在 Excel 中打开的普通 CSV 文件。

If you need a rich XLS file, there are many options including using third party DLLs such as 'CarlosAg.ExcelXmlWriter.dll' to create the XLS file. You can search for samples of using this dll on the net or Look here

如果您需要丰富的 XL​​S 文件,有很多选择,包括使用第三方 DLL(例如“CarlosAg.ExcelXmlWriter.dll”)来创建 XLS 文件。您可以在网上搜索使用此 dll 的示例或查看此处

回答by Raj Sharma

You can start using this files. It will create a excel file and help you download it.

您可以开始使用这些文件。它将创建一个 excel 文件并帮助您下载它。

You can add rows like this in the

您可以在

01-simple-download-xlsx.php

01-simple-download-xlsx.php

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Roll')
            ->setCellValue('B1', 'Name')
            ->setCellValue('C1', 'Total Classes')
            ->setCellValue('D1', 'Class Attended')
        ->setCellValue('E1', 'Percentage');

You can print data from a database like this using a while loop.

您可以使用 while 循环从这样的数据库中打印数据。

$i=2;   
while ($row = $database->fetch_array($result)) 
 { 

       $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i, $row[0])
            ->setCellValue('B'.$i, $row[1])
            ->setCellValue('C'.$i, $row[2])
            ->setCellValue('D'.$i, $total)
        ->setCellValue('E'.$i, round(($row[2]/$total)*100));
          $i++;
 }