Home > Backend Development > PHP Tutorial > EXCEL文件导入MYSQL出错

EXCEL文件导入MYSQL出错

WBOY
Release: 2016-06-23 14:27:17
Original
1194 people have browsed it

本帖最后由 xuzuning 于 2013-11-14 13:36:39 编辑

在的表格很简单,就12列数据,要导入MYSQL
在网上找的代码,自己测试的文件和下载的文件都在一个文件夹里,
有四个文件1,data.xls  2.index.php  3,reader.php(下载的)  4,oleread.inc(下载的) 

data.xls表格里数据
num,names,years,season,groups,colornote,colornum,price,types,sizes,materials,note
有两行记录

index代码如下
<?require_once 'reader.php'; // ExcelFile($filename, $encoding); $data = new Spreadsheet_Excel_Reader(); // Set output Encoding. $data->setOutputEncoding('gbk'); //”data.xls”是指要导入到mysql中的excel文件 $data->read('data.xls'); @ $db = mysql_connect('localhost', 'root', '123123') or die("Could not connect to database.");//连接数据库 mysql_query("set names 'gbk'");//输出中文 mysql_select_db('test'); //选择数据库 error_reporting(E_ALL ^ E_NOTICE); for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {//以下注释的for循环打印excel表数据 /* for ($j = 1; $j < = $data->sheets[0]['numCols']; $j++) { echo "\"".$data->sheets[0]['cells'][$i][$j]."\","; } echo "\n"; */ //以下代码是将excel表数据【3个字段】插入到mysql中, //根据你的excel表字段的多少,改写以下代码吧! $sql = "INSERT INTO test (	num,names,years,season,groups,colornote,colornum,price,types,sizes,materials,note)VALUES('". $data->sheets[0]['cells'][$i][1]."','". $data->sheets[0]['cells'][$i][2]."','". $data->sheets[0]['cells'][$i][4]."','". $data->sheets[0]['cells'][$i][5]."','". $data->sheets[0]['cells'][$i][6]."','". $data->sheets[0]['cells'][$i][7]."','". $data->sheets[0]['cells'][$i][8]."','". $data->sheets[0]['cells'][$i][9]."','". $data->sheets[0]['cells'][$i][10]."','". $data->sheets[0]['cells'][$i][11]."','". $data->sheets[0]['cells'][$i][12]."')"; echo $sql.'< br />'; $res = mysql_query($sql); } ?>
Copy after login
 

运行index.php报以下错误

Fatal error: Call to a member function read() on a non-object in D:\AppServ\www\aaee\index.php on line 4

回复讨论(解决方案)

第 4 行  $data->read('data.xls');
这和 $data 是在哪里实例化的?

显然你也太粗心了,从网上复制的代码也不认真整理一下
那句 $data = new Spreadsheet_Excel_Reader(); 不还在第二行的注释中吗?

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