简体   繁体   English

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

[英]Editing Data in an XLS with PHP then importing into mySQL

I am trying to import an XLS file into PHP, where I can then edit the information and import it into mySQL. 我正在尝试将XLS文件导入PHP,然后我可以编辑信息并将其导入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 PHP Excel Reader
  • ExcelRead ExcelRead
  • PHPExcel 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. XLS文件无法转换为任何其他文件格式。 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. XLS文件是在另一个网站上生成的报告,每次都具有相同的格式(列)。 For example, every XLS file with have the same amount of columns (this would be A1): 例如,每个具有相同数量的XLS文件(这将是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. 但是,XLS文件中的列数多于要导入数据库的列数。 For example, the rows that are being imported (this would be A1): 例如,正在导入的行(这将是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. 我知道编辑数据的两种方法之一是A.使用PHPExcel之类的东西来读入数据,编辑它,然后将它发送到DB或B.使用类似PHPExcel的东西将XLS转换为CSV,做一个原始导入临时表,编辑数据,并将其插入旧表。

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. 我已经阅读了很多PHPExcel文档,但它没有任何关于导入到数据库的内容,我甚至不知道在导入之前或之后从哪里开始编辑XLS。

I have googled a lot of keywords and mostly found results on how to read/write/preview XLS. 我搜索了很多关键字,并且主要找到了如何读取/写入/预览XLS的结果。 I am looking for advice on the best way of doing all of these things in the least and simplest steps. 我正在寻找关于以最简单和最简单的步骤完成所有这些事情的最佳方式的建议。

See this article on using PHP-ExcelReader , in particular the short section titled "Turning the Tables". 请参阅有关使用PHP-ExcelReader的这篇文章 ,特别是标题为“转动表”的简短部分。

Any solution you have will end up looking like this: 你有任何解决方案将最终看起来像这样:

  1. Read a row from the XLS (requires an XLS reader) 从XLS中读取一行(需要XLS阅读器)
  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. 这只是PHP - 您从XLS阅读器获取值,使用PHP代码修改它,然后插入数据库。 There's no intermediate file, you don't modify the XLS--it's just PHP. 没有中间文件,你不修改XLS - 它只是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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM