如何使用 PHP 读取 .xls 文件(Excel)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8128616/
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
How can i read .xls files (Excel) with PHP?
提问by Kristian Rafteseth
I need to convert an .xls file to text, i dont need it formatted in any way. The file is filled with productids like PN-5098P, which i will preg_match_all to get.
我需要将 .xls 文件转换为文本,我不需要以任何方式对其进行格式化。该文件充满了像 PN-5098P 这样的产品 ID,我将使用 preg_match_all 来获取。
What is the simplest solution? I tried simply opening the file in a regular texteditor, the ids are not showing, so i guess i need some kind of binary converter?
最简单的解决方案是什么?我试着在普通的文本编辑器中简单地打开文件,id 没有显示,所以我想我需要某种二进制转换器?
回答by Matchu
XLS is a complex proprietary format, and it might be difficult to work with. If you end up having trouble with libraries like Clint suggested, consider only accepting files in a more simpler format, like CSV, which is super-duper easy to deal with. If that solution is acceptable in this situation, it'll end up making your life much easier.
XLS 是一种复杂的专有格式,可能难以使用。如果您最终在使用Clint 建议的库时遇到问题,请考虑仅接受更简单格式的文件,例如 CSV,它非常容易处理。如果在这种情况下该解决方案是可以接受的,它最终会让您的生活变得更轻松。
回答by paulsm4
Newer versions of MS Office can output your spreadsheet directly in XML format. Here's an example of using such an XML-format spreadsheet directly from PHP:
较新版本的 MS Office 可以直接以 XML 格式输出电子表格。下面是一个直接从 PHP 使用这种 XML 格式电子表格的示例:
Otherwise, I've heard good things about PHPExcel:
否则,我听说过有关 PHPExcel 的好消息:
Here's one other link that might at least give you some additional ideas:
这是另一个链接,至少可以为您提供一些额外的想法:
回答by Vera de Kok
I convert my Excel sheets to MySQL using xls2mysql.com, but it isn't perfect. It doesn't recognize characters like -, /, é, ü, ? etc.. I've been working around this by converting those characters to a number in between hash tags (#4# for -) and unconverting them afterwards, but it is a bit of a hassle.
我使用xls2mysql.com将我的 Excel 工作表转换为 MySQL ,但它并不完美。它不能识别像 -、/、é、ü、? 等等。我一直在解决这个问题,将这些字符转换为哈希标签之间的数字(#4# 代表 -),然后再将它们转换,但这有点麻烦。