导入代码,更新操作 if ( $_FILES["inputExcel"]["type"] == "application/vnd.ms-excel" ){ $inputFileType = 'Excel5'; } elseif ( $_FILES["inputExcel"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ){ $inputFileType = 'Excel2007'; } else { echo "Type: " . $_FILES["inputExcel"]["type"] . "<br />"; echo "Invalid file type"; exit(); } if ($_FILES["inputExcel"]["error"] > 0) { echo "Error: " . $_FILES["inputExcel"]["error"] . "<br />"; exit(); } $inputFileName =$_G['setting']['attachdir'].'temp/'.$upload->attach['attachment']; //导入phpExcel include_once 'Classes/PHPExcel/IOFactory.php'; //设置php服务器可用内存,上传较大文件时可能会用到 ini_set('memory_limit', '1024M'); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $WorksheetInfo = $objReader->listWorksheetInfo($inputFileName); //读取文件最大行数、列数,偶尔会用到。 $maxRows = $WorksheetInfo[0]['totalRows']; $maxColumn = $WorksheetInfo[0]['totalColumns']; //列数可用于粗略判断所上传文件是否符合模板要求 //设置只读,可取消类似"3.08E-05"之类自动转换的数据格式,避免写库失败 $objReader->setReadDataOnly(true); $objPHPExcel = $objReader->load($inputFileName); $sheetData = $objPHPExcel->getSheet(0)->toArray(null,true,true,true); //excel2003文件,可使用'$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);' //excel2007文件使用"getActiveSheet()"方法时会提示出错:对non-object使用了"toArray"方法。 //才疏学浅,原因未明。。。 //获取表头,并判断是否符合格式 $keywords = $sheetData[1]; /*$warning = '上传文件格式不正确,请修改后重新上传!<br />'; $columns = array ( 'A', 'C', 'D', 'E', 'F', 'G', 'H','I','K','L','M','N' ); $keysInFile= array ( '物流公司', '地址(收件人)', '电话(座机)', '邮编', '收件人', '手机号','套数','内件品名','寄件人地址','快递单号','序号id' ); foreach( $columns as $keyIndex => $columnIndex ){ if ( $keywords[$columnIndex] != $keysInFile[$keyIndex] ){ echo $warning . $columnIndex . '列应为' . $keysInFile[$keyIndex] . ',而非' . $keywords[$columnIndex]; exit(); } }*/ $number=0; foreach ( $sheetData as $key => $words ){ if ( $key != 1&&$words['A']&&$words['N']){ //忽略表头 $update=array(); $update['company']=$words['A']; $update['code']=$words['M']; $update['orderstatus']=1; $where['applyid']=$words['N']; $insert_id=DB::update('plugin_auctionapply',$update,$where); if($insert_id){ $number++; } } } 导出代码: require_once 'Classes/PHPExcel.php'; require_once 'Classes/phpExcel/Writer/Excel2007.php'; require_once 'Classes/PHPExcel/Reader/Excel5.php'; include_once 'Classes/PHPExcel/IOFactory.php'; $filename='transport.xlsx'; $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setLastModifiedBy("Maarten Balliauw")->setTitle("PHPExcel Test Document")->setSubject("PHPExcel Test Document")->setDescription("Test document for PHPExcel, generated using PHP classes.")->setCategory("kingdee result file"); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setVisible(false); $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setVisible(false); $objRichText = new PHPExcel_RichText(); $objPayable = $objRichText->createTextRun('物流模板信息'); $objPayable->getFont()->setBold(true); $objPayable->getFont()->setSize(30); $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) ); $objPHPExcel->getActiveSheet()->getCell('A1')->setValue($objRichText); //$objPHPExcel->getActiveSheet()->mergeCells('A1:B1'); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '物流公司')->setCellValue('C1', '地址(收件人)') ->setCellValue('D1', '电话(座机)')->setCellValue('E1', '邮编')->setCellValue('F1', '收件人') ->setCellValue('G1', '手机号')->setCellValue('H1', '套数')->setCellValue('I1', '内件品名') ->setCellValue('K1','寄件人地址')->setCellValue('L1','寄件人电话')->setCellValue('M1','快递单号') ->setCellValue('N1','序号id'); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->setActiveSheetIndex(0); $callStartTime = microtime(true); $objPHPExcel->createSheet(); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $sql=''; $query = DB::query($sql); $i =1; while($row = DB::fetch($query)) { $i++; $objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('D'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('E'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('K'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('K'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('L'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('L'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('M'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('M'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('N'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); $objPHPExcel->getActiveSheet()->getStyle('N'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('C'.$i,$row['province'].$row['city'].$row['area'].$row['community'].$row['address']) ->setCellValue('D'.$i, $rowp['mobile']) ->setCellValue('E'.$i, $row['postcode']) ->setCellValue('F'.$i, $row['customer']) ->setCellValue('G'.$i, $row['mobile']) ->setCellValue('H'.$i, '') ->setCellValue('I'.$i,$row['goodsname']?$row['goodsname']:$row['goodsname1']) ->setCellValue('K'.$i, '000') ->setCellValue('L'.$i, '') ->setCellValue('M'.$i,'') ->setCellValue('N'.$i,$row['applyid']); } if(file_exists('transport.xlsx')) { unlink('transport.xlsx'); } //header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //header("Content-Disposition: attachment; filename=\"$filename\""); //header('Cache-Control: max-age=0'); $objWriter->save('transport.xlsx'); ob_flush();