//找出某个字段名对应的excel表头位置
private function getColumnChr($num = 0){
$v = chr(ord('A') + intval($num));
return $v;
}
//获取上传的excel,将导入相关的内容
//获取上传的excel,将导入相关的内容
public function upload() {
//原创文章,来源:www.aspbc.com,转载请注明来源
ini_set('memory_limit','1024M');
if (!empty($_FILES)) {
$config = array(
'exts' => array('xlsx','xls','csv' ),
'maxSize' => 3145728000,
'rootPath' =>"./Public/",
'savePath' => 'Uploads/',
'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['files']['savepath'].$info['files']['savename']; //files为表单name
$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表格后缀格式
switch ($extension){
case "xlsx":{
$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
break;
}
case "xls":{
$objReader =\PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
break;
}
case "csv":{
$PHPReader = \PHPExcel_IOFactory::createReader('csv');
//默认输入字符集
$PHPReader->setInputEncoding('gb2312');
//默认的分隔符
$PHPReader->setDelimiter(',');
//载入文件
$objPHPExcel = $PHPReader->load($file_name);
}
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();//取得总行数
$highestColumn =$sheet->getHighestColumn(); //取得总列数
$rowData = $sheet->rangeToArray('A1:' . $highestColumn . 1, NULL, TRUE, FALSE);
$crr = array();
foreach($rowData[0] as $key => $val){
$crr[$val] = $key; //找出表头对应的位置
} $arr_status = array();
$arr_status['处理完成'] = 3;
$arr_status['处理中'] = 1;
$arr_status['未处理'] = 0;
for ($i = 2; $i <= $highestRow; $i++) {
//看这里看这里,前面小写的a是表中的字段名,后面的大写A是excel中位置
$k_no = trim($objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['运单号']) . $i)->getValue());
$catid_str = trim($objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['问题类型']) .$i)->getValue());
if($k_no != '' && $catid_str != ""){
$data['k_no'] = $k_no;
$data['cat_id'] = $catid_str;
$status = trim($objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['状态']).$i)->getValue());
$data['k_status'] = $status;
$data['k_qustion'] = $objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['问题描述']). $i)->getValue();
$data['k_time'] = strtotime($objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['上报时间']). $i)->getValue());
$data['k_custom'] = $objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['物料客户']). $i)->getValue();
$data['k_organization'] = $objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['接收组织']). $i)->getValue();
$data['k_pjy'] = $objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['派件员']). $i)->getValue();
$data['k_sbr'] = $objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['上报人']). $i)->getValue();
$data['k_gjr'] = $objPHPExcel->getActiveSheet()->getCell($this->getColumnChr($crr['跟进人']). $i)->getValue();
$data['addtime'] = time();
//写入数据库中
D('tablesname')->tableSave($data);
unset($catid_str,$data,$status,$k_no);
}
}
$this->success('导入成功!');
}
else {
$this->error("请选择上传的文件");
}
}
到此,完成上传和导入功能。
【1】 2