Home Backend Development PHP Tutorial Classic example of phpexcel exporting excel

Classic example of phpexcel exporting excel

Jul 25, 2016 am 08:56 AM

  1. <?php require_once('conn.php');//Link database?>
  2. <?php
  3. /**
  4. * phpexcel class library to export excel files
  5. * edit: bbs.it-home.org
  6. *
  7. */
  8. require 'php-excel.class.php' ;//Refer to Google's phpexcel class
  9. $result = mysql_query("SELECT * FROM dingdan"); //Query a data table and return a record set
  10. $data = array( 1 => array ('Order number', 'Line name'),);
  11. while($row = mysql_fetch_array($result)){//Loop the query results to EXCEL
  12. array_push($data,array($row["dd_bh"], $row ["dd_xianlu_name"]));
  13. }
  14. // generate file (constructor parameters are optional)
  15. $xls = new Excel_XML('GB2312', false, 'Financial Statement');
  16. $xls->addArray($data );
  17. $xls->generateXML('2011010320');
  18. ?>
Copy code

Attached is the phpexcel class php-excel.class.php file code.

  1. <p><?php
  2. class Excel_XML
  3. {
  4. /**
  5. * Header (of document)
  6. * @var string
  7. */
  8. private $header = "<?xml version=/"1.0/" encoding=/"%s/"?/>/n<Workbook xmlns=/"urn:schemas-microsoft-com:office:spreadsheet/" xmlns:x=/"urn:schemas-microsoft-com:office:excel/" xmlns:ss=/"urn:schemas-microsoft-com:office:spreadsheet/" xmlns:html=/"http://www.w3.org/TR/REC-html40/">";
  9. /**
  10. * Footer (of document)
  11. * @var string
  12. */
  13. private $footer = "</Workbook>";
  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 .= "<Cell><Data ss:Type=/"$type/">" . $v . "</Data></Cell>/n";
  98. endforeach;
  99. $this->lines[] = "<Row>/n" . $cells . "</Row>/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. }</p>
  110. <p> /**
  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 "/n<Worksheet ss:Name=/"" . $this->sWorksheetTitle . "/">/n<Table>/n";
  125. foreach ($this->lines as $line)
  126. echo $line;
  127. echo "</Table>/n</Worksheet>/n";
  128. echo $this->footer;
  129. }
  130. }
  131. ?></p>
复制代码


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles