


PHP import excel data to MYSQL mysql import excel file java excel import mysql mysql import excel
Here is an example of directly importing excel files into mysql. I spent one night testing it, and there was no garbled code when importing simplified or traditional Chinese. It was very easy to use.
PHP-ExcelReader, download address: http://sourceforge.net/projects/phpexcelreader
Instructions:
Test environment: MYSQL database uses utf8 encoding. The imported EXCEL document is in xls format. After testing, xlsx format [excel 2007] is also OK .
The red markings in the text are areas that need attention. Please replace them with your configured data, such as database configuration, etc. Run http://localost/test.php to import.
The following is the detailed code I posted, in which test.php is the test file I wrote, and the reader.php and oleread.inc files are downloaded from the URL provided above.
1. test.php
The code is as follows:
<?<span>php </span><span>require_once</span> './includes/reader.php'<span>; </span><span>//</span><span> ExcelFile($filename, $encoding); </span><span>$data</span> = <span>new</span><span> Spreadsheet_Excel_Reader(); </span><span>//</span><span> Set output Encoding. </span><span>$data</span>->setOutputEncoding('gbk'<span>); </span><span>//</span><span>”data.xls”是指要导入到mysql中的excel文件 </span><span>$data</span>->read('date.xls'<span>); @ </span><span>$db</span> = <span>mysql_connect</span>('localhost', 'root', '1234'<span>) or </span><span>die</span>("Could not connect to database.");<span>//</span><span>连接数据库 </span><span>mysql_query</span>("set names 'gbk'");<span>//</span><span>输出中文 </span><span>mysql_select_db</span>('wenhuaedu'); <span>//</span><span>选择数据库 </span><span>error_reporting</span>(<span>E_ALL</span> ^ <span>E_NOTICE</span><span>); </span><span>for</span> (<span>$i</span> = 1; <span>$i</span> <= <span>$data</span>->sheets[0]['numRows']; <span>$i</span>++<span>) { </span><span>//</span><span>以下注释的for循环打印excel表数据 </span><span>/*</span><span> for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { echo """.$data->sheets[0]['cells'][$i][$j]."","; } echo "n"; </span><span>*/</span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span><span>//</span><span>以下代码是将excel表数据【3个字段】插入到mysql中,根据你的excel表字段的多少,改写以下代码吧! </span><span>$sql</span> = "INSERT INTO test VALUES('". <span>$data</span>->sheets[0]['cells'][<span>$i</span>][1]."','". <span>$data</span>->sheets[0]['cells'][<span>$i</span>][2]."','". <span>$data</span>->sheets[0]['cells'][<span>$i</span>][3]."')"<span>; </span><span>echo</span><span>$sql</span>.'<br />'<span>; </span><span>$res</span> = <span>mysql_query</span>(<span>$sql</span><span>); </span>?>
Contained file
OLERead.php
<?<span>php </span><span>define</span>('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c<span>); </span><span>define</span>('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c<span>); </span><span>define</span>('ROOT_START_BLOCK_POS', 0x30<span>); </span><span>define</span>('BIG_BLOCK_SIZE', 0x200<span>); </span><span>define</span>('SMALL_BLOCK_SIZE', 0x40<span>); </span><span>define</span>('EXTENSION_BLOCK_POS', 0x44<span>); </span><span>define</span>('NUM_EXTENSION_BLOCK_POS', 0x48<span>); </span><span>define</span>('PROPERTY_STORAGE_BLOCK_SIZE', 0x80<span>); </span><span>define</span>('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c<span>); </span><span>define</span>('SMALL_BLOCK_THRESHOLD', 0x1000<span>); </span><span>//</span><span> property storage offsets </span><span>define</span>('SIZE_OF_NAME_POS', 0x40<span>); </span><span>define</span>('TYPE_POS', 0x42<span>); </span><span>define</span>('START_BLOCK_POS', 0x74<span>); </span><span>define</span>('SIZE_POS', 0x78<span>); </span><span>define</span>('IDENTIFIER_OLE', <span>pack</span>("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1<span>)); </span><span>//</span><span>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 </span><span>function</span> GetInt4d(<span>$data</span>, <span>$pos</span><span>) { </span><span>$value</span> = <span>ord</span>(<span>$data</span>[<span>$pos</span>]) | (<span>ord</span>(<span>$data</span>[<span>$pos</span>+1]) << 8) | (<span>ord</span>(<span>$data</span>[<span>$pos</span>+2]) << 16) | (<span>ord</span>(<span>$data</span>[<span>$pos</span>+3]) << 24<span>); </span><span>if</span> (<span>$value</span>>=4294967294<span>) { </span><span>$value</span>=-2<span>; } </span><span>return</span><span>$value</span><span>; } </span><span>class</span><span> OLERead { </span><span>var</span><span>$data</span> = ''<span>; </span><span>function</span><span> OLERead(){ } </span><span>function</span> read(<span>$sFileName</span><span>){ </span><span>//</span><span> check if file exist and is readable (Darko Miljanovic) </span><span>if</span>(!<span>is_readable</span>(<span>$sFileName</span><span>)) { </span><span>$this</span>->error = 1<span>; </span><span>return</span><span>false</span><span>; } </span><span>$this</span>->data = @<span>file_get_contents</span>(<span>$sFileName</span><span>); </span><span>if</span> (!<span>$this</span>-><span>data) { </span><span>$this</span>->error = 1<span>; </span><span>return</span><span>false</span><span>; } </span><span>//</span><span>echo IDENTIFIER_OLE; //echo 'start'; </span><span>if</span> (<span>substr</span>(<span>$this</span>->data, 0, 8) !=<span> IDENTIFIER_OLE) { </span><span>$this</span>->error = 1<span>; </span><span>return</span><span>false</span><span>; } </span><span>$this</span>->numBigBlockDepotBlocks = GetInt4d(<span>$this</span>->data,<span> NUM_BIG_BLOCK_DEPOT_BLOCKS_POS); </span><span>$this</span>->sbdStartBlock = GetInt4d(<span>$this</span>->data,<span> SMALL_BLOCK_DEPOT_BLOCK_POS); </span><span>$this</span>->rootStartBlock = GetInt4d(<span>$this</span>->data,<span> ROOT_START_BLOCK_POS); </span><span>$this</span>->extensionBlock = GetInt4d(<span>$this</span>->data,<span> EXTENSION_BLOCK_POS); </span><span>$this</span>->numExtensionBlocks = GetInt4d(<span>$this</span>->data,<span> NUM_EXTENSION_BLOCK_POS); </span><span>/*</span><span> echo $this->numBigBlockDepotBlocks." "; echo $this->sbdStartBlock." "; echo $this->rootStartBlock." "; echo $this->extensionBlock." "; echo $this->numExtensionBlocks." "; </span><span>*/</span><span>//</span><span>echo "sbdStartBlock = $this->sbdStartBlock\n"; </span><span>$bigBlockDepotBlocks</span> = <span>array</span><span>(); </span><span>$pos</span> =<span> BIG_BLOCK_DEPOT_BLOCKS_POS; </span><span>//</span><span> echo "pos = $pos"; </span><span>$bbdBlocks</span> = <span>$this</span>-><span>numBigBlockDepotBlocks; </span><span>if</span> (<span>$this</span>->numExtensionBlocks != 0<span>) { </span><span>$bbdBlocks</span> = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4<span>; } </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>$bbdBlocks</span>; <span>$i</span>++<span>) { </span><span>$bigBlockDepotBlocks</span>[<span>$i</span>] = GetInt4d(<span>$this</span>->data, <span>$pos</span><span>); </span><span>$pos</span> += 4<span>; } </span><span>for</span> (<span>$j</span> = 0; <span>$j</span> < <span>$this</span>->numExtensionBlocks; <span>$j</span>++<span>) { </span><span>$pos</span> = (<span>$this</span>->extensionBlock + 1) *<span> BIG_BLOCK_SIZE; </span><span>$blocksToRead</span> = <span>min</span>(<span>$this</span>->numBigBlockDepotBlocks - <span>$bbdBlocks</span>, BIG_BLOCK_SIZE / 4 - 1<span>); </span><span>for</span> (<span>$i</span> = <span>$bbdBlocks</span>; <span>$i</span> < <span>$bbdBlocks</span> + <span>$blocksToRead</span>; <span>$i</span>++<span>) { </span><span>$bigBlockDepotBlocks</span>[<span>$i</span>] = GetInt4d(<span>$this</span>->data, <span>$pos</span><span>); </span><span>$pos</span> += 4<span>; } </span><span>$bbdBlocks</span> += <span>$blocksToRead</span><span>; </span><span>if</span> (<span>$bbdBlocks</span> < <span>$this</span>-><span>numBigBlockDepotBlocks) { </span><span>$this</span>->extensionBlock = GetInt4d(<span>$this</span>->data, <span>$pos</span><span>); } } </span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span><span>//</span><span> var_dump($bigBlockDepotBlocks); // readBigBlockDepot </span><span>$pos</span> = 0<span>; </span><span>$index</span> = 0<span>; </span><span>$this</span>->bigBlockChain = <span>array</span><span>(); </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>$this</span>->numBigBlockDepotBlocks; <span>$i</span>++<span>) { </span><span>$pos</span> = (<span>$bigBlockDepotBlocks</span>[<span>$i</span>] + 1) *<span> BIG_BLOCK_SIZE; </span><span>//</span><span>echo "pos = $pos"; </span><span>for</span> (<span>$j</span> = 0 ; <span>$j</span> < BIG_BLOCK_SIZE / 4; <span>$j</span>++<span>) { </span><span>$this</span>->bigBlockChain[<span>$index</span>] = GetInt4d(<span>$this</span>->data, <span>$pos</span><span>); </span><span>$pos</span> += 4<span> ; </span><span>$index</span>++<span>; } } </span><span>//</span><span>var_dump($this->bigBlockChain); //echo '=====2'; // readSmallBlockDepot(); </span><span>$pos</span> = 0<span>; </span><span>$index</span> = 0<span>; </span><span>$sbdBlock</span> = <span>$this</span>-><span>sbdStartBlock; </span><span>$this</span>->smallBlockChain = <span>array</span><span>(); </span><span>while</span> (<span>$sbdBlock</span> != -2<span>) { </span><span>$pos</span> = (<span>$sbdBlock</span> + 1) *<span> BIG_BLOCK_SIZE; </span><span>for</span> (<span>$j</span> = 0; <span>$j</span> < BIG_BLOCK_SIZE / 4; <span>$j</span>++<span>) { </span><span>$this</span>->smallBlockChain[<span>$index</span>] = GetInt4d(<span>$this</span>->data, <span>$pos</span><span>); </span><span>$pos</span> += 4<span>; </span><span>$index</span>++<span>; } </span><span>$sbdBlock</span> = <span>$this</span>->bigBlockChain[<span>$sbdBlock</span><span>]; } </span><span>//</span><span> readData(rootStartBlock) </span><span>$block</span> = <span>$this</span>-><span>rootStartBlock; </span><span>$pos</span> = 0<span>; </span><span>$this</span>->entry = <span>$this</span>->__readData(<span>$block</span><span>); </span><span>/*</span><span> while ($block != -2) { $pos = ($block + 1) * BIG_BLOCK_SIZE; $this->entry = $this->entry.substr($this->data, $pos, BIG_BLOCK_SIZE); $block = $this->bigBlockChain[$block]; } </span><span>*/</span><span>//</span><span>echo '==='.$this->entry."==="; </span><span>$this</span>-><span>__readPropertySets(); } </span><span>function</span> __readData(<span>$bl</span><span>) { </span><span>$block</span> = <span>$bl</span><span>; </span><span>$pos</span> = 0<span>; </span><span>$data</span> = ''<span>; </span><span>while</span> (<span>$block</span> != -2<span>) { </span><span>$pos</span> = (<span>$block</span> + 1) *<span> BIG_BLOCK_SIZE; </span><span>$data</span> = <span>$data</span>.<span>substr</span>(<span>$this</span>->data, <span>$pos</span>,<span> BIG_BLOCK_SIZE); </span><span>//</span><span>echo "pos = $pos data=$data\n"; </span><span>$block</span> = <span>$this</span>->bigBlockChain[<span>$block</span><span>]; } </span><span>return</span><span>$data</span><span>; } </span><span>function</span><span> __readPropertySets(){ </span><span>$offset</span> = 0<span>; </span><span>//</span><span>var_dump($this->entry); </span><span>while</span> (<span>$offset</span> < <span>strlen</span>(<span>$this</span>-><span>entry)) { </span><span>$d</span> = <span>substr</span>(<span>$this</span>->entry, <span>$offset</span>,<span> PROPERTY_STORAGE_BLOCK_SIZE); </span><span>$nameSize</span> = <span>ord</span>(<span>$d</span>[SIZE_OF_NAME_POS]) | (<span>ord</span>(<span>$d</span>[SIZE_OF_NAME_POS+1]) << 8<span>); </span><span>$type</span> = <span>ord</span>(<span>$d</span><span>[TYPE_POS]); </span><span>//</span><span>$maxBlock = strlen($d) / BIG_BLOCK_SIZE - 1; </span><span>$startBlock</span> = GetInt4d(<span>$d</span>,<span> START_BLOCK_POS); </span><span>$size</span> = GetInt4d(<span>$d</span>,<span> SIZE_POS); </span><span>$name</span> = ''<span>; </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>$nameSize</span> ; <span>$i</span>++<span>) { </span><span>$name</span> .= <span>$d</span>[<span>$i</span><span>]; } </span><span>$name</span> = <span>str_replace</span>("\x00", "", <span>$name</span><span>); </span><span>$this</span>->props[] = <span>array</span><span> ( </span>'name' => <span>$name</span>, 'type' => <span>$type</span>, 'startBlock' => <span>$startBlock</span>, 'size' => <span>$size</span><span>); </span><span>if</span> ((<span>$name</span> == "Workbook") || (<span>$name</span> == "Book"<span>)) { </span><span>$this</span>->wrkbook = <span>count</span>(<span>$this</span>->props) - 1<span>; } </span><span>if</span> (<span>$name</span> == "Root Entry"<span>) { </span><span>$this</span>->rootentry = <span>count</span>(<span>$this</span>->props) - 1<span>; } </span><span>//</span><span>echo "name ==$name=\n"; </span><span>$offset</span> +=<span> PROPERTY_STORAGE_BLOCK_SIZE; } } </span><span>function</span><span> getWorkBook(){ </span><span>if</span> (<span>$this</span>->props[<span>$this</span>->wrkbook]['size'] <<span> SMALL_BLOCK_THRESHOLD){ </span><span>//</span><span> getSmallBlockStream(PropertyStorage ps) </span><span>$rootdata</span> = <span>$this</span>->__readData(<span>$this</span>->props[<span>$this</span>->rootentry]['startBlock'<span>]); </span><span>$streamData</span> = ''<span>; </span><span>$block</span> = <span>$this</span>->props[<span>$this</span>->wrkbook]['startBlock'<span>]; </span><span>//</span><span>$count = 0; </span><span>$pos</span> = 0<span>; </span><span>while</span> (<span>$block</span> != -2<span>) { </span><span>$pos</span> = <span>$block</span> *<span> SMALL_BLOCK_SIZE; </span><span>$streamData</span> .= <span>substr</span>(<span>$rootdata</span>, <span>$pos</span>,<span> SMALL_BLOCK_SIZE); </span><span>$block</span> = <span>$this</span>->smallBlockChain[<span>$block</span><span>]; } </span><span>return</span><span>$streamData</span><span>; }</span><span>else</span><span>{ </span><span>$numBlocks</span> = <span>$this</span>->props[<span>$this</span>->wrkbook]['size'] /<span> BIG_BLOCK_SIZE; </span><span>if</span> (<span>$this</span>->props[<span>$this</span>->wrkbook]['size'] % BIG_BLOCK_SIZE != 0<span>) { </span><span>$numBlocks</span>++<span>; } </span><span>if</span> (<span>$numBlocks</span> == 0) <span>return</span> ''<span>; </span><span>//</span><span>echo "numBlocks = $numBlocks\n"; //byte[] streamData = new byte[numBlocks * BIG_BLOCK_SIZE]; //print_r($this->wrkbook); </span><span>$streamData</span> = ''<span>; </span><span>$block</span> = <span>$this</span>->props[<span>$this</span>->wrkbook]['startBlock'<span>]; </span><span>//</span><span>$count = 0; </span><span>$pos</span> = 0<span>; </span><span>//</span><span>echo "block = $block"; </span><span>while</span> (<span>$block</span> != -2<span>) { </span><span>$pos</span> = (<span>$block</span> + 1) *<span> BIG_BLOCK_SIZE; </span><span>$streamData</span> .= <span>substr</span>(<span>$this</span>->data, <span>$pos</span>,<span> BIG_BLOCK_SIZE); </span><span>$block</span> = <span>$this</span>->bigBlockChain[<span>$block</span><span>]; } </span><span>//</span><span>echo 'stream'.$streamData; </span><span>return</span><span>$streamData</span><span>; } } } </span>?>
Reference: http://hovertree.com/h/bjaf/to3l3tjm.htm
http:// www.cnblogs.com/roucheng/p/phpmysql.html
The above introduces PHP to import excel data into MYSQL, including the content of importing EXCEL and Mysql. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.
