(Super Detail Edition) using Thinkphp3.2.3+phpexcel to import tabular data into a database

Source: Internet
Author: User

Please read the following steps and download the source code at the endThe first step:download thinkphp_3.2.3 and phpexcel_1.8.0 and unzipthe respective websites are:http://www.thinkphp.cn/down.html
Https://github.com/PHPOffice/PHPExcel
 Step Two:① Copy the extracted thinkphp files to the Web site root directory② and create the index.php code as follows 
 Step Three:① in thinkphp\library\vendor Directory new folder Phpexcel ②copy files from Phpexcel _1.8.0_doc\classes to thinkphp\library\vendor\
In Phpexcel  
 Fourth Step:

① run the root directory of the index.php file and manually create the public folder

Thinkphp Welcome Interface:


root directory structure: The ②application directory is as follows: 
 Fifth Step: thinkphp connect to our MySQL database file Location: root directory/application/home/conf/config.php 
  sixth step: Create a folder" Index "  (note case) and create a index.html file in the folder
Code: <! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Document</title>
<body>
<form action= "{: U (' Upload ')}" enctype= "Multipart/form-data" method= "POST" >
<input type= "file" name= "Photo"/>
<input type= "Submit" value= "Import Data" >
</body>
 Seventh Step: Modify the IndexController.class.php file in the Application\home\controller directory
  
 
 The code is as follows:<?php
namespace Home\controller;
Use Think\controller;
Use Phpexcel_iofactory;
Use Phpexcel;
Use Behavior;
Class Indexcontroller extends Controller {
Public Function index () {
$this->display ();
}
Public function upload () {
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 (' Pro_info ')->execute (' TRUNCATE table Pro_info ');
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 [' pId '] = $objPHPExcel->getactivesheet ()->getcell ("A". $i)->getvalue ();
$data [' pName '] = $objPHPExcel->getactivesheet ()->getcell ("B". $i) GetValue ();
$data [' pprice '] = $objPHPExcel->getactivesheet ()->getcell ("C". $i) GetValue ();
$data [' pcount '] = $objPHPExcel->getactivesheet ()->getcell ("D". $i) GetValue ();
//look here, this location write the table name in the database

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

}
}
/span>  Eighth step: Run thinkphp, import Excel file   Excel file contents (according to the database):
database structure:  finally---- done!!!

# #源码

75913317

 

(Super Detail Edition) using Thinkphp3.2.3+phpexcel to import tabular data 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.