Importing data to a database using Phpexcel

Source: Internet
Author: User

Export Function Reference: http://www.cnblogs.com/zhouqi666/p/5978017.html

More serious problem: when the amount of Excel data is large, there will be a memory overflow situation, currently cannot be resolved

the data format for Excel is

A B

preceding corresponds to the database type, followed by the phone number

Database

using A THINKPHP3.2.3 is actually connected to the database when inserting the use and reference to load the file when the TP is just

The code is as follows

Introduction of PhpexcelIf notTP with Require_once
Vendor ("Phpexcel.phpexcel");
Vendor ("Phpexcel.iofactory");
$cacheMethod = \phpexcel_cachedobjectstoragefactory::cache_to_phptemp;
$cacheSettings =Array(' memorycachesize ' = ' 8MB ');
\phpexcel_settings::Setcachestoragemethod($cacheMethod, $cacheSettings);

Instantiating a Exel Object
File path
$file _path = './public/excel/test1.xlsx ';

if(!file_exists ($file _path)) {
die(' File not exists ');
}
File name extension
$ext = Strtolower (PathInfo ($file _path,pathinfo_extension));
if($ext = = ' xlsx ') {
$objReader = \phpexcel_iofactory::Createreader(' Excel2007 ');
$objPHPExcel = $objReader->load ($file _path);
}ElseIf($ext = = ' xls ') {
$objReader = \phpexcel_iofactory::Createreader(' Excel5 ');
$objPHPExcel = $objReader->load ($file _path);
}
$objReader->setreaddataonly (true);
$sheet = $objPHPExcel->getsheet (0);
$highestRow = $sheet->gethighestrow ();//Get Total rows
$highestColumn = $sheet->gethighestcolumn ();//Gets the total number of columns


Ten B
for($i = 1; $i <= $highestRow; $i + +) {
$record =Array();//Declare an array of each record
for($j = ' A '; $j <= $highestColumn; $j + +) {
$record [] = $objPHPExcel->getactivesheet ()->getcell ("$j $i")->getvalue ();//Read cell
}
$record [' type '] = $record [0];
$record [' phone '] = $record [1];
$res = D (' Test ')->add ($record);
Header ("Content-type:text/html;charset=utf-8");
if($res = =false){
Echo' ERROR inserting data! Data format: ';
Dump ($record);
die;
}
}

Importing data to a database using Phpexcel

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.