Let me introduce you to Excel two instances in PHP, one is to use Php-excelreader to import Excel and output, the other is to directly input Excel and export, see the example below.
With Php-excelreader this open source class, we can easily import Excel file data with the following example code:
Php-excelreader Download Address: http://sourceforge.net/projects/phpexcelreader/
Example. importing an Excel file
The code is as follows |
Copy Code |
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. Phpexcel lead to Excel
1. test.php
The code is as follows |
Copy Code |
Require_once ' reader.php '; Excelfile ($filename, $encoding); $data = new Spreadsheet_excel_reader (); Set output Encoding. $data->setoutputencoding (' GBK '); "Data.xls" means an Excel file to import into MySQL $data->read (' Data.xls '); @ $db = mysql_connect (' localhost ', ' root ', ' 123456 ') or Die ("Could not connect to database."); /Connection Database mysql_query ("Set names ' GBK '");//Output Chinese mysql_select_db (' mydb '); Select Database Error_reporting (e_all ^ e_notice); for ($i = 1; $i <= $data->sheets[0][' numrows '); $i + +) { The For loop of the following comments prints the Excel table data /* 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 is to insert the Excel table Data "3 fields" into MySQL, according to the number of your Excel table field, rewrite the following code! $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. exporting an Excel file
For example, I need a PHP to export Excel program, only need to export the relevant data to Excel table, so simple operation will not need to use those class library or something. Directly with the header of the way you can: header ("Content-type:application/vnd.ms-excel");
Look at the code:
|
copy code |
"!--? php header ("Content-type:appli Cation/vnd.ms-excel "); Header ("Content-disposition:attachment;filename=export_test.xls"); $tab = "T"; $br = "n"; $head = "number". $tab. " Remarks ". $br; The //output reads as follows: Echo $head. $br; echo "test321318312". $tab; echo "string1"; Echo $br; echo "330181199006061234". $tab;//Direct output is recognized by Excel as a numeric type echo "number"; Echo $br; echo "=" 330181199006061234 "". $tab;//output is required to process echo "string2"; Echo $br; ? |
After the export, you will find a problem, if the data is a number, there will be some unexpected situation. For example, "012345", in Excel will become "12345", if you enter the ID number such a long number, in Excel will be used in the scientific notation, and the last four digits will be biased, 0000, such as conjugation. This will require the cell to be formatted as text by
echo "=" 330181199006061234 "" If the program is UTF-8 encoded, but also need to use the Iconv function to transcode, otherwise it will be garbled, garbled.
Another word format import is similar, you can specify the header:
The code is as follows |
Copy Code |
Header ("Content-type:application/msword"); Header ("content-disposition:attachment; Filename=doc.doc "); |
http://www.bkjia.com/PHPjc/633143.html www.bkjia.com true http://www.bkjia.com/PHPjc/633143.html techarticle Let me introduce you to Excel two instances in PHP, one is to use Php-excelreader to import Excel and output, the other is to directly input Excel and export, see the example below. With the help of ...