PHPEXCEL生成excel文件与导出excel文件
<?php Header("Content-type:text/html;charset=utf-8"); error_reporting(E_ALL & ~E_NOTICE); require_once(ESF_DIR.‘/zjadmin/phpexcel/Classes/PHPExcel.php‘); $db->query("set names utf8"); $Info_area=array(2=>"x山区",1=>"x河区",3=>"x阳区",4=>"x海区"); $m = isset($_GET[‘m‘])?$_GET[‘m‘]:0; if($m == 1) { $reader = PHPExcel_IOFactory::createReader(‘Excel5‘); // 读取 excel 文件方式 此方法是读取excel2007之前的版本 excel2007 为读取2007以后的版本 也可以查\Classes\PHPExcel\Reader 文件夹中的类(为所有读取类,需要哪个填上哪个就行) $PHPExcel = $reader->load("hf_lp.xls"); // 文件名称 $sheet = $PHPExcel->getSheet(0); // 读取第一个工作表从0读起 $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumn = $sheet->getHighestColumn(); // 取得总列数 $arr = array(1=>‘A‘,2=>‘B‘,3=>‘C‘); for ($row = 2; $row <= $highestRow; $row++) { for ($column = 0; $column<count($arr); $column++) { $val = $sheet->getCellByColumnAndRow($column, $row)->getValue(); $list[$row][] = $val; } $str = ‘‘; foreach ($list[$row] as $key => $value) { if($key === 0) $str .= ‘‘.array_search($value,$Info_area).‘,‘; else $str .= ‘\‘‘.$value.‘\‘,‘; } $field = rtrim($str,‘,‘); echo $field; mysql_query("insert into temp_table(area,name,address) values(".$field.")"); echo "<hr>"; unset($list); } } else { $sql_temp = mysql_query("select * from temp_table"); $i=1; $im = array(); while ($row = mysql_fetch_array($sql_temp)) { $sql = mysql_query("SELECT did,areaid,buildname,address from dictionary WHERE areaid=".$row[‘area‘]." AND buildname=‘".$row[‘name‘]."‘ "); if(mysql_num_rows($sql)<1) { $i++; $im[] = array($Info_area[$row[‘area‘]],$row[‘name‘],$row[‘address‘]); } } echo $i++;
include_once(ESF_DIR.‘/zjadmin/phpexcel/Classes/PHPExcel/Writer/Excel2007.php‘);
include_once(ESF_DIR.‘/zjadmin/phpexcel/Classes/PHPExcel/Writer/Excel5.php‘);
include_once(ESF_DIR.‘/zjadmin/phpexcel/Classes/PHPExcel/IOFactory.php‘);
$fileName = "test_excel";
$headArr = array("区域","名称","地址");
getExcel($fileName,$headArr,$im);
}
function getExcel($fileName,$headArr,$data){
if(empty($data) || !is_array($data)){
die("data must be a array");
}
if(empty($fileName)){
exit;
}
$date = date("Y_m_d",time());
$fileName .= "_{$date}.xls";
//创建新的PHPExcel对象
$objPHPExcel = new PHPExcel();
$objProps = $objPHPExcel->getProperties();
//设置表头
$key = ord("A");
foreach($headArr as $v){
$colum = chr($key);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.‘1‘, $v);
$key += 1;
}
$column = 2;
$objActSheet = $objPHPExcel->getActiveSheet();
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(‘Simple‘);
$objPHPExcel->setActiveSheetIndex(0);
header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘);
header("Content-Disposition: attachment; filename=\"$fileName\"");
header(‘Cache-Control: max-age=0‘);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007‘);
$objWriter->save(‘php://output‘); //文件通过浏览器下载
exit;}
导出来源:http://www.oschina.net/code/snippet_112754_15765
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。