Most effective way to create Excel files

好久不见. 提交于 2019-12-24 00:15:03

问题


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

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

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

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?

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

回答1:


There are alternatives to PHPExcel, as listed here

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

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.

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.




回答2:


Yes there is. You can use Pear's Spreadsheet_Excel_Writer. It only creates BIFF8 or older files (Word 2003), but it works quite well.

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...



来源:https://stackoverflow.com/questions/4046607/most-effective-way-to-create-excel-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!