Excel 到 PHP 数组,可能和如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8170525/
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
Excel to PHP array, possible and how?
提问by Karem
I have a big excel file that looks like this:
我有一个像这样的大 excel 文件:
I would like to put each row into an array.
我想将每一行放入一个数组中。
Is this possible to access the first row's order id
like this?
这可以order id
像这样访问第一行吗?
$result[0][2] // returns 7432
Assuming the actual first row that gives prefix for the columns' name is not present.
假设不存在为列名称提供前缀的实际第一行。
How could I do that?
我怎么能那样做?
采纳答案by Dan
Save the spreadsheet as a CSV, then use PHP's built-in CSV functions. See the sample code here:
将电子表格另存为 CSV,然后使用 PHP 的内置 CSV 函数。请参阅此处的示例代码:
回答by Haim Evgi
I recommended to use PHPEXCEL library
我推荐使用 PHPEXCEL 库
https://github.com/PHPOffice/PHPExcel
https://github.com/PHPOffice/PHPExcel
you can see an example
你可以看到一个例子
回答by Rostyslav Pylypenko
May be my answer is too simple (for one time work only), but I use the CONCATENATE "Function" in excell. The last cell on each row will have concatenation function, like this:
可能是我的答案太简单了(仅用于一次工作),但我在 Excel 中使用了 CONCATENATE “Function”。每行的最后一个单元格将具有连接功能,如下所示:
=CONCATENATE("['";A2;"'=>['data1' => '";B2;"', 'data2' => '";C2;"'],")
where:
在哪里:
column "A" is ID of something;
column "B" is first characteristic;
column "C" is second characteristic;
etc.
Then just copy and paste function results to Your script or config file, and do not forget the first and the last bracket.
然后只需将函数结果复制并粘贴到您的脚本或配置文件中,不要忘记第一个和最后一个括号。
回答by paulalexandru
This works for me:
这对我有用:
$content = file_get_contents($your_file_path);
$lines = array_map("rtrim", explode("\n", $content));
回答by Saed Yousef
Since the PHPExcel library deprecated they've released "PhpSpreadsheet"
由于 PHPExcel 库已弃用,因此他们发布了“PhpSpreadsheet”
This will help PhpSpreadsheet
这将有助于PhpSpreadsheet