Example of php excel table export
Include ("config. php ");
- // Load library
- Require 'php-excel. class. php ';
- // Create a simple 2-dimen1_array
- // @ Mysql_query ("set character set gb2312 ");
- $ StrSql = "select * from booklist ";
- $ Result = mysql_query ($ strSql );
// Print_r ($ result );
$ Data = array (
- 1 => array ('id', "name of the book"), // if the title is in Chinese, the nominal value in the table is blank.
- );
- While ($ row = mysql_fetch_array ($ result ))
- {
- Array_push ($ data, array ($ row ['bookid'], $ row ['bookname']);
- }
- // Generate file (constructor parameters are optional)
- $ Xls = new Excel_XML ('utf-8', false, 'My Test Sheet ');
- $ Xls-> addArray ($ data );
- $ Xls-> generateXML ('My-test ');
- ?>
- /**
- * Simple excel generating from PHP5
- *
- * @ Version 1.0
- */
/**
- * Generating excel documents on-the-fly from PHP5
- *
- * Uses the excel XML-specification to generate a native
- * XML document, readable/processable by excel.
- *
- * @ Package Utilities
- * @ Version 1.1
- *
- * @ Todo Issue #4: Internet Explorer 7 does not work well with the given header
- * @ Todo Add option to give out first line as header (bold text)
- * @ Todo Add option to give out last line as footer (bold text)
- * @ Todo Add option to write to file
- */
- 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 UTF-8)
- * @ 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;
- }
- }
- ?>
|