PHP method for importing and exporting Excel files _ PHP Tutorial

Source: Internet
Author: User
Use PHP to import and export Excel files. Next I will introduce you to the two excel instances in php. one is to import and output excel files using PHP-ExcelReader, and the other is to directly input and export excel files. The following is an example. I will introduce you to the following two examples of excel operations in php. one is to use PHP-ExcelReader to import and output excel files, and the other is to directly input and export excel files. let's look at the example below.


With the open-source PHP-ExcelReader class, we can easily import Excel file data. the sample code is as follows:

PHP-ExcelReader: http://sourceforge.net/projects/phpexcelreader/

Example: import an Excel file

The code is as follows:

Require_once 'Excel/reader. php ';
$ Data = new Spreadsheet_Excel_Reader ();
$ Data-> setOutputEncoding ('gbk ');
$ Data-> read('test.xls ');
For ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++ ){
For ($ j = 1; $ j <= $ data-> sheets [0] ['numcols']; $ j ++ ){
Echo ". $ data-> sheets [0] ['cells '] [$ I] [$ j]." ",";
}
Echo "n ";
}
?>

Example. export from phpexcel to excel

1. test. php

The code is as follows:

Require_once 'reader. php ';

// ExcelFile ($ filename, $ encoding );
$ Data = new Spreadsheet_Excel_Reader ();

// Set output Encoding.
$ Data-> setOutputEncoding ('gbk ');

// ”Data.xls refers to the excel file to be imported to mysql.
$ Data-> read('data.xls ');

@ $ Db = mysql_connect ('localhost', 'root', '123') or
Die ("cocould not connect to database."); // connect to the database
Mysql_query ("set names 'gbk'"); // output Chinese
Mysql_select_db ('mydb'); // select a database
Error_reporting (E_ALL ^ E_NOTICE );

For ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++ ){
// Print the excel table data in a for loop with the following comments
/*
For ($ j = 1; $ j <= $ data-> sheets [0] ['numcols']; $ j ++ ){
Echo ". $ data-> sheets [0] ['cells '] [$ I] [$ j]." ",";
}
Echo "n ";
// PHP open source code


*/
// The following code inserts [3 fields] of the excel table data into mysql. rewrite the following code based on the number of fields in your excel table!
$ SQL = "INSERT INTO test VALUES ('".
$ Data-> sheets [0] ['cells '] [$ I] [1]. "', '".
$ Data-> sheets [0] ['cells '] [$ I] [2]. "', '".
$ Data-> sheets [0] ['cells '] [$ I] [3]. "')";
Echo $ SQL .'
';
$ Res = mysql_query ($ SQL );
}

?>

Example: export an excel file


For example, if I need a php excel export program, I only need to export the relevant data to an excel table. in this simple operation, I don't need to use the class libraries or anything. You can use the header directly: header ("Content-type: application/vnd. ms-excel ");

Look at the code:

The code is as follows:

Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment?filename=export_test.xls ");
$ Tab = "t"; $ br = "n ";
$ Head = "no.". $ tab. "Remarks". $ br;
// The output content is as follows:
Echo $ head. $ br;
Echo "test321318312". $ tab;
Echo "string1 ";
Echo $ br;

Echo "330181199006061234". $ tab; // The direct output is recognized as a number by Excel.
Echo "number ";
Echo $ br;

Echo "=" 330181199006061234 "". $ tab; // The original output needs to be processed.
Echo "string2 ";
Echo $ br;
?>

After export, you will find a problem. if the data is a number, unexpected situations may occur. For example, "012345" can be changed to "12345" in excel. if you enter a long number such as an ID card number, it will be represented by scientific notation in excel, in addition, the last four digits may see deviations, 0000 displacement, and so on. In this case, you need to set the cell to the text format by using the following method:
Echo "=" 330181199006061234 "" If the program is UTF-8 encoded, you also need to use the iconv function for transcoding. Otherwise, it will be garbled.

In addition, if the word format is similar, you can specify the header:

The code is as follows:

Header ("Content-Type: application/msword ");
Header ("Content-Disposition: attachment; filename=doc.doc ");

Bytes. With the help...

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.