php导出excel数据
php导出excel数据
提供两种导出excel方法1 最简单的导出excel
header('Content-Type: application/vnd.ms-excel'); //设置文件类型 也可以将 vnd.ms-excel' 改成xml(导出xml文件)
header('Content-Disposition: attachment;filename="云平台用户报表.xls"'); //设置导出的excel的名字
header('Cache-Control: max-age=0');
echo iconv("utf-8","gbk","时间\t错误码\t出现次数\t错误码含义\n"); // \t是制表符 \n是换行符
foreach ($arr as $key=>$val){ //$arr 是所要导出的数据
echo iconv("utf-8","gbk",date("Y-m-d",$val["time"])."\t".$val["error_code"]."\t".$val["num"]."\t".$val["code_mean"]."\n");
}
2.使用phpexcel导出excel
到网上下载phpexcel压缩包 将phpexcel.php 和phpexcel压缩包放入你的项目中
实例:
require_once ('PHPExcel.php'); //引用这两个文件
require_once ('PHPExcel/Writer/Excel2007.php');
//导出excel
$objExcel = new PHPExcel();
$objProps = $objExcel->getProperties();
$objProps->setCreator("Zeal Li");
$objProps->setLastModifiedBy("Zeal Li");
$objProps->setTitle("Office XLS Test Document");
$objProps->setSubject("Office XLS Test Document, Demo");
$objProps->setDescription("Test document, generated by PHPExcel.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("Test");
$objProps = $objExcel->getProperties();
$objProps->setCreator("Zeal Li");
$objProps->setLastModifiedBy("Zeal Li");
$objProps->setTitle("Office XLS Test Document");
$objProps->setSubject("Office XLS Test Document, Demo");
$objProps->setDescription("Test document, generated by PHPExcel.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("Test");
//*************************************
//设置当前的sheet索引,用于后续的内容操作。
//一般只有在使用多个sheet的时候才需要显示调用。
//缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
//网游类
//设置当前活动sheet的名称
$objActSheet->setTitle('网游类');
$objActSheet->setCellValue('A1','游戏名称'); //生成的excel中第一个sheet里面的第一行数据
$objActSheet->setCellValue('B1','具体域名');
$objActSheet->setCellValue('C1','服务器描述');
$objActSheet->setCellValue('D1','线路属性');
$objActSheet->setCellValue('E1','提交的服务器IP');
$objActSheet->setCellValue('F1','对应优化路由条目');
$objActSheet->setCellValue('G1','关键字优化');
$objActSheet->setCellValue('H1','优化线路');
$objActSheet->setCellValue('I1','优化状态');
foreach($result_webgame as $k1=>$v1){ //遍历从数据库中读取的数据
$objActSheet->setCellValue('A'.($k1+2),$v1["gamename"]);
$objActSheet->setCellValue('B'.($k1+2),$v1['domain']);
$objActSheet->setCellValue('C'.($k1+2),$v1['gameserver']);
$objActSheet->setCellValue('D'.($k1+2),$v1['line_attribute']);
$objActSheet->setCellValue('E'.($k1+2),$v1['server_ip']);
$objActSheet->setCellValue('F'.($k1+2),$v1['optimize_route']);
$objActSheet->setCellValue('G'.($k1+2),$v1['keyword']);
$objActSheet->setCellValue('H'.($k1+2),$v1['line']);
$objActSheet->setCellValue('I'.($k1+2),$v1['optimize_status']);
}
//网站类
//添加一个新的worksheet
$objExcel->createSheet();
$reource_ready=$objExcel->getSheet(1)->setTitle('网站类');
$reource_ready->setCellValue('A1','网站名称');
$reource_ready->setCellValue('B1','具体域名');
$reource_ready->setCellValue('C1','线路属性');
$reource_ready->setCellValue('D1','IP网段');
$reource_ready->setCellValue('E1','优化线路');
$reource_ready->setCellValue('F1','优化状态');
foreach($result_website as $k1=>$v2){
$reource_ready->setCellValue('A'.($k1+2),$v2["name"]);
$reource_ready->setCellValue('B'.($k1+2),$v2['domain']);
$reource_ready->setCellValue('C'.($k1+2),$v2['line_attribute']);
$reource_ready->setCellValue('D'.($k1+2),$v2['ip']);
$reource_ready->setCellValue('E'.($k1+2),$v2['line']);
$reource_ready->setCellValue('F'.($k1+2),$v2['optimize_status']);
}
//以此类推 可以导出含有多个sheet的excel