Home >Backend Development >PHP Tutorial >PHP import excel data to mysql_PHP tutorial

PHP import excel data to mysql_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:21:53938browse

Example:

1. Import an excel file with only one sheet
require_once ("db.php"); //Reference database instantiation class
require_once ("reader.php"); // Apply the class to import excel
$data = new Spreadsheet_Excel_Reader(); //Instantiate class
$data->setOutputEncoding('utf-8');//Set encoding
$data->read($_FILES["excel"]["tmp_name"]);//Read excel temporary file
if ($data->sheets[0]['numRows']>0){ //Determine whether the number of rows in excel is greater than 0 rows $data->sheets[0]['numRows' ] is the total number of rows in excel
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) { //Insert execl data into the database $i represents the $ith item from excel Line starts reading
                                                                                                                                                                         
                      '{$data->sheets[0]['cells'][$i][1]}', //$i is the row number in excel
                     '{$data->sheets[0]['cells'][$i][2]}',
                    '{$data->sheets[0]['cells'][$i][3]}',
                                                                                                               
)";
$db->query($sql);
}
                                                                   
}
2 Import excel files with multiple sheets
In fact, it is the same as importing a sheet. If there are two sheets (and so on)
require_once ("db.php"); //Reference database instantiation class
require_once ("reader.php"); // Apply the class to import excel
$data = new Spreadsheet_Excel_Reader(); //Instantiate class
$data->setOutputEncoding('utf-8');//Set encoding
$data->read($_FILES["excel"]["tmp_name"]);//Read excel temporary file
if ($data->sheets[0]['numRows']>0){ //Judge whether the number of rows in excel is greater than 0 rows$data->sheets[0]['numRows' ] is the total number of rows in excel. Here, $data->sheets[0] represents the first sheets
in excel.
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) { //Insert execl data into the database $i represents the $ith item from excel Line starts reading
                                                                                                                                                                         
                      '{$data->sheets[0]['cells'][$i][1]}', //$i is the row number in excel
                     '{$data->sheets[0]['cells'][$i][2]}',
                   '{$data->sheets[0]['cells'][$i][3]}',
                                                                                                         
)";
$db->query($sql);
}
if ($data->sheets[1]['numRows']>0){ //Determine whether the number of rows in excel is greater than 0 rows $data->sheets[0]['numRows' ] is the total number of rows in excel. Here, $data->sheets[1] represents the second sheets
for ($i = 2; $i <= $data->sheets[1]['numRows']; $i++) { //Insert execl data into the database $i represents the $ith item from excel Line starts reading
                                                                                                                                                                         
                      '{$data->sheets[1]['cells'][$i][1]}', //$i is the row number in excel
                      '{$data->sheets[1]['cells'][$i][2]}',
                  '{$data->sheets[1]['cells'][$i][3]}',
                                                                                                               
        )";
$db->query($sql);
}
                                                                   
}
http://www.bkjia.com/PHPjc/477132.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477132.htmlTechArticleExample: 1. Import an excel file with only one sheet require_once (db.php); //Reference database instantiation Class require_once (reader.php); // Apply the class imported into excel $data = new Spreadsheet...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn