php实现导出数据分类合并单元格功能

 1 <?php
 2     $conn = mysql_connect("localhost","root","root");
 3     $db = mysql_select_db("imexcel",$conn);
 4     mysql_query("set names ‘gbk‘");
 5 
 6     $query = "SELECT danwei,count(danwei) as num ,group_concat(fuze SEPARATOR ‘,‘) as gw FROM `dy` group by danwei";
 7     $result = mysql_query($query);
 8     header("Content-type:application/vnd.ms-excel;charset=GBK");
 9     header("Content-Disposition:filename=test.xls");
10     echo11         <table border="1">
12             <caption class="text-center">信息统计表</caption>
13                <thead>
14                     <tr >
15                         <th>单位</th>
16                         <th>负责人</th>
17                     </tr>
18                </thead>
19               <tbody>
20;
21                while($row = mysql_fetch_array($result)) {
22                    $gw = explode(",",$row[‘gw‘]);
23                    echo24                    <tr>
25                        <td rowspan="‘.$row[‘num‘].‘">‘.$row[‘danwei‘].‘</td>
26                        <td>‘.iconv("gbk","utf-8",$gw[0]).‘</td>
27                    </tr>
28;
29                     if($row[‘num‘] > 1){
30                         for($i=1;$i<$row[‘num‘];$i++){
31                             echo ‘<tr><td>‘.iconv("gbk","utf-8",$gw[$i]).‘</td></tr>‘;
32                         }
33                     }else{
34 
35                     }
36 
37                }
38 
39 echo40             </tbody>
41         </table>
42;
43 ?>
 1 CREATE TABLE IF NOT EXISTS `dy` (
 2   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 3   `danwei` varchar(30) NOT NULL,
 4   `fuze` varchar(30) NOT NULL,
 5   PRIMARY KEY (`id`)
 6 ) ENGINE=MyISAM  DEFAULT CHARSET=gbk AUTO_INCREMENT=7 ;
 7 
 8 --
 9 -- 转存表中的数据 `dy`
10 --
11 
12 INSERT INTO `dy` (`id`, `danwei`, `fuze`) VALUES
13 (1, a, 小张),
14 (2, a, 小孙),
15 (3, b, 小王),
16 (4, c, 小李),
17 (5, a, 小红),
18 (6, b, 小赵);

 功能实现预览:

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。