复制代码 代码如下:
$inputFileType = 'Excel2007';
$inputFileName = './public/files/import_user_template.xlsx';
$sheetname = 'Sheet1';
//指定Excel类型,创建一个reader
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
//设置只读取数据,不包括公式和格式
$objReader->setReadDataOnly(true);
//只读取指定的sheet
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);
$curSheet = $objPHPExcel->getSheet(0);
//包含数据的最大列
$allColumn = $curSheet->getHighestColumn();
//包含数据的最大行
$allRow = $curSheet->getHighestRow();
for($currentRow = 1; $currentRow <= $allRow; $currentRow++){
for($currentCol = 'A'; $currentCol <= $allColumn; $currentCol++){
echo $curSheet->getCell($currentCol.$currentRow)->getValue()."/t";
}
echo "/r/n";
}
复制代码 代码如下:
vendor('PHPExcel.PHPExcel');
复制代码 代码如下:
spl_autoload_register(array('Think','autoload'));
复制代码 代码如下:
protected function Import_Execl($file){
if(!file_exists($file)){
return array("error"=>1);
}
Vendor("PHPExcel.PHPExcel");
$PHPExcel = new PHPExcel();
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($file)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($file)){
return array("error"=>2);
}
}
$PHPExcel = $PHPReader->load($file);
$SheetCount = $PHPExcel->getSheetCount();
for($i=0;$i<$SheetCount;$i++){
$currentSheet = $PHPExcel->getSheet($i);
$allColumn = $this->ExcelChange($currentSheet->getHighestColumn());
$allRow = $currentSheet->getHighestRow();
$array[$i]["Title"] = $currentSheet->getTitle();
$array[$i]["Cols"] = $allColumn;
$array[$i]["Rows"] = $allRow;
$arr = array();
for($currentRow = 1 ;$currentRow<=$allRow;$currentRow++){
$row = array();
for($currentColumn=0;$currentColumn<$allColumn;$currentColumn++){
$row[$currentColumn] = $currentSheet->getCellByColumnAndRow($currentColumn,$currentRow)->getValue();
}
$arr[$currentRow] = $row;
}
$array[$i]["Content"] = $arr;
}
spl_autoload_register(array('Think','autoload'));//必须的,不然ThinkPHP和PHPExcel会冲突
unset($currentSheet);
unset($PHPReader);
unset($PHPExcel);
unlink($file);
return array("error"=>0,"data"=>$array);
}
protected function ExcelChange($str){//配合Execl批量导入的函数
$len = strlen($str)-1;
$num = 0;
for($i=$len;$i>=0;$i--){
$num += (ord($str[$i]) - 64)*pow(26,$len-$i);
}
return $num;
}
复制代码 代码如下:
public function import(){
if(isset($_FILES["import"]) && ($_FILES["import"]["error"] == 0)){
$result = $this->Import_Execl($_FILES["import"]["tmp_name"]);
if($this->Execl_Error[$result["error"]] == 0){
$execl_data = $result["data"][0]["Content"];
unset($execl_data[1]);
$data = D("Data");
foreach($execl_data as $k=>$v){
$d["serial_no"] = $v[0];
$d["check_no"] = $v[1];
$d["work_no"] = $v[2];
$d["class_name"] = $v[3];
$d["user_name"] = $v[4];
$d["new_class"] = $v[5];
$d["error_level"] = $v[6];
$data->data($d)->add();
}
$this->success($this->Execl_Error[$result["error"]]);
}else{
$this->error($this->Execl_Error[$result["error"]]);
}
}else{
$this->error("上传文件失败");
}
}
复制代码 代码如下:
protected $Execl_Error = array("数据导入成功","找不到文件","Execl文件格式不正确");
新闻热点
疑难解答