Maison >développement back-end >tutoriel php >PHP将EXCEL导入MYSQL的具体方法介绍_PHP教程
在网上搜了很多这方面的资料,发现都是将excel文件另存为csv文件,然后从csv文件导入。这里介绍一个直接将excel文件导入mysql的例子。我花了一晚上的时间测试,无论导入简繁体都不会出现乱码,非常好用。
PHP-ExcelReader,下载地址: http://sourceforge.net/projects/phpexcelreader
说明:
PHP将EXCEL导入MYSQL的测试环境:MYSQL数据库采用utf8编码.导入EXCEL文档是xls格式,经过测试,xlsx 格式[excel 2007]也OK.
文中红色标注为需要注意的地方,请替换成你配置好的数据,如数据库配置等。运行http://localost/test.php实现导入。
以下是我贴出的详细代码,其中test.php为我写的测试文件,reader.php和oleread.inc文件是从上面提供的网址中下载的。
1. PHP将EXCEL导入MYSQL的代码示例test.php
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>require_once 'reader.php'; </span></li> <li class="alt"><span>// ExcelFile($filename, $encoding); </span></li> <li> <span>$</span><span class="attribute">data</span><span> = </span><span class="attribute-value">new</span><span> Spreadsheet_Excel_Reader(); </span> </li> <li class="alt"><span>// Set output Encoding. </span></li> <li> <span>$data-</span><span class="tag">></span><span>setOutputEncoding('gbk'); </span> </li> <li class="alt"><span>//”data.xls”是指要导入到mysql中的excel文件 </span></li> <li> <span>$data-</span><span class="tag">></span><span>read('data.xls'); </span> </li> <li class="alt"> <span>@ $</span><span class="attribute">db</span><span> = </span><span class="attribute-value">mysql_connect</span><span>('localhost', 'root', '123456') or </span> </li> <li><span>die("Could not connect to database.");//连接数据库 </span></li> <li class="alt"><span>mysql_query("set names 'gbk'");//输出中文 </span></li> <li><span>mysql_select_db('mydb'); //选择数据库 </span></li> <li class="alt"><span>error_reporting(E_ALL ^ E_NOTICE); </span></li> <li> <span>for ($</span><span class="attribute">i</span><span> = </span><span class="attribute-value">1</span><span>; $i </span><span class="tag"><span>= $data-</span><span class="tag">></span><span>sheets[0]['numRows']; $i++) { </span></span> </li> <li class="alt"><span>//以下注释的for循环打印excel表数据 </span></li> <li><span>/* </span></li> <li class="alt"> <span>for ($</span><span class="attribute">j</span><span> = </span><span class="attribute-value">1</span><span>; $j </span><span class="tag"><span> = $data-</span><span class="tag">></span><span>sheets[0]['numCols']; $j++) { </span></span> </li> <li> <span>echo """.$data-</span><span class="tag">></span><span>sheets[0]['cells'][$i][$j]."","; </span> </li> <li class="alt"><span>} </span></li> <li><span>echo "n"; </span></li> <li class="alt"><span>*/ </span></li> <li><span>//以下代码是将excel表数据【3个字段】插入到mysql中,<br>根据你的excel表字段的多少,改写以下代码吧! </span></li> <li class="alt"> <span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"INSERT INTO test VALUES('"</span><span>. </span> </li> <li> <span>$data-</span><span class="tag">></span><span>sheets[0]['cells'][$i][1]."','". </span> </li> <li class="alt"> <span>$data-</span><span class="tag">></span><span>sheets[0]['cells'][$i][2]."','". </span> </li> <li> <span>$data-</span><span class="tag">></span><span>sheets[0]['cells'][$i][3]."')"; </span> </li> <li class="alt"> <span>echo $sql.'</span><span class="tag"><span> </span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>'; </span></span> </li> <li> <span>$</span><span class="attribute">res</span><span> = </span><span class="attribute-value">mysql_query</span><span>($sql); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
以上就是PHP将EXCEL导入MYSQL的相关方法介绍,希望多又需要的朋友有所帮助。