<?php error_reporting(E_ALL &~ E_WARNING &~ E_NOTICE); date_default_timezone_set('Europe/London'); require_once './Classes/PHPExcel/IOFactory.php'; $filePath = './SR_Template.xlsx'; //读取文件 if (!file_exists($filePath)) { exit("you dont have "); }else{ } $connection = @mysql_connect("localhost:3306","root","111111"); mysql_select_db("sr_template", $connection); if (!$connection) { die('Could not connect: ' . mysql_error()); } mysql_close($con); $objPHPExcel = PHPExcel_IOFactory::load($filePath); $sheet = $objPHPExcel->getSheet(0); // 读取第一個工作表 $highestColumm = $sheet->getHighestColumn(); // 取得总列数 $highestRow = $sheet->getHighestRow(); // 取得总行数 $exlCASArr = array(); /** 循环读取每个单元格的数据 */ for ($row =2; $row <= $highestRow; $row++){//行数是以第1行开始 $ex = $sheet->getCell('E'.$row)->getValue(); $sql = 'select a,b from table where ex='.$ex;//和数据 $resultInfo = mysql_query($sql,$connection); $casInfo = mysql_fetch_array($resultInfo); //这里这样写有报错 /* $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('G'.$row, $casInfo['a']); ->setCellValue('H'.$row, $casInfo['b']); */ $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('G'.$row, $casInfo['a']);//修改数据 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('H'.$row, $casInfo['b']); } // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: p_w_upload;filename="01simple.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('文件路径\\01simple.xlsx'); exit; ?>
转载于:https://blog.51cto.com/liuzan/1338985