首页 > 代码库 > php导出数据excel
php导出数据excel
public function villageinfo(){
set_time_limit(‘0‘);
$villageDb=D("Village");
$houseDb=D("House");
$areaDb=D("FangArea");
$field="villageName,villageId,fangAreaId";
$data=$villageDb->field($field)->select();
foreach($data as $key=>$val){
$a=$areaDb->getAreaName($val[‘fangAreaId‘]);
$data[$key][‘areaname‘]=$a[‘fangAreaName‘];
$b=$houseDb->getHouseNum($val[‘villageId‘],340100);
$data[$key][‘count‘]=$b;
}
$this->getExcel(‘‘,‘‘,$data);
}
private function getExcel($fileName,$headArr,$data){
//导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
require_once THINK_PATH.‘Library/Org/Util/PHPexcel/PHPExcel.php‘;
import("Library.Org.Util.PHPExcel");
import("Library.Org.Util.PHPExcel.Writer.Excel5");
import("Library.Org.Util.PHPExcel.IOFactory.php");
$date = date("Y_m_d",time());
$fileName .= "_{$date}.xls";
//创建PHPExcel对象,注意,不能少了\
$objPHPExcel = new \PHPExcel();
$objProps = $objPHPExcel->getProperties();
//设置表头
$key = ord("A");
//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;
}
php导出数据excel
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。