通过mysql+PHPExcel实现查询导出
通过php+mysql自建了几个zabbix事件查询的页面,不过后面在web页面上查询完,需要将结果导出到excel 时,感觉不怎么方便,当然可以通过python 实现快速导出到excel 。也可以通过mysql 官方出的插件,直接在excel 里进行SQL查询并返回。不过为了便于在页面上进行一键式的导出,这里通过PHPExcel 模块实现一键式导出 。在操作过程中,遇到最难搞的问题就是,可以正常导入到本机,但通过web导出保存时,打开生成的excel 文件提示文件格式不正确或受损。后来通过将mysql conn 串直接写到该页面时,可以正常导出,后面又分出来,发现conn.php最后的 ” ?> ” 不加,可以正常,加上不成功 。最后也不知道改了什么地方,来回折腾了两天,又可以了。
实现代码如下:
1<?php
2/* code from www.361way.com */
3include 'lib/PHPExcel.php';
4include 'conn.php';
5//include 'lib/PHPExcel/IOFactory.php';
6//include 'lib/PHPExcel/Writer/Excel2007.php';
7$objPHPExcel = new PHPExcel();
8// Set the active Excel worksheet to sheet 0
9$objPHPExcel->setActiveSheetIndex(0);
10$objPHPExcel->getActiveSheet()->setCellValue('A1', '主机名')
11 ->setCellValue('B1', '触发ID')
12 ->setCellValue('C1', '描述信息')
13 ->setCellValue('D1', '时间');
14$sql="select host,triggerid,description,value,time from newevent
15where description like '%磁盘%' and value=1
16and eventid in (select max(eventid) from newevent group by triggerid )";
17$q=$mysqli->query($sql);
18$rowCount=2;
19while($row=$q->fetch_assoc()){
20 //print_r($row);
21 $objPHPExcel->getActiveSheet()->setCellValue('A'.$rowCount, $row['host']);
22 $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['triggerid']);
23 $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['description']);
24 $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $row['time']);
25 $rowCount++;
26}
27//$q->close();
28$q->free();
29$mysqli->close();
30//标题填充
31$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
32$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFill()->getStartColor()->setARGB('00ffff00');
33//Set column widths 设置列宽度
34$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
35$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
36$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
37$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
38/* 直接导出到本地
39// Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
40$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
41// Write the Excel file to filename some_excel_file.xlsx in the current directory
42$objWriter->save('some_excel_file.xlsx');
43*/
44// Redirect output to a client’s web browser (Excel2007) 通过web页面导出
45header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
46header('Content-Disposition: attachment;filename="disk.xlsx"');
47header('Cache-Control: max-age=0');
48// If you're serving to IE 9, then the following may be needed
49header('Cache-Control: max-age=1');
50// If you're serving to IE over SSL, then the following may be needed
51header ('Expires: Mon, 26 Jul 2016 05:00:00 GMT'); // Date in the past
52header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
53header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
54header ('Pragma: public'); // HTTP/1.0
55$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
56$objWriter->save('php://output');
57exit;
58?>
其中conn.php内容如下:
1<?php
2/*
3$link=mysql_connect('10.211.137.173','report','password')or die("数据库连接失败");
4//连接数据库
5mysql_select_db('report',$link);//选择数据库
6mysql_query("set names utf8");//设置编码格式
7*/
8$mysqli = new mysqli('10.211.137.173','report','password','report');
9/* check connection */
10if ($mysqli->connect_errno) {
11 printf("Connect failed: %s\n", $mysqli->connect_error);
12 exit();
13}
14$mysqli->query("SET NAMES 'utf8'");
最后通过URL打开该链接后,会自动下载excel 文件,打开后,格式如下:
写在最后:
PHPExcel 相对重量级,可以设置的项也比较多,如果想使用轻量级的,可以使用 PHP_XLSXWriter 。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/phpexcel/5420.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.