php 导出 生成多个sheet

 1 <?php
 2 ini_set("max_execution_time", 0);
 3 set_time_limit(0);
 4 function get_all_downexcel($fileName) {
 5     include "./PHPExcel/PHPExcel.php";
 6     $conn = get_db();
 7 
 8     //店铺
 9     $sql = "SELECT * FROM shop where `comment_counts` > 0 ";
10     $shop_query = $conn->query($sql);
11     $shop_row = $shop_query->fetchAll();
12     if(empty($shop_row)){
13         echo "没有数据";exit;
14     }
15     $objPHPExcel = new PHPExcel();
16     $objPHPExcel->getProperties()
17         ->setCreator("gaoxing")
18         ->setLastModifiedBy("gaoxing")
19         ->setTitle("Office 2003 XLSX Test Document")
20         ->setSubject("Office 2003 XLSX Test Document")
21         ->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
22         ->setKeywords("office 2003 openxml php")
23         ->setCategory("Test result file");
24     //循环店铺
25     foreach($shop_row as $key => $val){
26         $dianping_sql = 'SELECT * FROM dianping_comment where `dianping_id` = "'.$val['dianping_id'].'"';
27         $dianping_query = $conn->query($dianping_sql);
28         $dianping_row = $dianping_query->fetchAll();
29         if(empty($dianping_row)){
30             continue;
31         }
32         //第一个sheet
33         $objPHPExcel->createSheet();
34         $objActSheet = $objPHPExcel->setActiveSheetIndex($key);
35         //标题
36         $objPHPExcel->getActiveSheet(0)->setCellValue('A1',"昵称");
37         $objPHPExcel->getActiveSheet(0)->setCellValue('B1',"评分");
38         $objPHPExcel->getActiveSheet(0)->setCellValue('C1',"评论");
39         $objPHPExcel->getActiveSheet(0)->setCellValue('D1',"时间");
40 
41         $i = 2;
42         foreach($dianping_row as $dianping_val){
43             //数据
44             $objPHPExcel->getActiveSheet()->setCellValue('A'.($i),$dianping_val['nickname']);
45             $objPHPExcel->getActiveSheet()->setCellValue('B'.($i),$dianping_val['rank']);
46             $objPHPExcel->getActiveSheet()->setCellValue('C'.($i),$dianping_val['content']);
47             $objPHPExcel->getActiveSheet()->setCellValue('D'.($i),$dianping_val['posttime']);
48             $i ++;
49         }
50         //sheet名称
51         $objPHPExcel->getActiveSheet()->setTitle($val['region'].'-'.$val['city'].'-'.$val['name']);
52 
53     }
54     $objWrite = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
55     header('pragma:public');
56     header("Content-Disposition:attachment;filename=$fileName.xls");
57     $objWrite->save('php://output');exit;
58     die;
59 
60 }
61 
62 function get_db(){
63     $servername = 'mysql:host=210.74.14.150:7000;dbname=lvyou';
64     $username = 'wayhood';
65     $password = 'wayhood';
66     $db = new PDO($servername, $username, $password);
67     return $db;
68 }
69 
70 
71 get_all_downexcel($fileName="书店-评论");

 

posted @ 2021-02-22 10:23  没事就更  阅读(714)  评论(0编辑  收藏  举报