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之后,它似乎是解决此问题的最佳解决方案。我很感谢你的所有答案,如果我能解决的话,我会给出三个正确的答案。但是投票必须要做:)
9 个解决方案
#1
14
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文件中不可能的其他功能。此外,请记住,打开CSV文件时Excel不能正确支持UTF-8编码的内容。
If you want a formatted XLS file, then you need a library such as PhpSpreadsheet that can write a file with that formatting, or COM if you're server is running on Windows with Excel installed
如果你想要一个格式化的XLS文件,那么你需要一个像PhpSpreadsheet这样可以用这种格式编写文件的库,或者如果服务器在安装了Excel的Windows上运行的话需要COM
#2
12
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文件!
#3
3
You could use one of the XML formats that Microsoft Office understands.
您可以使用Microsoft Office理解的XML格式之一。
See Microsoft's OpenXML Developer site for details/specs.
有关详细信息/规格,请参阅Microsoft的OpenXML Developer站点。
There is a library on called PHPExcel that 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。
#4
2
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文件容易得多。
#7
1
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示例
Here's another link to DocRaptor's home page:
这是DocRaptor主页的另一个链接:
使用DocRaptor将HTML转换为Excel
#8
0
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
如果您需要丰富的XLS文件,有许多选项,包括使用第三方DLL(如“CarlosAg.ExcelXmlWriter.dll”)来创建XLS文件。您可以在网上搜索使用此dll的样本或在此处查看
#9
0
You can start using this files. It will create a excel file and help you download it. Download Souce code or live demo here : http://purpledesign.in/blog/to-generate-excel-using-php/
您可以开始使用此文件。它将创建一个excel文件并帮助您下载它。在这里下载Souce代码或现场演示:http://purpledesign.in/blog/to-generate-excel-using-php/
You can add rows like this in the
您可以在中添加这样的行
01-simple-download-xlsx.php
01,简单下载,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++;
}
#1
14
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文件中不可能的其他功能。此外,请记住,打开CSV文件时Excel不能正确支持UTF-8编码的内容。
If you want a formatted XLS file, then you need a library such as PhpSpreadsheet that can write a file with that formatting, or COM if you're server is running on Windows with Excel installed
如果你想要一个格式化的XLS文件,那么你需要一个像PhpSpreadsheet这样可以用这种格式编写文件的库,或者如果服务器在安装了Excel的Windows上运行的话需要COM
#2
12
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文件!
#3
3
You could use one of the XML formats that Microsoft Office understands.
您可以使用Microsoft Office理解的XML格式之一。
See Microsoft's OpenXML Developer site for details/specs.
有关详细信息/规格,请参阅Microsoft的OpenXML Developer站点。
There is a library on called PHPExcel that 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。
#4
2
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文件容易得多。
#5
#6
#7
1
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示例
Here's another link to DocRaptor's home page:
这是DocRaptor主页的另一个链接:
使用DocRaptor将HTML转换为Excel
#8
0
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
如果您需要丰富的XLS文件,有许多选项,包括使用第三方DLL(如“CarlosAg.ExcelXmlWriter.dll”)来创建XLS文件。您可以在网上搜索使用此dll的样本或在此处查看
#9
0
You can start using this files. It will create a excel file and help you download it. Download Souce code or live demo here : http://purpledesign.in/blog/to-generate-excel-using-php/
您可以开始使用此文件。它将创建一个excel文件并帮助您下载它。在这里下载Souce代码或现场演示:http://purpledesign.in/blog/to-generate-excel-using-php/
You can add rows like this in the
您可以在中添加这样的行
01-simple-download-xlsx.php
01,简单下载,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++;
}