本文介绍了在 PHP 网站中将数据从 Excel 导入 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网站,我想让用户选择上传包含所有数据的 excel 文件.

I am building a website in which i want to give the users a choice to upload their excel file which has all the data.

网站建立在 PHP 上,使用的数据库 - MySQL.

Website is built on PHP, Database used- MySQL.

当用户上传 Excel 表格时,所有数据都必须导入到我的数据库中.现在我想使用 PHP 以编程方式进行.谁能帮我解决这个问题.该代码还应该能够从 Excel 文件中的多个选项卡中提取数据.

When a user uploads the excel sheet, all the data has to be imported into my Database. Now i want to do it programatically using PHP. Can anyone help me out with this. The code should also be able to extract data from multiple tabs in the excel file.

谢谢.

推荐答案

如果您希望 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);
}
?>

这篇关于在 PHP 网站中将数据从 Excel 导入 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:04