background image

PHP 如何利用 phpexcel 导入数据库

以下是对

PHP 中利用 phpexcel 导入数据库的实现代码进行了介绍,需要的朋友可以过来参

考下
废话不多说,直接上代码吧
代码如下

:

 
<?php 

error_reporting

(E_ALL); 

//开启错误 

set_time_limit(0); 

//脚本不超时 

 
date_default_timezone_set('Europe/London'); 

//设置时间 

 

/** Include path **/

set_include_path(get_include_path() . PATH_SEPARATOR . '');//设置环境变量 
 

/** PHPExcel_IOFactory */
include

 'PHPExcel/IOFactory.php'; 

 

//$inputFileType = 'Excel5';    //这个是读 xls 的 

    

$inputFileType

 = 'Excel2007';

//这个是计 xlsx 的 

//$inputFileName = './sampleData/example2.xls'; 

$inputFileName

 = './sampleData/book.xlsx'; 

 

        echo

 'Loading file ',

pathinfo

(

$inputFileName

,PATHINFO_BASENAME),' using IOFactory 

with a defined reader type of ',

$inputFileType

,'<br />'; 

        

$objReader

 = PHPExcel_IOFactory::createReader(

$inputFileType

); 

        

$objPHPExcel

 = 

$objReader

->load(

$inputFileName

); 

        

/* 

        

$sheet = $objPHPExcel->getSheet(0); 

        

$highestRow = $sheet->getHighestRow(); //取得总行数 

        

$highestColumn = $sheet->getHighestColumn(); //取得总列 

        

*/

    

        

$objWorksheet

 = 

$objPHPExcel

->getActiveSheet();

//取得总行数 

        

$highestRow

 = 

$objWorksheet

->getHighestRow();

//取得总列数 

 

        echo

 'highestRow='.

$highestRow

        echo

 "<br>"; 

        

$highestColumn

 = 

$objWorksheet

->getHighestColumn(); 

        

$highestColumnIndex

 = PHPExcel_Cell::columnIndexFromString(

$highestColumn

);

//总列