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. /**

    通义万相
    通义万相

    通义万相,一个不断进化的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 = " ";
  47. /**

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

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

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

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

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

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

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

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

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

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

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

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

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


相关文章

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

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

下载

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Golang 生态工具与框架:扩展开发能力
Golang 生态工具与框架:扩展开发能力

《Golang 生态工具与框架》系统梳理 Go 语言在实际工程中的主流工具链与框架选型思路,涵盖 Web 框架、RPC 通信、依赖管理、测试工具、代码生成与项目结构设计等内容。通过真实项目场景解析不同工具的适用边界与组合方式,帮助开发者构建高效、可维护的 Go 工程体系,并提升团队协作与交付效率。

1

2026.02.24

Golang 性能优化专题:提升应用效率
Golang 性能优化专题:提升应用效率

《Golang 性能优化专题》聚焦 Go 应用在高并发与大规模服务中的性能问题,从 profiling、内存分配、Goroutine 调度、GC 机制到 I/O 与锁竞争逐层分析。结合真实案例讲解定位瓶颈的方法与优化策略,帮助开发者建立系统化性能调优思维,在保证代码可维护性的同时显著提升服务吞吐与稳定性。

2

2026.02.24

Golang 面试题精选:高频问题与解答
Golang 面试题精选:高频问题与解答

Golang 面试题精选》系统整理企业常见 Go 技术面试问题,覆盖语言基础、并发模型、内存与调度机制、网络编程、工程实践与性能优化等核心知识点。每道题不仅给出答案,还拆解背后的设计原理与考察思路,帮助读者建立完整知识结构,在面试与实际开发中都能更从容应对复杂问题。

1

2026.02.24

Golang 运行与部署实战:从本地到云端
Golang 运行与部署实战:从本地到云端

《Golang 运行与部署实战》围绕 Go 应用从开发完成到稳定上线的完整流程展开,系统讲解编译构建、环境配置、日志与配置管理、容器化部署以及常见运维问题处理。结合真实项目场景,拆解自动化构建与持续部署思路,帮助开发者建立可靠的发布流程,提升服务稳定性与可维护性。

3

2026.02.24

Golang 疑难杂症解决指南:常见问题排查与优化
Golang 疑难杂症解决指南:常见问题排查与优化

《Golang 疑难杂症解决指南》聚焦开发过程中常见却棘手的问题,从并发模型、内存管理、性能瓶颈到工程化实践逐步拆解。通过真实案例与调试思路,帮助开发者定位问题根因,建立系统化排查方法。不只给出答案,更强调分析路径与工具使用,让你在复杂 Go 项目中具备持续解决问题的能力。

1

2026.02.24

Golang 入门学习路线:从零基础到上手开发
Golang 入门学习路线:从零基础到上手开发

Golang 入门路线涵盖从零到上手的核心路径:首先打牢基础语法与切片等底层机制;随后攻克 Go 的灵魂——接口设计与 Goroutine 并发模型;接着通过 Gin 框架与 GORM 深入 Web 开发实战;最后在微服务与云原生工具开发中进阶,旨在培养具备高性能并发处理能力的后端工程师。

0

2026.02.24

中国研究生招生信息网官方网站入口 研招网网页版在线入口
中国研究生招生信息网官方网站入口 研招网网页版在线入口

中国研究生招生信息网入口(https://yz.chsi.com.cn) 此网站是研究生报名入口的唯一官方网站

95

2026.02.24

苹果官网入口与在线访问指南_中国站点快速直达与iPhone查看方法
苹果官网入口与在线访问指南_中国站点快速直达与iPhone查看方法

本专题汇总苹果官网最新可用入口及中国站点访问方式,涵盖官网直达链接、iPhone官方页面查看方法与常见访问说明,帮助用户快速进入苹果官方网站,便捷了解产品信息与官方服务。

14

2026.02.24

Asianfanfics官网入口与访问指南_AFF官方平台最新登录地址
Asianfanfics官网入口与访问指南_AFF官方平台最新登录地址

本专题系统整理Asianfanfics(AFF)官方网站最新可用入口,涵盖官方平台最新直达地址、官网登录方式及中文访问指引,帮助用户快速、安全地进入AFF平台浏览与使用相关内容。

15

2026.02.24

热门下载

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

精品课程

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

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