/* thinkphp导入excel */ function getExcel($fileName,$headArr,$data){ //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入 import("Org.Util.PHPExcel"); import("Org.Util.PHPExcel.Writer.Excel5"); import("Org.Util.PHPExcel.IOFactory.php"); //$date = date("Y_m_d",time()); $fileName .= ".xls"; //创建PHPExcel对象,注意,不能少了\ $objPHPExcel = new \PHPExcel(); $objProps = $objPHPExcel->getProperties(); //设置表头 $key = ord("A"); $objPHPExcel->getActiveSheet()->getStyle('A')->getFont()->setName('宋体'); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); //print_r($headArr);exit; foreach($headArr as $v){ $colum = chr($key); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v); $key += 1; } $column = 2; $objActSheet = $objPHPExcel->getActiveSheet(); //print_r($data);exit; foreach($data as $key => $rows){ //行写入 $span = ord("A"); foreach($rows as $keyName=>$value){ // 列写入 $j = chr($span); $objActSheet->setCellValue($j.$column, $value); $span++; } $column++; } $fileName = iconv("utf-8", "gb2312", $fileName); //重命名表 //$objPHPExcel->getActiveSheet()->setTitle('test'); //设置活动单指数到第一个表,所以Excel打开这是第一个表 $objPHPExcel->setActiveSheetIndex(0); ob_end_clean();//清除缓冲区,避免乱码 header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: attachment;filename=\"$fileName\""); header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); //文件通过浏览器下载 exit; } //导入数据方法 protected function goods_import($filename, $exts='xls') { //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入 import("Org.Util.PHPExcel"); //创建PHPExcel对象,注意,不能少了\ $PHPExcel=new \PHPExcel(); //如果excel文件后缀名为.xls,导入这个类 if($exts == 'xls'){ import("Org.Util.PHPExcel.Reader.Excel5"); $PHPReader=new \PHPExcel_Reader_Excel5(); }else if($exts == 'xlsx'){ import("Org.Util.PHPExcel.Reader.Excel2007"); $PHPReader=new \PHPExcel_Reader_Excel2007(); } //载入文件 $PHPExcel=$PHPReader->load($filename); //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推 $currentSheet=$PHPExcel->getSheet(0); //获取总列数 $allColumn=$currentSheet->getHighestColumn(); //获取总行数 $allRow=$currentSheet->getHighestRow(); //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始 for($currentRow=2;$currentRow<=$allRow;$currentRow++){ //从哪列开始,A表示第一列 for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){ //数据坐标 $address=$currentColumn.$currentRow; //读取到的数据,保存到数组$arr中 $cell =$currentSheet->getCell($address)->getValue(); if (preg_match('/^([$[A-Z]*-[0-9A-F]*])*[hmsdy]/i', $cell)) { $cell=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value)); } 这段是重点 日期格式化。如果excel里面不需要日期上传,可以把这段去掉 //$cell = $data[$currentRow][$currentColumn]; if($cell instanceof PHPExcel_RichText){ $cell = $cell->__toString(); } $data[$currentRow][] = $cell; } } //$this->save_import($data); return $data; }
以上代码我是放在 CommonController.class.php 控制器里面的。
文件导入控制器如下:
/年度爱心名录class BenevolenceController extends CommonController{ function import(){ if(IS_POST){ $file = $_FILES['upfile']; $suffix = pathinfo($file['name'], PATHINFO_EXTENSION); if(!($suffix =='xls' || $suffix == 'xlsx')){ $this->error('请选择Excel文件'); } $data = $this->goods_import($file['tmp_name'],'xlsx'); /* import("Org.Util.PHPExcel.Shared.Date"); $shared = new \PHPExcel_Shared_Date();*/ // dump($data);die; if(!empty($data)){ foreach ($data as $key => $value) { $tmp = array(); $tmp['name'] = $value[0]; $tmp['phone'] = $value[1]; $tmp['address'] = $value[2]; $tmp['email'] = $value[3]; $tmp['use'] = $value[4]; $tmp['money'] = $value[5]; $tmp['updatetime']=$value[6]; $tmp['is_show'] = 1; $tmp['status'] = 1; $tmp['create_time'] = time(); // echo ""; // print_r($tmp);die; $rr=M('benevolence')->add($tmp); } $this->success('导入成功',U('index')); } }else{ $this->display(); } }}
注意 goods_import 这个类。