0% found this document useful (0 votes)
49 views

PHP Import Excel Into Database (Xls & XLSX) - Stack Overflow

Uploaded by

abdulrf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

PHP Import Excel Into Database (Xls & XLSX) - Stack Overflow

Uploaded by

abdulrf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PHP import Excel into database (xls & xlsx) - Stack Overflow http://stackoverflow.

com/questions/11447918/php-import-excel-into-database-xls-xlsx

sign up log in tour help

_
Stack Overflow is a community of 4.7 Join the Stack Overflow community to:
million programmers, just like you,
helping each other.

Join them; it only takes a minute:


Ask Answer and help Get recognized for your
Sign up programming your peers expertise
questions

PHP import Excel into database (xls & xlsx)

I tried to search for some plugins to import Excel file into MySQL database, one of them is http://code.google.com/p/php-excel-reader/

The tool is so powerful that it displays the entire excel content into html.

However, I think I just need to read the Excel file and extract the contents, for example, into an array, and then write a SQL statement for
entering into the database.

Would there be any good codes and packages? Thanks!

php excel import

edited Jun 30 '14 at 17:12 asked Jul 12 '12 at 8:33


JasonMArcher Ham
5,778 8 36 43 286 1 9 20

Google is your friend: de77.com/php/import-excel-xls-document-into-mysql-database – Doa Jul 12 '12 at


8:36

1 Is php-excel-reader not sufficient for you? – Lion Jul 12 '12 at 8:36

@Lion - title mentions both xls and xlsx, so php-excel-reader probably isn't adequate (unless it's been
updated since I last looked) as it only handles xlsx – Mark Baker Jul 12 '12 at 9:00

5 Answers

1 of 4 4/1/2016 6:01 PM
PHP import Excel into database (xls & xlsx) - Stack Overflow http://stackoverflow.com/questions/11447918/php-import-excel-into-database-xls-xlsx

This is best plugin with proper documentation and examples

http://phpexcel.codeplex.com/
Plus point: you can ask for help in its discussion forum and you will get response within a day
from the author itself, really impressive.

edited Apr 3 '13 at 4:23 answered Jul 12 '12 at 8:37


diEcho
27.4k 14 82 148

5 Thanks... I try to oblige, though I can't guarantee a response within a day – Mark Baker Jul 12 '12 at 9:01

1 @MarkBaker , but most of the problems already solved in your discussion forum :) – diEcho Jul 12 '12 at
9:03

Sometimes I need to import large xlsx files into database, so I use spreadsheet-reader as it
can read file per-row. It is very memory-efficient way to import.

<?php
// If you need to parse XLS files, include php-excel-reader
require('php-excel-reader/excel_reader2.php');

require('SpreadsheetReader.php');

$Reader = new SpreadsheetReader('example.xlsx');


// insert every row just after reading it
foreach ($Reader as $row)
{
$db->insert($row);
}
?>

https://github.com/nuovo/spreadsheet-reader

answered Jan 6 '15 at 12:25


shukshin.ivan
1,089 8 18

2 of 4 4/1/2016 6:01 PM
PHP import Excel into database (xls & xlsx) - Stack Overflow http://stackoverflow.com/questions/11447918/php-import-excel-into-database-xls-xlsx

If you can convert .xls to .csv before processing, you can use the query below to import the
csv to the database:

load data local infile 'FILE.CSV' into table TABLENAME fields terminated by ',' enclosed
by '"' lines terminated by '\n' (FIELD1,FIELD2,FIELD3)

edited Jul 12 '12 at 9:14 answered Jul 12 '12 at 9:03


Jon Taylor user1259132
6,234 1 18 38 180 1 2 11

If you save the excel file as a CSV file then you can import it into a mysql database using tools
such as PHPMyAdmin

Im not sure if this would help in your situation, but a csv file either manually or programatically
would be a lot easier to parse into a database than an excel file I would have thought.

EDIT: I would however suggest looking at the other answers rather than mine since @diEcho
answer seems more appropriate.

answered Jul 12 '12 at 8:36


Jon Taylor
6,234 1 18 38

I wrote an inherited class:

<?php
class ExcelReader extends Spreadsheet_Excel_Reader {

function GetInArray($sheet=0) {

$result = array();

for($row=1; $row<=$this->rowcount($sheet); $row++) {


for($col=1;$col<=$this->colcount($sheet);$col++) {
if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
$val = $this->val($row,$col,$sheet);
$result[$row][$col] = $val;
}
}
}
return $result;

3 of 4 4/1/2016 6:01 PM
PHP import Excel into database (xls & xlsx) - Stack Overflow http://stackoverflow.com/questions/11447918/php-import-excel-into-database-xls-xlsx

}
?>

► Run code snippet

So I can do this:

<?php

$data = new ExcelReader("any_excel_file.xls");


print_r($data->GetInArray());

?>

► Run code snippet

answered Feb 23 '15 at 13:42


Ozsvar Istvan
11 1

4 of 4 4/1/2016 6:01 PM

You might also like