phpexcel导出excel的经典实例

php中文网
发布: 2016-07-25 08:56:07
原创
1080人浏览过
  1. /**
  2. * phpexcel类库导出excel文件
  3. * edit: bbs.it-home.org
  4. *
  5. */
  6. require 'php-excel.class.php';//引用谷歌的phpexcel 类
  7. $result = mysql_query("SELECT * FROM dingdan"); //查询一个数据表,并返回记录集
  8. $data = array( 1 => array ('订单编号', '线路名称'),);
  9. while($row = mysql_fetch_array($result)){//把查询的结果循环输出到EXCEL中
  10. array_push($data,array($row["dd_bh"], $row["dd_xianlu_name"]));
  11. }
  12. // generate file (constructor parameters are optional)
  13. $xls = new Excel_XML('GB2312', false, '财务报表');
  14. $xls->addArray($data);
  15. $xls->generateXML('2011010320');
  16. ?>
复制代码

附,phpexcel 类 php-excel.class.php 文件代码。

  1. class excel_xml

  2. {
  3. /**
  4. * header (of document)
  5. * @var string
  6. */
  7. private $header = "/n";
  8. /**
  9. * footer (of document)
  10. * @var string
  11. */
  12. private $footer = "
  13. ";
  14. /**
  15. * lines to output in the excel document
  16. * @var array
  17. */
  18. private $lines = array();
  19. /**
  20. * used encoding
  21. * @var string
  22. */
  23. private $sencoding;
  24. /**
  25. * convert variable types
  26. * @var boolean
  27. */
  28. private $bconverttypes;
  29. /**
  30. * worksheet title
  31. * @var string
  32. */
  33. private $sworksheettitle;
  34. /**
  35. * constructor
  36. *
  37. * the constructor allows the setting of some additional
  38. * parameters so that the library may be configured to
  39. * one's needs.
  40. *
  41. * on converting types:
  42. * when set to true, the library tries to identify the type of
  43. * the variable value and set the field specification for excel
  44. * accordingly. be careful with article numbers or postcodes
  45. * starting with a '0' (zero)!
  46. *
  47. * @param string $sencoding encoding to be used (defaults to gbk)
  48. * @param boolean $bconverttypes convert variables to field specification
  49. * @param string $sworksheettitle title for the worksheet
  50. */
  51. public function __construct($sencoding = 'utf-8', $bconverttypes = false, $sworksheettitle = 'table1')
  52. {
  53. $this->bconverttypes = $bconverttypes;
  54. $this->setencoding($sencoding);
  55. $this->setworksheettitle($sworksheettitle);
  56. }
  57. /**
  58. * set encoding
  59. * @param string encoding type to set
  60. */
  61. public function setencoding($sencoding)
  62. {
  63. $this->sencoding = $sencoding;
  64. }
  65. /**
  66. * set worksheet title
  67. *
  68. * strips out not allowed characters and trims the
  69. * title to a maximum length of 31.
  70. *
  71. * @param string $title title for worksheet
  72. */
  73. public function setworksheettitle ($title)
  74. {
  75. $title = preg_replace ("/[///|:|//|/?|/*|/[|/]]/", "", $title);
  76. $title = substr ($title, 0, 31);
  77. $this->sworksheettitle = $title;
  78. }
  79. /**
  80. * add row
  81. *
  82. * adds a single row to the document. if set to true, self::bconverttypes
  83. * checks the type of variable and returns the specific field settings
  84. * for the cell.
  85. *
  86. * @param array $array one-dimensional array with row content
  87. */
  88. private function addrow ($array)
  89. {
  90. $cells = "";
  91. foreach ($array as $k => $v):
  92. $type = 'string';
  93. if ($this->bconverttypes === true && is_numeric($v)):
  94. $type = 'number';
  95. endif;
  96. $v = htmlentities($v, ent_compat, $this->sencoding);
  97. $cells .= "" . $v . "/n";
  98. endforeach;
  99. $this->lines[] = "/n" . $cells . "/n";
  100. }
  101. /**
  102. * add an array to the document
  103. * @param array 2-dimensional array
  104. */
  105. public function addarray ($array)
  106. {
  107. foreach ($array as $k => $v)
  108. $this->addrow ($v);
  109. }
  110. /**

    ViiTor实时翻译
    ViiTor实时翻译

    AI实时多语言翻译专家!强大的语音识别、AR翻译功能。

    ViiTor实时翻译 116
    查看详情 ViiTor实时翻译
  111. * Generate the excel file
  112. * @param string $filename Name of excel file to generate (...xls)
  113. */
  114. public function generateXML ($filename = 'excel-export')
  115. {
  116. // correct/validate filename
  117. $filename = preg_replace('/[^aA-zZ0-9/_/-]/', '', $filename);
  118. // deliver header (as recommended in php manual)
  119. header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
  120. header("Content-Disposition: inline; filename=/"" . $filename . ".xls/"");
  121. // print out document to the browser
  122. // need to use stripslashes for the damn ">"
  123. echo stripslashes (sprintf($this->header, $this->sEncoding));
  124. echo "/nsWorksheetTitle . "/">/n/n";
  125. foreach ($this->lines as $line)
  126. echo $line;
  127. echo "
  128. /n
    /n";
  129. echo $this->footer;
  130. }
  131. }
  132. ?>
复制代码


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

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

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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