php将数据导出为execl文件格式
php将数据导出为execl文件格式br /
php将数据导出为execl文件格式
<?php session_start(); $sql_excel=""; if(isset($_SESSION["excel"])) { $sql_excel=$_SESSION["excel"]; } //unset($_SESSION["excel"]); $conn=mysql_connect("localhost","root",""); mysql_select_db("test"); mysql_query("set names utf8",$conn); $result=mysql_query($sql_excel); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=export_data.xls"); //$result=mysql_query($sql_excel); $result=mysql_query($sql_excel); while($arr = mysql_fetch_assoc($result)){ $content[]=$arr; } // print_r($content); $table = '<table>'; $table .= '<thead><tr><th>id</th><th>标题</th><th>电话</th><th>地址</th><th>审核状态</th><th>内容</th><th>留言时间</th></thead>'; $table .= '<tbody>'; foreach($content as $key=>$value){ $table .= "<tr>"; $table .= "<td>".$value['id'].'</td>'; $table .= "<td>".$value['title'].'</td>'; $table .= "<td>".$value['phone'].'</td>'; $table .= "<td>".$value['address'].'</td>'; $table .= "<td>".$value['ischeck'].'</td>'; $table .= "<td>".$value['content'].'</td>'; $table .= "<td>".substr($value['datetime'],0,11).'</td>'; $table .= '</tr>'; } $table .= '</tbody>'; $table .= '</table>'; echo $table; ?>
3. [代码][PHP]代码 跳至 [1] [3] [全屏预览]
<?php session_start(); $link= mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error()); mysql_query("set names utf8",$link); mysql_select_db("test"); $sqls = "select * from news order by id desc "; $_SESSION["excel"]=$sqls; $rs1 = mysql_query($sqls); while($arr = mysql_fetch_assoc($rs1)){ $content[]=$arr; } echo '<table style="margin-top:20px;width:80%">'; echo '<caption > 视频列表<span style="color:#FF0000; margin-left:6px;">(index:在首页显示 lanmu:在视频页显示)</span> </caption>'; echo '<thead>'; echo '<tr>'; echo '<th>序号</th>'; echo '<th>用户uid</th>'; echo '<th>显示位置</th>'; echo '<th>文件名</th>'; echo '<th>视频名</th>'; echo '<th>视频介绍</th>'; echo '<th>上传时间</th>'; echo '</tr>'; echo '</thead>'; echo '<tbody class="">'; foreach ($content as $k => $v){ echo "<tr onMouseOver=this.style.backgroundColor='#cccccc' onMouseOut=this.style.backgroundColor='#FFFFFF'>"; echo "<td>$k</td>"; echo "<td>{$v['title']}</td>"; echo "<td>{$v['phone']}</td>"; echo "<td>{$v['address']}</td>"; echo "<td>{$v['ischeck']}</td>"; echo "<td>{$v['content']}</td>"; echo "<td>{$v['datetime']}</td>"; echo "</tr>"; } echo '<input name="button" type="button" value="导出数据" onclick="excel()" />'; echo '</tbody>'; echo '</table>'; //var_dump($content); mysql_free_result($rs1); mysql_close($link); ?> <script type="text/javascript"> function excel() { window.location.href="download_excel.php"; } </script>
- 上一篇:文件上传
- 下一篇:php在线解压zip压缩文件
精彩图集
精彩文章