且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 PHP 网站中将数据从 Excel 导入 MySQL

更新时间:2022-06-17 10:09:42

如果您希望 Excel 文件本身需要导入,您可以尝试使用以下任何库.

You can try with any of the below libraries if you want Excel file itself need to be imported.

http://phpexcel.codeplex.com/

http://sourceforge.net/projects/phpexcelreader/

注意:

从 Excel 文件导入比从 CSV 文件导入更难.所以我建议你提供一个从 CSV 导入 MySQL 的选项.(用户可以使用 Excel 将 XLS 转换为 CSV)

Importing from Excel files is harder than improting from CSV files. So I suggest you to provide an option for importing into MySQL from CSV. (Users can convert XLS to CSV using Excel)

查看 PHP 函数 fgetcsv 位于:

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