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 echo '11 <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 echo '24 <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 echo '40 </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`) VALUES13 (1, 'a', '小张'),14 (2, 'a', '小孙'),15 (3, 'b', '小王'),16 (4, 'c', '小李'),17 (5, 'a', '小红'),18 (6, 'b', '小赵');
関数実装プレビュー: