How to achieve conversion from Excel to mysql in php

WBOY
Release: 2016-08-08 09:33:49
Original
941 people have browsed it

Since I have a certain understanding of PHP and MySQL, I have been eyeing the huge Excel database in the company and want to secretly "share" this confidential business data. While thinking about how to get it, I also considered how to convert it into a MySQL database. , I went to many PHP website forums to ask experts for advice, but there was no result. One day, I suddenly imagined, why not try this method? The result is really working, everything is OK. Now I will fully share my experience with everyone (maybe you have a better method)
1. First introduce the structure of this Excel database, and name it E.xls. There are 4 fields (the same goes for 40). The field names are a, b, c, d. Then decompose jm.xls:
1) Select all records in field a and select "Copy"
2) Create a new Excel database file, paste the copied data under a blank field (usually A), make sure other fields are blank, and save it as a.xls
3) Save a.xls as a.txt (the format is "Text file (tab delimited)")
4) The data of the other three fields in E.xls continues to generate b.txt, c.txt and d.txt according to steps 1 to 3.
​ ​ 2. After completing the above steps, you have completed most of the work. The following is done by php and mysql. It is very simple:
1) To put data in mysql, you must create a mysql database file named dbname (which must be consistent with the dbname in the PHP code), including the table tbname (which must be consistent with the tbname in the PHP code), and 4 fields inc char ( 100), adds char(100), PRi char(100), tel char(100), the size should be adjusted according to the field size in Excel, otherwise some values ​​may be lost.
2) The most important thing is to write the php code. The code is as follows:
------txt2mysql.php---------
$inc=file("a.txt");
$adds=file("b.txt");
$pri=file("c.txt");
$tel=file("d.txt");
$i=0;
mysql_connect();
while (strlen($inc[$i])>0)
{
$sql="insert into tbname values ​​('$inc[$i]','$adds[$i]','$pri[$i]','$tel[$i]')";
$do=mysql_db_query("dbname",$sql);
$i=$i+1;
echo '
';
}
$s="select * from tbname";
$gg=mysql_db_query("dbname",$s);
$n=mysql_num_rows($gg);
mysql_close();
echo '
';
echo "A total of ".$n." records were added";
?>

Note: a.txt, b.txt, c.txt, d.txt and txt2mysql.php must be in the same directory

The above introduces how to realize the conversion from Excel to mysql in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!