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