Sometimes if there is a large amount of data to import into the database, the lowest level of the method is, a manual add, and daily life, often with a table to record, can let PHP directly read an Excel table, and then, the table content, all imported into the database, this way, can save a lot of time.
Php-excel-reader is a class that reads Excel, and it is easy to use it to read Excel files.
The first thing to download is the file:
Link: http://pan.baidu.com/s/1i5990hv Password: 4NPD
The remaining documents are case files, please carefully analyze the source code.
Table corresponding content:
1: Introduce class, create object, set directory to read files
<?php error_reporting (e_all ^ e_notice); require_once ' excel_reader2.php '; $data = new Spreadsheet_excel_reader ();// Create Object $data->setoutputencoding (' UTF-8 ');//Set Encoding format $data->read ("Example.xls");//Read Excel document
2: After reading, the information about the table will be stored in a large array.
<?phperror_reporting (e_all ^ e_notice); require_once ' excel_reader2.php '; $data = new Spreadsheet_excel_reader ();// Create the Object $data->setoutputencoding (' UTF-8 ');//Set the encoding format $data->read ("Example.xls");//Read the Excel document echo "<pre>"; Print_r ($data->sheets); echo "</pre>";
The operation results are as follows
3: If you want to read, the details in the array, give a few examples
<?phperror_reporting (e_all ^ e_notice); require_once ' excel_reader2.php '; $data = new Spreadsheet_excel_reader ();// Create Object $data->setoutputencoding (' UTF-8 ');//Set Encoding format $data->read ("Example.xls");//Read Excel document//echo "<pre>" ;//print_r ($data->sheets);//echo "</pre>", Echo $data->sheets[0][' NumRows ']. " <br>//read out a few lines of echo $data->sheets[0][' Numcols '). " Column <br> ";//read out a total of several columns of echo $data->sheets[0][' cells '][1][1]." <br> ";//read out the contents of the first column of the first row print_r ($data->sheets[0][' cells '][1]);//The first line of data echo" <br> "; Echo implode (", ", $ data->sheets[0][' cells '][1]). " <br> ";///Remove the first row of data, each middle add delimiter for ($i =1; $i <= $data->sheets[0][' numcols '); $i + +)//read out all data of the first row {echo $data- >sheets[0][' cells '][1][$i]. " ";} echo "<br>", echo "<br>";//read out all data for ($i = 1; $i <= $data->sheets[0][' numrows '); $i + +) {//$data->she ets[0][' Numcols ' for Excel columns for ($j = 1; $j <= $data->sheets[0][' numcols '); $j + +) {//Display each cell contents echo $data->sh eets[0][' cells ' [$i] [$j]. ' '; } Echo ' <br> ';}
Note the above, Echo implode (",", $data->sheets[0][' cells '][1]). " <br> ";//Remove the first row of data, each middle add delimiter, the application, so that you can directly insert a whole row of data into the database
Note:
Dump (), which can output Excel content in HTML format:
echo $data->dump (true,true);
<?phperror_reporting (e_all ^ e_notice); require_once ' excel_reader2.php '; $data = new Spreadsheet_excel_reader (" Example.xls ");? >
Reading an Excel file using Php-excel-reader