Method 1: features, simple, worry-free, applicable only to Windows Servers
Reference content is as follows:
<? PHP header ("Content-Type: Application/vnd. MS-excel "); header (" content-Disposition: attachment=filename=test_data.xls "); $ Tx = 'header'; echo $ Tx. "\ n"; // The output content is as follows: Echo "name ". "\ t"; echo "age ". "\ t"; echo "education ". "\ t"; echo "\ n"; echo "Zhang San ". "\ t"; echo "25 ". "\ t"; echo "Undergraduate ". "\ t";?>
Method 2: reference the small library recommended by Google Code (similar to method 1, more complex)
: Http://code.google.com/p/php-excel/downloads/list
Method 3: phpexcel class library. It has powerful functions and is very convenient to Operate Excel. It is especially convenient to add images. It supports jpg gif format and win excel2003 and win excel2007.
: Http://www.codeplex.com/PHPExcel
The following are the usage methods
<? /*** The following is an example. For rows starting with //, it is optional. Open the comment of the corresponding row as needed. * If excel5 is used, the output content should be GBK encoded. */Include 'phpexcel. PHP '; Include 'phpexcel/Writer/excel2007.php'; // or include 'phpexcel/Writer/excel5.php '; for output. xls // uncomment // require_once 'phpexcel/Writer/excel5.php '; // used for other earlier versions of XLS // or /// require_once 'phpexcel/Writer/excel2007.php '; // used for Excel-2007 format // create an instance for processing objects $ objexcel = new phpexcel (); // create an instance for writing objects in file format, uncomment // $ objwriter = new phpexcel_writer_excel5 ($ objexcel); // used for other version formats // o R // $ objwriter = new phpexcel_writer_excel2007 ($ objexcel); // used in the 2007 format // $ objwriter-> setoffice2003compatibility (true ); //*************************************/ /set the basic attributes of the document $ objprops = $ objexcel-> getproperties (); $ objprops-> setcreator ("Zeal li"); $ objprops-> setlastmodifiedby ("Zeal li"); $ objprops-> settitle ("office XLS test document "); $ objprops-> setsubject ("office XLS test document, Demo"); $ objprops-> setd Eworkflow ("test document, generated by phpexcel. "); $ objprops-> setkeywords (" Office EXCEL phpexcel "); $ objprops-> setcategory (" test "); //*************************************/ /set the current sheet index, used for subsequent content operations. // Display the call only when multiple sheets are used. // By default, phpexcel will automatically create the first sheet with the sheetindex set to 0 $ objexcel-> setactivesheetindex (0); $ objactsheet = $ objexcel-> getactivesheet (); // set the name of the current active sheet $ objactsheet-> settitle ('test sheet '); //*************************************/ /set the cell content /// phpexcel automatically determines the cell content type based on the input content $ objactsheet-> setcellvalue ('a1 ', 'string content'); // string content $ objactsheet-> setcellvalue ('a2 ', 26); // value $ objactsheet-> setcellvalue ('a3', true ); // Boolean value $ objactsheet -> Setcellvalue ('a4 ',' = sum (A2: A2) '); // formula // explicitly specify the content type $ objactsheet-> setcellvalueexplicit ('a5 ', '200', phpexcel_cell_datatype: type_string); // merge cells $ objactsheet-> mergecells ('b1: c22'); // separate cells $ objactsheet-> unmergecells ('b1: c22 '); //*************************************/ /set the cell style /// set the width $ objactsheet-> getcolumndimension ('B ') -> setautosize (true); $ objactsheet-> getcolumndimension ('A')-> setwidth (3 0); $ objstylea5 = $ objactsheet-> getstyle ('a5 '); // you can specify the number format of the cell content. //// If phpexcel_writer_excel5 is used to generate content, // note that in the const variable defined in the phpexcel_style_numberformat class, other types can be used normally, but when setformatcode // is format_number, the actual effect is not set to "0 ". You need to // modify the getxf ($ style) method in the phpexcel_writer_excel5_format class source code, // In if ($ this-> _ biff_version = 0x0500) {(near 363rd rows) first add a // line of code: // if ($ ifmt = '0') $ ifmt = 1; // set the format to phpexcel_style_numberformat: format_number, to prevent some large numbers from being displayed in scientific notation, you can use the setautosize method below to display all the content of each row according to the original content. $ Objstylea5-> getnumberformat ()-> setformatcode (phpexcel_style_numberformat: format_number); // set the font $ objfonta5 = $ objstylea5-> getfont (); $ objfonta5-> setname ('courier new'); $ objfonta5-> setsize (10); $ objfonta5-> setbold (true); $ objfonta5-> setunderline (phpexcel_style_font :: underline_single); $ objfonta5-> getcolor ()-> setargb ('ff9999999999 '); // set the alignment mode $ align = $ objstylea5-> getalignment (); $ objaligna5-> Al (Border: horizontal_right); $ objaligna5-> setvertical (Border: vertical_center); // set the border $ objbordera5 = $ objstylea5-> getborders (); $ objbordera5-> gettop ()-> setborderstyle (phpexcel_style_border: border_thin); $ objbordera5-> gettop ()-> getcolor ()-> setargb ('ffff0000 '); // color $ objbordera5-> getbottom ()-> setborderstyle (phpexcel_style_border: border_thin); $ objbordera5-> getlef T ()-> setborderstyle (phpexcel_style_border: border_thin); $ objbordera5-> getright ()-> setborderstyle (phpexcel_style_border: border_thin ); // set the fill color $ objfilla5 = $ objstylea5-> getfill (); $ objfilla5-> setfilltype (phpexcel_style_fill: fill_solid); $ objfilla5-> getstartcolor () -> setargb ('ffeeeeeee'); // copy the style information from the specified cell. $ objactsheet-> duplicatestyle ($ objstylea5, 'b1: c22 '); //*************************************/ /Add image $ OBJ Drawing = new phpexcel_worksheet_drawing (); $ objdrawing-> setname ('zealimg '); $ objdrawing-> setdescription ('image inserted by zeal'); $ objdrawing-> setpath ('. /zeali.net.logo.gif '); $ objdrawing-> setheight (36); $ objdrawing-> setcoordinates ('c23'); $ objdrawing-> setoffsetx (10 ); $ objdrawing-> setrotation (15); $ objdrawing-> getshadow ()-> setvisible (true); $ objdrawing-> getshadow ()-> setdirection (36 ); $ objdrawing-> setworks HEET ($ objactsheet); // Add a new worksheet $ objexcel-> createsheet (); $ objexcel-> getsheet (1)-> settitle ('test 2 '); // protect cells $ objexcel-> getsheet (1)-> getprotection ()-> setsheet (true); $ objexcel-> getsheet (1)-> protectcells ('a1: c22', 'phpexcel '); //*************************************/ /output content // $ outputfilename = "output.xls "; // to the file // $ objwriter-> Save ($ outputfilename); // to the browser header ("Content-Type: Application/force-download" ); Header ("Content-Type: Application/octet-stream"); header ("Content-Type: Application/download"); header ('content-Disposition: inline; filename = "'. $ outputfilename. '"'); header (" content-transfer-encoding: Binary "); header (" expires: Mon, 26 Jul 1997 05:00:00 GMT "); header (" last-modified: ". gmdate ("D, D m y h: I: s "). "GMT"); header ("cache-control: Must-revalidate, post-check = 0, pre-check = 0"); header ("pragm A: No-Cache "); $ objwriter-> Save ('php: // output');?>