0

0

php导出excel表格综合示例

php中文网

php中文网

发布时间:2016-07-25 08:55:07

|

1339人浏览过

|

来源于php中文网

原创

  1. include("config.php");

  2. // load library
  3. require 'php-excel.class.php';
  4. // create a simple 2-dimensional array
  5. //@mysql_query("set character set gb2312");
  6. $strsql="select * from booklist";
  7. $result=mysql_query($strsql);
  8. //print_r($result);

  9. $data = array(

  10. 1 => array ('id', "书名"),//标题使用中文则到表格里面值为空,待解决
  11. );
  12. while($row=mysql_fetch_array($result))
  13. {
  14. array_push($data,array($row['bookid'], $row['bookname']));
  15. }
  16. // generate file (constructor parameters are optional)
  17. $xls = new Excel_XML('UTF-8', false, 'My Test Sheet');
  18. $xls->addArray($data);
  19. $xls->generateXML('my-test');
  20. ?>
  21. /**
  22. * Simple excel generating from PHP5
  23. *
  24. * @version 1.0
  25. */
  26. /**

    Groq
    Groq

    GroqChat是一个全新的AI聊天机器人平台,支持多种大模型语言,可以免费在线使用。

    下载
  27. * Generating excel documents on-the-fly from PHP5
  28. *
  29. * Uses the excel XML-specification to generate a native
  30. * XML document, readable/processable by excel.
  31. *
  32. * @package Utilities
  33. * @version 1.1
  34. *
  35. * @todo Issue #4: Internet Explorer 7 does not work well with the given header
  36. * @todo Add option to give out first line as header (bold text)
  37. * @todo Add option to give out last line as footer (bold text)
  38. * @todo Add option to write to file
  39. */
  40. class Excel_XML
  41. {

    立即学习PHP免费学习笔记(深入)”;

  42. /**

  43. * Header (of document)
  44. * @var string
  45. */
  46. private $header = "\n";
  47. /**

  48. * Footer (of document)
  49. * @var string
  50. */
  51. private $footer = "
  52. ";
  53. /**

  54. * Lines to output in the excel document
  55. * @var array
  56. */
  57. private $lines = array();
  58. /**

  59. * Used encoding
  60. * @var string
  61. */
  62. private $sEncoding;
  63. /**

  64. * Convert variable types
  65. * @var boolean
  66. */
  67. private $bConvertTypes;
  68. /**

  69. * Worksheet title
  70. * @var string
  71. */
  72. private $sWorksheetTitle;
  73. /**

  74. * Constructor
  75. *
  76. * The constructor allows the setting of some additional
  77. * parameters so that the library may be configured to
  78. * one's needs.
  79. *
  80. * On converting types:
  81. * When set to true, the library tries to identify the type of
  82. * the variable value and set the field specification for Excel
  83. * accordingly. Be careful with article numbers or postcodes
  84. * starting with a '0' (zero)!
  85. *
  86. * @param string $sEncoding Encoding to be used (defaults to UTF-8)
  87. * @param boolean $bConvertTypes Convert variables to field specification
  88. * @param string $sWorksheetTitle Title for the worksheet
  89. */
  90. public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
  91. {
  92. $this->bConvertTypes = $bConvertTypes;
  93. $this->setEncoding($sEncoding);
  94. $this->setWorksheetTitle($sWorksheetTitle);
  95. }
  96. /**

  97. * Set encoding
  98. * @param string Encoding type to set
  99. */
  100. public function setEncoding($sEncoding)
  101. {
  102. $this->sEncoding = $sEncoding;
  103. }
  104. /**

  105. * Set worksheet title
  106. *
  107. * Strips out not allowed characters and trims the
  108. * title to a maximum length of 31.
  109. *
  110. * @param string $title Title for worksheet
  111. */
  112. public function setWorksheetTitle ($title)
  113. {
  114. $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
  115. $title = substr ($title, 0, 31);
  116. $this->sWorksheetTitle = $title;
  117. }
  118. /**

  119. * Add row
  120. *
  121. * Adds a single row to the document. If set to true, self::bConvertTypes
  122. * checks the type of variable and returns the specific field settings
  123. * for the cell.
  124. *
  125. * @param array $array One-dimensional array with row content
  126. */
  127. private function addRow ($array)
  128. {
  129. $cells = "";
  130. foreach ($array as $k => $v):
  131. $type = 'String';
  132. if ($this->bConvertTypes === true && is_numeric($v)):
  133. $type = 'Number';
  134. endif;
  135. $v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
  136. $cells .= "" . $v . "\n";
  137. endforeach;
  138. $this->lines[] = "\n" . $cells . "\n";
  139. }
  140. /**

  141. * Add an array to the document
  142. * @param array 2-dimensional array
  143. */
  144. public function addArray ($array)
  145. {
  146. foreach ($array as $k => $v)
  147. $this->addRow ($v);
  148. }
  149. /**
  150. * Generate the excel file
  151. * @param string $filename Name of excel file to generate (...xls)
  152. */
  153. public function generateXML ($filename = 'excel-export')
  154. {
  155. // correct/validate filename
  156. $filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);
  157. // deliver header (as recommended in php manual)

  158. header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
  159. header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
  160. // print out document to the browser

  161. // need to use stripslashes for the damn ">"
  162. echo stripslashes (sprintf($this->header, $this->sEncoding));
  163. echo "\nsWorksheetTitle . "\">\n\n";
  164. foreach ($this->lines as $line)
  165. echo $line;
  166. echo "

  167. \n
    \n";
  168. echo $this->footer;
  169. }
  170. }
  171. ?>
复制代码


相关文章

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

65

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

43

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

35

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

41

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

204

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

9

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

8

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号