简体   繁体   English

创建Excel文件的最有效方法

[英]Most effective way to create Excel files

Looking for a PHP solution to create Excel files on the fly (Think dynamic reporting). 寻找一个PHP解决方案以动态创建Excel文件(请考虑动态报告)。

Now I have seen PHP Excel and I know I can create a CSV file but are these by best options? 现在,我已经了解了PHP Excel,并且知道可以创建CSV文件,但是这些是最好的选择吗?

I running this script on a Linux system using PHP Excel but it doesn't set all the options 我在使用PHP Excel的Linux系统上运行此脚本,但未设置所有选项

$objPHPExcel->getProperties()->setCreator("Phill");
$objPHPExcel->getProperties()->setLastModifiedBy("Phill");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX");

Also if I give the extension .xls instead of the .xlsx it throws a invalid file and doesn't open. 另外,如果我给扩展名是.xls而不是.xlsx,它会抛出一个无效文件并且无法打开。 (NOTE: I'm using Open Office to view the generated excel sheet) (注意:我正在使用Open Office查看生成的Excel工作表)

Wanted to know if there are any other/better solutions out there? 想知道是否还有其他/更好的解决方案?

EDIT: 编辑:

How I save the file 我如何保存文件

$file = '/path/to/777/dir/file.xlsx'; // not viewable by public
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($file);

There are alternatives to PHPExcel, as listed here 此处列出 PHPExcel的替代方法

But before you dismiss PHPExcel out of hand, I'd like to know why it isn't working. 但是,在您立即取消使用PHPExcel之前,我想知道为什么它不起作用。 This is something that normally works without any problems; 这通常是没有任何问题的。 but your code snippet doesn't show anything about saving the file. 但是您的代码段未显示任何有关保存文件的信息。 How are you saving the file: which writer are you using? 您如何保存文件:您正在使用哪个作家?

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('/path/to/777/dir/file.xls');

EDIT 编辑

or 要么

$file = '/path/to/777/dir/file.xls'; // not viewable by public 
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 
$objWriter->save($file); 

The latest version of Open Office will (I believe) read .xlsx files as well, but it does prefer if they have the correct extension. (我相信)最新版本的Open Office也将读取.xlsx文件,但是如果它们具有正确的扩展名,它确实更喜欢。

Note that the Excel5 Writer doesn't support document properties (though Excel2007 does). 请注意,Excel5 Writer不支持文档属性(尽管Excel2007支持)。 There is an active work item for this on the PHPExcel issues list. PHPExcel问题列表上有一个活跃的工作项。

Yes there is. 就在这里。 You can use Pear's Spreadsheet_Excel_Writer . 您可以使用Pear的Spreadsheet_Excel_Writer It only creates BIFF8 or older files (Word 2003), but it works quite well. 它仅创建BIFF8或更旧的文件(Word 2003),但是效果很好。

Edit: I just noticed they are saying it needs a complete rewrite and don't recommend using it for new development. 编辑:我只是注意到他们说它需要完全重写,并且不建议将其用于新开发。 I've been using it for about 3 years in production systems, and other than a few tiny little bugs it's worked great for me... 我已经在生产系统中使用了大约3年的时间,除了一些小bug之外,它对我来说非常有用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM