Phpexcel Excel data import into a database

Source: Internet
Author: User

Recently done project encountered to the Excel table data import into the MySQL database, has been error: The file does not exist or unreadable, after an afternoon of the wrong, finally fix, not much to say, direct code:

function Impuser () {
Ini_set (' Memory_limit ', ' 1024M ');
if (!empty ($_files)) {
$config = Array (
' exts ' = = Array (' xlsx ', ' xls '),
' MaxSize ' = 3145728000,
' RootPath ' = './public/',
' Savepath ' = ' uploads/',
' SubName ' = Array (' Date ', ' YMD '),
);
$upload = new \think\upload ($config);
if (! $info = $upload->upload ()) {
$this->error ($upload->geterror ());
}
Vendor ("Phpexcel.phpexcel");
$file _name= $upload->rootpath $info [' photo '] [' Savepath ']. $info [' photo '] [' savename '];
$extension = Strtolower (PathInfo ($file _name, pathinfo_extension));//Determine Import table suffix format
if ($extension = = ' xlsx ') {
$objReader =\phpexcel_iofactory::createreader (' Excel2007 ');
$objPHPExcel = $objReader->load ($file _name, $encode = ' utf-8 ');
} else if ($extension = = ' xls ') {
$objReader =\phpexcel_iofactory::createreader (' Excel5 ');
$objPHPExcel = $objReader->load ($file _name, $encode = ' utf-8 ');
}
$sheet = $objPHPExcel->getsheet (0);
$highestRow = $sheet->gethighestrow ();//Get Total rows
$highestColumn = $sheet->gethighestcolumn (); Total number of columns obtained
D (' Expert ')->execute (' TRUNCATE table Qw_expert ');
for ($i = 2; $i <= $highestRow; $i + +) {
Look here, the front lowercase a is the field name in the table, and the upper case A is the position in Excel
$data [' id '] = $objPHPExcel->getactivesheet ()->getcell ("A". $i)->getvalue ();
$data [' name '] = $objPHPExcel->getactivesheet ()->getcell ("B". $i)->getvalue ();
$data [' sex '] = $objPHPExcel->getactivesheet ()->getcell ("C". $i)->getvalue ();
$data [' thumbnail '] = $objPHPExcel->getactivesheet ()->getcell ("D". $i)->getvalue ();
$data [' workunit '] = $objPHPExcel->getactivesheet ()->getcell ("E". $i)->getvalue ();
$data [' phone '] = $objPHPExcel->getactivesheet ()->getcell ("F". $i)->getvalue ();
$data [' sex '] = $objPHPExcel->getactivesheet ()->getcell ("G". $i)->getvalue ();
$data [' email '] = $objPHPExcel->getactivesheet ()->getcell ("H". $i)->getvalue ();
$data ['->getactivesheet '] = $objPHPExcel ()->getcell ("I". $i)->getvalue ();
$data [' sort '] = $objPHPExcel->getactivesheet ()->getcell ("J". $i)->getvalue ();
$data [' time '] = $objPHPExcel->getactivesheet ()->getcell ("K". $i)->getvalue ();

Look here, look at this place. Write the table name in the database

D (' Expert ')->add ($data);
}
$this->success (' Import succeeded! ');
} else {
$this->error ("Please select the uploaded file");
}



}

Description: 1,

2. Download Phpexcel

3. Be consistent with the fields of the data.

Phpexcel Excel data import into a database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.