问题描述
我想使用PHP从excel文件导入数据,然后将其保存到MySQL数据库.
I want to import data from excel file using PHP and then if possible, save it to a MySQL database.
推荐答案
从Excel文件(XLS)导入要比从CSV文件导入更困难.通常,我使用Excel将XLS保存到CSV,然后使用PHP在此CSV上工作...
Importing from Excel files (XLS) is way harder than improting from CSV files. Usually I save my XLS to CSV with Excel then work on this CSV with PHP...
在以下位置查看PHP函数fgetcsv: http://ca.php.net/manual/zh/function.fgetcsv.php
Look at PHP function fgetcsv at:http://ca.php.net/manual/en/function.fgetcsv.php
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
如果您仍然想直接从PHP加载XLS,则有可能(但是多么可靠)...快速搜索导致 http://sourceforge.net/projects/phpexcelreader/可能会有所帮助.
If you still want to load XLS directly from PHP it's possible (but how reliable)... A quick seach resulted in http://sourceforge.net/projects/phpexcelreader/ which might be helpful.
这篇关于在PHP中从Excel导入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!