PHP imports excel Data to MYSQL, and php imports excelmysql
Here is an example of importing an excel file to mysql. It took me one night to test the function. No garbled characters are displayed in simplified and traditional Chinese characters. It is very easy to use.
PHP-ExcelReader,: http://sourceforge.net/projects/phpexcelreader
Note:
Test environment: the MYSQL database adopts utf8 encoding. The imported EXCEL file is in xls format. After testing, the xlsx format [excel 2007] is also OK.
In this text, the red mark is worth attention. Replace it with your configured data, such as database configuration. Run http: // localost/test. php to import data.
Below is the detailed code I posted, where test. php is the test file I wrote, reader. php and oleread. inc files are downloaded from the web site provided above.
1. test. php
The Code is as follows:
<? Phprequire_once '. /shortdes/reader. php '; // ExcelFile ($ filename, $ encoding); $ data = new Spreadsheet_Excel_Reader (); // Set output Encoding. $ data-> setOutputEncoding ('gbk'); // your data.xls is an excel file to be imported to mysql $ data-> read('date.xls '); @ $ db = mysql_connect ('localhost', 'root', '000000') or die ("cocould not connect to database. "); // connect to the database mysql_query (" set names 'gbk' "); // output the Chinese mysql_select_db ('wenbaiedu ');/ /Select 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 annotations/* for ($ j = 1; $ j <= $ data-> sheets [0] ['numcols']; $ j ++) {echo """. $ data-> sheets [0] ['cells '] [$ I] [$ j]. "", ";} echo" n "; * // * He asked hovertree.com * // the following code inserts the excel table data [3 fields] into mysql, modify 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. '<br/>'; $ res = mysql_query ($ SQL);?>
Included files
OLERead. php
<? Php define ('num _ BIG_BLOCK_DEPOT_BLOCKS_POS ', 0x2c); define ('small _ BLOCK_DEPOT_BLOCK_POS', 0x3c); define ('root _ START_BLOCK_POS ', 0x30 ); define ('Big _ BLOCK_SIZE ', 0x200); define ('small _ BLOCK_SIZE', 0x40); define ('extension _ BLOCK_POS ', 0x44 ); define ('num _ EXTENSION_BLOCK_POS ', 0x48); define ('Property _ STORAGE_BLOCK_SIZE', 0x80); define ('Big _ BLOCK_DEPOT_BLOCKS_POS ', 0x4c ); define ('small _ BLOCK_THRESHOLD ', 0x1000); // property storage offsets define ('size _ OF_NAME_POS ', 0x40); define ('Type _ pos', 0x42 ); define ('start _ BLOCK_POS ', 0x74); define ('size _ pos', 0x78); define ('identifier _ olean', pack ("cccccccccc ", 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1); // echo 'root _ START_BLOCK_POS = '. ROOT_START_BLOCK_POS. "\ n"; // echo bin2hex ($ data [ROOT_START_BLOCK_POS]). "\ n"; // echo "a ="; // echo $ data [ROOT_START_BLOCK_POS];/ /Function log function GetInt4d ($ data, $ pos) {$ value = ord ($ data [$ pos]) | (ord ($ data [$ pos + 1]) <8) | (ord ($ data [$ pos + 2]) <16) | (ord ($ data [$ pos + 3]) <24 ); if ($ value >=4294967294) {$ value =-2;} return $ value;} class OLERead {var $ data = ''; function OLERead () {} function read ($ sFileName) {// check if file exist and is readable (Darko Miljanovic) if (! Is_readable ($ sFileName) {$ this-> error = 1; return false;} $ this-> data = @ file_get_contents ($ sFileName); if (! $ This-> data) {$ this-> error = 1; return false;} // echo IDENTIFIER_OLE; // echo 'start '; if (substr ($ this-> data, 0, 8 )! = IDENTIFIER_OLE) {$ this-> error = 1; return false;} $ this-> numBigBlockDepotBlocks = GetInt4d ($ this-> data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS ); $ this-> sbdStartBlock = GetInt4d ($ this-> data, SMALL_BLOCK_DEPOT_BLOCK_POS); $ this-> rootStartBlock = GetInt4d ($ this-> data, ROOT_START_BLOCK_POS ); $ this-> extensionBlock = GetInt4d ($ this-> data, EXTENSION_BLOCK_POS); $ this-> numExtensionBlocks = GetInt4d ($ this-> data, NUM_EXTENSION_BLOCK_POS);/* echo $ this-> numBigBlockDepotBlocks. ""; echo $ this-> sbdStartBlock. ""; echo $ this-> rootStartBlock. ""; echo $ this-> extensionBlock. ""; echo $ this-> numExtensionBlocks. ""; * // echo "sbdStartBlock = $ this-> sbdStartBlock \ n"; $ bigBlockDepotBlocks = array (); $ pos = BIG_BLOCK_DEPOT_BLOCKS_POS; // echo "pos = $ pos"; $ bbdBlocks = $ this-> numBigBlockDepotBlocks; if ($ this-> nu MExtensionBlocks! = 0) {$ bbdBlocks = (BIG_BLOCK_SIZE-BIG_BLOCK_DEPOT_BLOCKS_POS)/4 ;}for ($ I = 0; $ I <$ bbdBlocks; $ I ++) {$ bigBlockDepotBlocks [$ I] = GetInt4d ($ this-> data, $ pos); $ pos + = 4 ;}for ($ j = 0; $ j <$ this-> numExtensionBlocks; $ j ++) {$ pos = ($ this-> extensionBlock + 1) * BIG_BLOCK_SIZE; $ blocksToRead = min ($ this-> numBigBlockDepotBlocks-$ bbdBlocks, BIG_BLOCK_SIZE/4-1); for ($ I = $ bbdBlocks; $ I <$ BbdBlocks + $ blocksToRead; $ I ++) {$ bigBlockDepotBlocks [$ I] = GetInt4d ($ this-> data, $ pos); $ pos + = 4 ;} $ bbdBlocks + = $ blocksToRead; if ($ bbdBlocks <$ this-> numBigBlockDepotBlocks) {$ this-> extensionBlock = GetInt4d ($ this-> data, $ pos );}} /* hovertree.com * // var_dump ($ bigBlockDepotBlocks); // readBigBlockDepot $ pos = 0; $ index = 0; $ this-> bigBlockChain = array (); for ($ I = 0; $ I <$ this-> nu MBigBlockDepotBlocks; $ I ++) {$ pos = ($ bigBlockDepotBlocks [$ I] + 1) * BIG_BLOCK_SIZE; // echo "pos = $ pos "; for ($ j = 0; $ j <BIG_BLOCK_SIZE/4; $ j ++) {$ this-> bigBlockChain [$ index] = GetInt4d ($ this-> data, $ pos); $ pos + = 4; $ index ++ ;}// var_dump ($ this-> bigBlockChain); // echo '==== 2 '; // readSmallBlockDepot (); $ pos = 0; $ index = 0; $ sbdBlock = $ this-> sbdStartBlock; $ this-> smallBlockChain = ar Ray (); while ($ sbdBlock! =-2) {$ pos = ($ sbdBlock + 1) * BIG_BLOCK_SIZE; for ($ j = 0; $ j <BIG_BLOCK_SIZE/4; $ j ++) {$ this-> smallBlockChain [$ index] = GetInt4d ($ this-> data, $ pos); $ pos + = 4; $ index ++ ;} $ sbdBlock = $ this-> bigBlockChain [$ sbdBlock];} // readData (rootStartBlock) $ block = $ this-> rootStartBlock; $ pos = 0; $ this-> entry = $ this->__ readData ($ block);/* while ($ block! =-2) {$ pos = ($ block + 1) * BIG_BLOCK_SIZE; $ this-> entry = $ this-> entry. substr ($ this-> data, $ pos, BIG_BLOCK_SIZE); $ block = $ this-> bigBlockChain [$ block];} * // echo '= '. $ this-> entry. "==="; $ this->__ readPropertySets ();} function _ readData ($ bl) {$ block = $ bl; $ pos = 0; $ data = ''; while ($ block! =-2) {$ pos = ($ block + 1) * BIG_BLOCK_SIZE; $ data = $ data. substr ($ this-> data, $ pos, BIG_BLOCK_SIZE); // echo "pos = $ pos data = $ data \ n "; $ block = $ this-> bigBlockChain [$ block];} return $ data;} function _ readPropertySets () {$ offset = 0; // var_dump ($ this-> entry); while ($ offset <strlen ($ this-> entry) {$ d = substr ($ this-> entry, $ offset, PROPERTY_STORAGE_BLOCK_SIZE); $ nameSize = ord ($ d [SIZE_OF_NAME _ POS]) | (ord ($ d [SIZE_OF_NAME_POS + 1]) <8); $ type = ord ($ d [TYPE_POS]); // $ maxBlock = strlen ($ d)/BIG_BLOCK_SIZE-1; $ startBlock = GetInt4d ($ d, START_BLOCK_POS); $ size = GetInt4d ($ d, SIZE_POS ); $ name = ''; for ($ I = 0; $ I <$ nameSize; $ I ++) {$ name. = $ d [$ I];} $ name = str_replace ("\ x00", "", $ name ); $ this-> props [] = array ('name' => $ name, 'type' => $ type, 'startblock' => $ startBlock, 'SIZE' => $ Size); if ($ name = "Workbook") | ($ name = "Book ")) {$ this-> wrkbook = count ($ this-> props)-1;} if ($ name = "Root Entry ") {$ this-> rootentry = count ($ this-> props)-1;} // echo "name = $ name = \ n "; $ offset + = PROPERTY_STORAGE_BLOCK_SIZE;} function getWorkBook () {if ($ this-> props [$ this-> wrkbook] ['SIZE'] <SMALL_BLOCK_THRESHOLD) {// getSmallBlockStream (PropertyStorage ps) $ rootdata = $ thi S->__ readData ($ this-> props [$ this-> rootentry] ['startblock']); $ streamData = ''; $ block = $ this-> props [$ this-> wrkbook] ['startblock']; // $ count = 0; $ pos = 0; while ($ block! =-2) {$ pos = $ block * SMALL_BLOCK_SIZE; $ streamData. = substr ($ rootdata, $ pos, SMALL_BLOCK_SIZE); $ block = $ this-> smallBlockChain [$ block];} return $ streamData ;} else {$ numBlocks = $ this-> props [$ this-> wrkbook] ['SIZE']/BIG_BLOCK_SIZE; if ($ this-> props [$ this-> wrkbook] ['SIZE'] % BIG_BLOCK_SIZE! = 0) {$ numBlocks ++;} if ($ numBlocks = 0) return ''; // echo" numBlocks = $ numBlocks \ n "; // byte [] streamData = new byte [numBlocks * BIG_BLOCK_SIZE]; // print_r ($ this-> wrkbook); $ streamData = ''; $ block = $ this-> props [$ this-> wrkbook] ['startblock']; // $ count = 0; $ pos = 0; // echo "block = $ block"; while ($ block! =-2) {$ pos = ($ block + 1) * BIG_BLOCK_SIZE; $ streamData. = substr ($ this-> data, $ pos, BIG_BLOCK_SIZE); $ block = $ this-> bigBlockChain [$ block];} // echo 'stream '. $ streamData; return $ streamData ;}}?>
Reference: http://hovertree.com/h/bjaf/to3l3tjm.htm
Http://www.cnblogs.com/roucheng/p/phpmysql.html