Home » excel » Editing Data in an XLS with PHP then importing into mySQL

Editing Data in an XLS with PHP then importing into mySQL

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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