Export a classic excel instance using phpexcel
-
- /**
- * Export an excel file using the phpexcel class library
- * Edit: bbs.it-home.org
- *
- */
- Require 'php-excel. class. php'; // reference Google's phpexcel class
- $ Result = mysql_query ("SELECT * FROM dingdan"); // query a data table and return the record set
- $ Data = array (1 => array ('Order number', 'line name '),);
- While ($ row = mysql_fetch_array ($ result) {// cyclically output the query results to EXCEL
- Array_push ($ data, array ($ row ["dd_bh"], $ row ["dd_xianlu_name"]);
- }
- // Generate file (constructor parameters are optional)
- $ Xls = new Excel_XML ('gb2312', false, 'financial report ');
- $ Xls-> addArray ($ data );
- $ Xls-> generateXML ('20140901 ');
- ?>
Appendix, php Excel class php-excel.class.php file code.
Class Excel_XML
- {
- /**
- * Header (of document)
- * @ Var string
- */
- Private $ header =" /N ";
- /**
- * Footer (of document)
- * @ Var string
- */
- Private $ footer ="";
- /**
- * Lines to output in the excel document
- * @ Var array
- */
- Private $ lines = array ();
- /**
- * Used encoding
- * @ Var string
- */
- Private $ sEncoding;
-
- /**
- * Convert variable types
- * @ Var boolean
- */
- Private $ bConvertTypes;
-
- /**
- * Worksheet title
- * @ Var string
- */
- Private $ sWorksheetTitle;
- /**
- * Constructor
- *
- * The constructor allows the setting of some additional
- * Parameters so that the library may be configured
- * One's needs.
- *
- * On converting types:
- * When set to true, the library tries to identify the type
- * The variable value and set the field specification for Excel
- * Accordingly. Be careful with article numbers or postcodes
- * Starting with a '0' (zero )!
- *
- * @ Param string $ sEncoding Encoding to be used (defaults to GBK)
- * @ Param boolean $ bConvertTypes Convert variables to field specification
- * @ Param string $ sWorksheetTitle Title for the worksheet
- */
- Public function _ construct ($ sEncoding = 'utf-8', $ bConvertTypes = false, $ sWorksheetTitle = 'table1 ')
- {
- $ This-> bConvertTypes = $ bConvertTypes;
- $ This-> setEncoding ($ sEncoding );
- $ This-> setWorksheetTitle ($ sWorksheetTitle );
- }
-
- /**
- * Set encoding
- * @ Param string Encoding type to set
- */
- Public function setEncoding ($ sEncoding)
- {
- $ This-> sEncoding = $ sEncoding;
- }
- /**
- * Set worksheet title
- *
- * Strips out not allowed characters and trims
- * Title to a maximum length of 31.
- *
- * @ Param string $ title Title for worksheet
- */
- Public function setWorksheetTitle ($ title)
- {
- $ Title = preg_replace ("/[// |: |/// | /? |/* |/[|/]/"," ", $ Title );
- $ Title = substr ($ title, 0, 31 );
- $ This-> sWorksheetTitle = $ title;
- }
- /**
- * Add row
- *
- * Adds a single row to the document. If set to true, self: bConvertTypes
- * Checks the type of variable and returns the specific field settings
- * For the cell.
- *
- * @ Param array $ array One-dimen1_array with row content
- */
- Private function addRow ($ array)
- {
- $ Cells = "";
- Foreach ($ array as $ k => $ v ):
- $ Type = 'string ';
- If ($ this-> bConvertTypes === true & is_numeric ($ v )):
- $ Type = 'number ';
- Endif;
- $ V = htmlentities ($ v, ENT_COMPAT, $ this-> sEncoding );
- $ Cells. =" ". $ V ." /N ";
- Endforeach;
- $ This-> lines [] =" /N ". $ cells ." /N ";
- }
- /**
- * Add an array to the document
- * @ Param array 2-dimen1_array
- */
- Public function addArray ($ array)
- {
- Foreach ($ array as $ k => $ v)
- $ This-> addRow ($ v );
- }
/**
- * Generate the excel file
- * @ Param string $ filename Name of excel file to generate (...xls)
- */
- Public function generateXML ($ filename = 'Excel-export ')
- {
- // Correct/validate filename
- $ Filename = preg_replace ('/[^ aA-zZ0-9/_/-]/', '', $ filename );
-
- // Deliver header (as recommended in php manual)
- Header ("Content-Type: application/vnd. ms-excel; charset =". $ this-> sEncoding );
- Header ("Content-Disposition: inline; filename =/" ". $ filename.". xls /"");
- // Print out document to the browser
- // Need to use stripslashes for the damn ">"
- Echo stripslashes (sprintf ($ this-> header, $ this-> sEncoding ));
- Echo "/n SWorksheetTitle. "/">/n
- Foreach ($ this-> lines as $ line)
- Echo $ line;
- Echo"
/N /N ";
- Echo $ this-> footer;
- }
- }
- ?>
|