加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php生成Excel文件 实现代码介绍

发布时间:2022-07-26 22:02:37 所属栏目:PHP教程 来源:互联网
导读:p有段日子没有更新博客了,生怕被百度遗忘啊,biu~biu~.最近有个项目需要统计网站的url和title,保存在excel里面,下面是具体的代码/p pre class=php name=code!--p //php生成excel报表,是通过发送header()头信息完成的 header(Content-Type: application/vnd.
   <p>有段日子没有更新博客了,生怕被百度遗忘啊,biu~biu~.最近有个项目需要统计网站的url和title,保存在excel里面,下面是具体的代码</p>
 
  <pre class="php" name="code"><!--p
 
  //php生成excel报表,是通过发送header()头信息完成的
 
  header("Content-Type: application/vnd.ms-execl");
 
  header("Content-Type: application/vnd.ms-excel; charset=gb2312");
 
  // 用 HTML 显示结果
 
  //$str =mb_convert_encoding ("测试1","gb2312","utf-8");
 
  echo "Title/t";
 
  echo "Keywords/t";
 
  echo "Description/t";
 
  echo "URL/t/n";

  //告知浏览器文件名称,并要求客户端下载
 
  header("Content-Disposition:filename=test.xls");
 
  header("Pragma: no-cache");
 
  header("Expires: 0");
 
  $link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
 
  mysql_select_db('novartis') or die('Could not select database');
 
  mysql_query("SET NAMES gb2312");
 
  $query = 'SELECT title,keywords,url,description FROM cms_content';
 
  $result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
  /* 格式: 换行:"/t/n": 单元格之间: "/t" */
 
  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
 
  echo $line['title']."/t";
 
  echo $line['keywords']."/t";
 
  echo $line['description']."/t";
 
  echo $line['url']."/t/n";
 
  }
 
  // 释放结果集
 
  mysql_free_result($result);
 
  // 关闭连接
 
  mysql_close($link);
 
  -->
 
  </pre>
 
  <div> </div>。
 

(编辑:济源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读