Thinkphp框架下PHPExcel实现Excel数据的批量化导入导出

时间:2022-11-14 04:21:10

第一步:下载官方的PHPExcel文件,下载地址https://github.com/PHPOffice/PHPExcel

第二步:解压打开,将PHPExcel\Classes\全部文件拷贝到thinkhp项目的根目录\ThinkPHP\Library\Vendor\,如下图所示

Thinkphp框架下PHPExcel实现Excel数据的批量化导入导出

第三步(导入)

HTML代码如下:

<form action="{:U('Index/impUser')}" method="post" enctype="multipart/form-data">
<input type="file" name="import"/>
<input type="submit" value="提交"/>
</form>

控制器层实现如下代码:

function impUser(){
if (!empty($_FILES)) {
//import("@.ORG.UploadFile");
$config=array(
'exts'=>array('xlsx','xls'),
'rootPath'=>"./Public/",
'savePath'=>'Upload/',
//'autoSub' => true,
);
$upload = new \Think\Upload($config); if (!$info=$upload->upload()) {
$this->error($upload->getError());
} /*else {
//$info = $upload->getUploadFileInfo();
}
*/ vendor("PHPExcel.PHPExcel");
$file_name=$upload->rootPath.$info['import']['savepath'].$info['import']['savename'];
// var_dump($file_name);
// exit;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
for($i=4;$i<=$highestRow;$i++)
{
$data['name'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
$data['age'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
$data['school']= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();=$data['jion_time']= gmdate("Y-m-d H:i:s", \PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue()));//其中gmdate函数是对excel表格中的时间进行格式化处理 M('detail_config')->add($data); }
$this->success('导入成功!','index');
}else
{
$this->error("请选择上传的文件");
}
}

第四步(导出)

HTML代码如下:

<a href="{:U('Index/expUser')}">导出数据并生成excel</a>

控制器层实现如下代码:

//统计数据自动化导出EXCEL
public function exportExcel($expTitle,$expCellName,$expTableData){
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $_SESSION['admin']['username'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData); vendor("PHPExcel.PHPExcel"); $objPHPExcel = new \PHPExcel();
$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'); $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
// $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
}
// Miscellaneous glyphs, UTF-8
for($i=0;$i<$dataNum;$i++){
for($j=0;$j<$cellNum;$j++){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
}
} header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
} public function expUser(){//导出Excel
$xlsName = "admin";
$xlsCell = array(
array('id','序列号'),
array('name','姓名'),
array('age','年龄'),
array('school','学校')
);
$xlsModel = M('Index'); $xlsData = $xlsModel->Field('id,name,age,school')->select(); $this->exportExcel($xlsName,$xlsCell,$xlsData); }

以上步骤即可实现数据批量化的导入导出;