在 PHP 中从 Excel 导入数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2066961/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:53:25  来源:igfitidea点击:

Import Data from Excel in PHP

phpmysqlimport-from-excel

提问by air

I want to import data from excel file using PHP and then if possible, save it to a MySQL database.

我想使用 PHP 从 excel 文件中导入数据,然后如果可能,将其保存到 MySQL 数据库中。

回答by AlexV

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...

从 Excel 文件 (XLS) 导入比从 CSV 文件导入要困难得多。通常我用 Excel 将我的 XLS 保存到 CSV,然后用 PHP 处理这个 CSV ...

Look at PHP function fgetcsv at: http://ca.php.net/manual/en/function.fgetcsv.php

查看 PHP 函数 fgetcsv: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);
}
?> 

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 加载 XLS,这是可能的(但有多可靠)...快速搜索导致http://sourceforge.net/projects/phpexcelreader/这可能会有所帮助。

回答by zombat

Quite possible. You can save your Excel file as a CSV file, and use fgetcsv()to read that file in to PHP. fgetcsv()will parse your data into an array, which you can then create SQL queries out of to put into your database.

很有可能。您可以将 Excel 文件另存为 CSV 文件,然后使用fgetcsv()将该文件读入 PHP。 fgetcsv()将您的数据解析为一个数组,然后您可以创建 SQL 查询并将其放入您的数据库中。

If all you're doing is putting it into a database, you might be able to bypass the need for a PHP script entirely and just use MySQL's LOAD DATA INFILEsyntax on your CSV file instead:

如果您所做的只是将其放入数据库中,您可能可以完全绕过对 PHP 脚本的需求,而只需LOAD DATA INFILE在 CSV 文件中使用 MySQL 的语法:

LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);

回答by FrustratedWithFormsDesigner

Best bet is to export from Excel to a CSV (Comma separated values) file. These files are easy to parse and load. If you are reading directly from an XLS file, I'm not sure how to do that. You might want to look and see if there is a libarary for PHP that can read Excel data files.

最好的办法是从 Excel 导出到 CSV(逗号分隔值)文件。这些文件易于解析和加载。如果您直接从 XLS 文件中读取,我不知道该怎么做。您可能想查看是否有可以读取 Excel 数据文件的 PHP 库。

回答by AndiDog

Here's a tutorial on reading/writing an Excel spreadsheet directly (without having to export to CSV). The necessary packages are available from SourceForge and PEAR (cf. article).

是直接读取/写入 Excel 电子表格的教程(无需导出为 CSV)。SourceForge 和 PEAR 提供了必要的软件包(参见文章)。

回答by Ashwani Singh

<?
 i$db = mysql_connect(“localhost”, “root”, “”) or die(“Could not connect.”);

if(!$db)

die(“no db”);

if(!mysql_select_db(“test”,$db))

die(“No database selected.”);

if(isset($_POST['submit']))

{

$filename=$_POST['filename'];

$handle = fopen(“$filename”, “r”);

while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE)

{

$import=”INSERT into sample(name,email) values(‘$data[0]‘,'$data[1]‘)”;

mysql_query($import) or die(mysql_error());

}

fclose($handle);

print “Import done”;

}

else

{

print “<form action='import.php' method='post'>”;

print “Type file name to import:<br>”;

print “<input type='text' name='filename' size='20′><br>”;

print “<input type='submit' name='submit' value='submit'></form>”;

}
 ?>

Source

来源