使用 PHP 编辑 XLS 中的数据,然后导入到 mySQL 中

发布于 2024-12-29 21:39:43 字数 870 浏览 2 评论 0原文

我正在尝试将 XLS 文件导入 PHP,然后我可以在其中编辑信息并将其导入 mySQL。我从来没有做过与此相关的任何事情,所以我很难掌握如何处理它。

我查看了一些开源项目:

  • PHP Excel Reader
  • ExcelRead
  • PHPExcel

这些选项都没有完全适合我想要做的事情,或者也许我只是没有深入了解文档。

有一些事情需要考虑。 XLS 文件无法转换为任何其他文件格式。这样做是为了方便非技术用户访问。 XLS 文件是在另一个网站上生成的报告,每次都具有相同的格式(列)。 例如,每个 XLS 文件都具有相同数量的列(这将是 A1):

*ID   |Email   |First Name  |Last Name  |Paid    |Active   |State  |Country|*

但是,XLS 文件中的列数比要导入到数据库中的列数要多。 例如,正在导入的行(这将是 A1):

*ID   |Email  |First Name  |Last Name  |Country*

我知道编辑数据的两种方法之一是 A。使用 PHPExcel 之类的东西读取数据,编辑它,然后将其发送到数据库或 B. 使用 PHPExcel 之类的工具将 XLS 转换为 CSV,对临时表进行原始导入,编辑数据,然后将其插入旧表中。

我已经阅读了很多 PHPExcel 文档,但是它没有任何关于导入数据库的内容,而且我什至不知道在导入之前或之后从哪里开始编辑 XLS。

我用 google 搜索了很多关键字,大部分找到了有关如何读取/写入/预览 XLS 的结果。我正在寻求有关以最少和最简单的步骤完成所有这些事情的最佳方法的建议。

I am trying to import an XLS file into PHP, where I can then edit the information and import it into mySQL. I have never done anything related to this, so I am having a hard time grasping how to approach it.

I have looked at a few open source projects:

  • PHP Excel Reader
  • ExcelRead
  • PHPExcel

None of these options perfectly fit what I want to do or maybe I just haven't gone deep enough into the documentation.

There are some things that needed to be taken into consideration. The XLS file cannot be converted into any other file format. This is being made for ease-of-access for nontechnical users. The XLS file is a report generated on another website that will have the same format (columns) every time.
For example, every XLS file with have the same amount of columns (this would be A1):

*ID   |Email   |First Name  |Last Name  |Paid    |Active   |State  |Country|*

But, there are more columns in the XLS file than what is going to be imported into the DB.
For example, the rows that are being imported (this would be A1):

*ID   |Email  |First Name  |Last Name  |Country*

I know one of two ways to do edit the data would be A. Use something like PHPExcel to read in the data, edit it, then send it to the DB or B. Use something like PHPExcel to convert the XLS to CSV, do a raw import into a temp table, edit the data, and insert it into the old table.

I have read a lot of the PHPExcel documentation but, it doesn't have anything on importing into a database and I don't really even know where to start with editing the XLS before or after importing.

I have googled a lot of keywords and mostly found results on how to read/write/preview XLS. I am looking for advice on the best way of doing all of these things in the least and simplest steps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2025-01-05 21:39:43

请参阅这篇有关使用 PHP-ExcelReader 的文章,特别是标题为“扭转局面”的简短部分。

您拥有的任何解决方案最终都会如下所示:

  1. 从 XLS 读取一行(需要 XLS 读取器)
  2. 根据数据库需要修改该行中的数据。
  3. 将修改后的数据插入数据库。

您似乎对“编辑数据”很着迷。这只是 PHP——您从 XLS 读取器获取一个值,用 PHP 代码修改它,然后插入数据库。没有中间文件,您无需修改​​ XLS——它只是 PHP。

这是您需要编写的程序的内部循环的一个超级简单、未经测试的示例。这只是为了说明一般模式。

$colsYouWant = array(1,2,3,4,8);
$sql = 'INSERT INTO data (id, email, fname, lname, country) VALUES (?,?,?,?,?)';
$stmt = $pdo->prepare($sql);

$sheet = $excel->sheets[0];
// the excel reader seems to index by 1 instead of 0: be careful!
for ($rowindex=2; $rowindex <= $sheet['numRows']; $rowindex++) {
    $xlsRow = $sheet['cells'][$rowindex];
    $row = array();
    foreach ($colsYouWant as $colindex) {
        $row[] = $xlsRow[$colindex];
    }
    // now let's "edit the row"
    // trim all strings
    $row = array_map('trim', $row);
    // convert id to an integer
    $row[0] = (int) $row[0];
    // capitalize first and last name
    // (use mb_* functions if non-ascii--I don't know spreadsheet's charset)
    $row[2] = ucfirst(strtolower($row[2]));
    $row[3] = ucfirst(strtolower($row[3]));

    // do whatever other normalization you want to $row

    // Insert into db:
    $stmt->execute($row);
}

See this article on using PHP-ExcelReader, in particular the short section titled "Turning the Tables".

Any solution you have will end up looking like this:

  1. Read a row from the XLS (requires an XLS reader)
  2. Modify the data from the row as needed for your database.
  3. Insert modified data into the database.

You seem to have this fixation on "Editing the data". This is just PHP--you get a value from the XLS reader, modify it with PHP code, then insert into the database. There's no intermediate file, you don't modify the XLS--it's just PHP.

This is a super-simple, untested example of the inner loop of the program you need to write. This is just to illustrate the general pattern.

$colsYouWant = array(1,2,3,4,8);
$sql = 'INSERT INTO data (id, email, fname, lname, country) VALUES (?,?,?,?,?)';
$stmt = $pdo->prepare($sql);

$sheet = $excel->sheets[0];
// the excel reader seems to index by 1 instead of 0: be careful!
for ($rowindex=2; $rowindex <= $sheet['numRows']; $rowindex++) {
    $xlsRow = $sheet['cells'][$rowindex];
    $row = array();
    foreach ($colsYouWant as $colindex) {
        $row[] = $xlsRow[$colindex];
    }
    // now let's "edit the row"
    // trim all strings
    $row = array_map('trim', $row);
    // convert id to an integer
    $row[0] = (int) $row[0];
    // capitalize first and last name
    // (use mb_* functions if non-ascii--I don't know spreadsheet's charset)
    $row[2] = ucfirst(strtolower($row[2]));
    $row[3] = ucfirst(strtolower($row[3]));

    // do whatever other normalization you want to $row

    // Insert into db:
    $stmt->execute($row);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文