Summary of PHP Excel File Operations

Source: Internet
Author: User
Tags php excel

1. generate EXCEL files without using COM in PHP CopyCode The Code is as follows: <?
Header ("Content-Type: Application/vnd. MS-excel ");
Header ("content-Disposition: filename=test.xls ");
Echo "test1 \ t ";
Echo "Test2 \ t \ n ";
Echo "test1 \ t ";
Echo "Test2 \ t \ n ";
Echo "test1 \ t ";
Echo "Test2 \ t \ n ";
Echo "test1 \ t ";
Echo "Test2 \ t \ n ";
Echo "test1 \ t ";
Echo "Test2 \ t \ n ";
Echo "test1 \ t ";
Echo "Test2 \ t \ n ";
?>

When you run the above Code in the PHP environment, you can see the browser asking you whether to download the Excel document, click Save, and an Excel file is added to the hard disk, you can see the final result when using Excel.
In fact, when doing real applications, you can extract the data from the database, and then add \ t after each column of data ends, after each row ends, add the \ n method to echo it, and use header ("Content-Type: Application/vnd. MS-excel ") indicates that the Excel file is output, and the file name output is text.xls using header (" content-Disposition: filename=test.xls. In this case, OK.
We can modify the header so that it can output more files in different formats. This makes PHP easier to process various types of files.
2. Use PHP to convert a MySQL DATA table to an Excel File Format
Copy code The Code is as follows: <? PHP
$ Db_server = "localhost ";
$ Db_username = "mydowns ";
$ Db_password = "";
$ Db_dbname = "mydowns ";
$ Db_tblname = "user ";
$ Connect = @ mysql_connect ($ db_server, $ db_username, $ db_password)
Or die ("Couldn @ # T connect .");
$ Db = @ mysql_select_db ($ db_dbname, $ Connect)
Or die ("Couldn @ # T select database .");
$ File_type = "Vnd. MS-excel ";
$ File_ending = "xls ";
Header ("Content-Type: Application/$ file_type ");
Header ("content-Disposition: attachment; filename = mydowns. $ file_ending ");
Header ("Pragma: No-Cache ");
Header ("expires: 0 ");
$ Now_date = Date (@ # Y-m-d h: I @#);
$ Title = "Database Name: $ db_dbname, data table: $ db_tblname, backup date: $ now_date ";
$ SQL = "select * from $ db_tblname ";
$ Alt_db = @ mysql_select_db ($ db_dbname, $ Connect)
Or die ("Couldn @ # T select database ");
$ Result = @ mysql_query ($ SQL, $ Connect)
Or die (mysql_error ());
Echo ("$ title \ n ");
$ Sep = "\ t ";
For ($ I = 0; $ I <mysql_num_fields ($ result); $ I ++ ){
Echo mysql_field_name ($ result, $ I). "\ t ";
}
Print ("\ n ");
$ I = 0;
While ($ ROW = mysql_fetch_row ($ result ))
{
$ Schema_insert = "";
For ($ J = 0; $ j <mysql_num_fields ($ result); $ J ++)
{
If (! Isset ($ row [$ J])
$ Schema_insert. = "null". $ Sep;
Elseif ($ row [$ J]! = "")
$ Schema_insert. = "$ row [$ J]". $ Sep;
Else
$ Schema_insert. = "". $ Sep;
}
$ Schema_insert = str_replace ($ Sep. "$", "", $ schema_insert );
$ Schema_insert. = "\ t ";
Print (TRIM ($ schema_insert ));
Print "\ n ";
$ I ++;
}
Return (true );
?>

3. An example of PHP Excel operations (using a COM object to generate an Excel file)
This is for those who just like to simply process the Excel files.Copy codeThe Code is as follows: <? PHP
// Define an Excel file
$ Workbook = "C:/My Documents ents/test.xls ";
$ Sheet = "sheet1 ";
// Generate a COM Object $ ex
$ EX = new COM ("Excel. Sheet") or die ("no connection !!! ");
// Open an Excel file
$ Book = $ ex-> application-> workbooks-> open ($ workbook) or die ("cannot be opened !!! ");
$ Sheets = $ book-> worksheets ($ sheet );
$ Sheets-> activate;
// Obtain a cell
$ Cell = $ sheets-> cells (5, 5 );
$ Cell-> activate;
// Assign a value to the cell
$ Cell-> value = 999;
// Save it as another file newtest.xls
$ Ex-> application-> activeworkbook-> saveas ("newtest.xls ");
// Close the Excel file. If you want to see the results, comment out the following two lines. You can manually turn off the Excel file.
$ Ex-> application-> activeworkbook-> close ("false ");
Unset ($ ex );
?>

4. Create Excel files in PHP
You can use PHP to generate an Excel file.
----------------------------
Excel Functions
----------------------------
Save the following code as Excel. php and include it in the page.
Then call
1. Call xlsbof ()
2. Write some content to xlswritenunber () or xlswritelabel.
3. Call xlseof ()
You can also use the fwrite function to directly write data to the server, instead of simply displaying data in the browser with Echo. Copy code The Code is as follows: <? PHP
// ----- Begin of function library -----
// Excel begin of File Header
Function xlsbof (){
Echo pack ("Ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0 );
Return;
}
// Excel end of file footer
Function xlseof (){
Echo pack ("SS", 0x0a, 0x00 );
Return;
}
// Function to write a number (double) into row, Col
Function xlswritenumber ($ row, $ Col, $ value ){
Echo pack ("sssss", 0x203, 14, $ row, $ Col, 0x0 );
Echo pack ("D", $ value );
Return;
}
// Function to write a label (text) into row, Col
Function xlswritelabel ($ row, $ Col, $ value ){
$ L = strlen ($ value );
Echo pack ("Ssssss", 0x204, 8 + $ L, $ row, $ Col, 0x0, $ L );
Echo $ value;
Return;
}
// ----- End of function library -----
?>
//
// To display the contents directly in a MIME compatible Browser
// Add the following lines on top of your php file:
<? PHP
Header ("expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("last-modified:". gmdate ("D, D m YH: I: s"). "GMT ");
Header ("cache-control: No-cache, must-revalidate ");
Header ("Pragma: No-Cache ");
Header (@ # Content-Type: Application/X-MSExcel @#);
Header ("content-Disposition: attachment; filename=empllist.xls ");
Header ("content-Description: PHP/Interbase generated data ");
//
// The next lines demonstrate the generation of the Excel stream
//
Xlsbof (); // begin Excel stream
Xlswritelabel (0, 0, "This is a label"); // write a label in A1, use for dates too
Xlswritenumber (0, 1, 9999); // write a number B1
Xlseof (); // close the stream
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.