关于ThinkCMF后台的导入、导出

导入
public function implode_post(){
ini_set('memory_limit','1024M');
        if (!empty($_FILES)) {
$config = array(
'exts' => array('xlsx','xls'),
'maxSize' => 3145728000,
'rootPath' =>"./Data/",
'savePath' => 'upload/Excle/',
'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['file']['savepath'].$info['file']['savename'];//这个地方需要注意,需要dump($info)来看具体的信息
$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表格后缀格式
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();//取得总行数
$highestColumn =$sheet->getHighestColumn(); //取得总列数
//D('pro_info')->execute('truncate table pro_info'); //每次导入清空原有数据
$arr=array();
for ($i = 2; $i <= $highestRow; $i++) {
$data=array();
$data['name']= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
$data['number'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
$data['status']= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
//看这里看这里,这个位置写数据库中的表名
array_push($arr,$data);
$j++;
}
M("grade")->addAll($arr);
            $this->success('导入成功!本次导入数量:'.$j, U('Grade/index'));
        } else {
            $this->error("请选择上传的文件");
        }
}

 

//导出
public function explode_post(){

 

vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel(); //创建PHPExcel实例

 

/*--------------设置表头信息------------------*/
//$objPHPExcel->getActiveSheet()->mergeCells('A1:C2');
//$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'ID');
$objPHPExcel->getActiveSheet(0)
->setCellValue('A1', 'ID')
->setCellValue('B1', 'name')
->setCellValue('C1', 'number')
->setCellValue('D1', 'status');
 
/*--------------开始从数据库提取信息插入Excel表中------------------*/
$data=M('grade')->select();
foreach($data as $k=>$v){
$key = $k + 2;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A".$key, $v['id'])
->setCellValue("B".$key, $v['name'])
->setCellValue("C".$key, $v['number'])
->setCellValue("D".$key, $v['status']);
}
$objPHPExcel->getActiveSheet()->setTitle('成功'); //设置sheet的名称

 

/*--------------下面是设置保存路径------------------*/
date_default_timezone_set("Asia/Shanghai");
$date = date("YmdHi");
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$date.'.xls"');
header("Content-Disposition:attachment;filename=$date.xls");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//Excel5为xls格式,excel2007为xlsx格式
$objWriter->save('php://output');//这句话必须要加,否侧会报错
exit;
}
posted @ 2018-09-07 15:10  攀登的蜗牛  阅读(1836)  评论(1编辑  收藏  举报