创建Excel文件的最有效方法 [英] Most effective way to create Excel files

查看:72
本文介绍了创建Excel文件的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一个PHP解决方案来动态创建Excel文件(请考虑动态报告).

Looking for a PHP solution to create Excel files on the fly (Think dynamic reporting).

现在我已经看到 PHP Excel ,而且我知道我可以创建CSV文件,但是最好的选择是这些?

Now I have seen PHP Excel and I know I can create a CSV file but are these by best options?

我在使用PHP Excel的Linux系统上运行此脚本,但未设置所有选项

I running this script on a Linux system using PHP Excel but it doesn't set all the options

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

另外,如果我给扩展名.xls而不是.xlsx,它将抛出一个无效文件并且无法打开. (注意:我正在使用Open Office查看生成的Excel工作表)

Also if I give the extension .xls instead of the .xlsx it throws a invalid file and doesn't open. (NOTE: I'm using Open Office to view the generated excel sheet)

是否想知道是否还有其他/更好的解决方案?

Wanted to know if there are any other/better solutions out there?

我如何保存文件

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

推荐答案

PHPExcel有替代方法,如

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. 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');

编辑

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

(我相信)最新版本的Open Office也会读取.xlsx文件,但是如果它们具有正确的扩展名,它确实更喜欢.

The latest version of Open Office will (I believe) read .xlsx files as well, but it does prefer if they have the correct extension.

请注意,Excel5 Writer不支持文档属性(尽管Excel2007支持). PHPExcel问题列表上有一个活跃的工作项目.

Note that the Excel5 Writer doesn't support document properties (though Excel2007 does). There is an active work item for this on the PHPExcel issues list.

这篇关于创建Excel文件的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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