Thinkphp使用PHPExcel实现导入Excel类,代码如下:
<?php
class ExcelToArrary{
public function __construct(){
Vendor(“Excel.PHPExcel”);//引入phpexcel类(注意你自己的路径)
Vendor(“Excel.PHPExcel.IOFactory”);
}
public function read($filename,$encode,$file_type){
//判断excel表类型为2003还是2007
if(strtolower($file_type )==’xls’){
Vendor(“Excel.PHPExcel.Reader.Excel5”);
$objReader = PHPExcel_IOFactory::createReader(‘Excel5′);
}elseif(strtolower($file_type )==’xlsx’){
Vendor(“Excel.PHPExcel.Reader.Excel2007”);
$objReader = PHPExcel_IOFactory::createReader(‘Excel2007’);
}
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
//获取工作表的数目
$sheetCount =$objPHPExcel->getSheetCount();
$excelData = array();
//循环工作表
for($_s=0;$_s<$sheetCount;$_s++){
//选择工作表
$objWorksheet = $objPHPExcel->getSheet($_s);
//取得一共有多少行
$highestRow = $objWorksheet->getHighestRow();
//取得一共有多少列
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
//循环组成三维数组
for ($row = 1;$row <= $highestRow;$row++){
for ($col = 0;$col < $highestColumnIndex;$col++){
$excelData[$_s][$row][]=(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
}
return $excelData;
}
}
暂无评论内容