我想用javaexcel api读取excel文件,再写到mysql数据库中遇到了问题!
其中excel中有67个字段,第一行为表头,所以从第二行读取,依次写到数据库中。
程序代码如下:
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ReadXLS
{
public static void main(String args[])
{
try
{
//打开文件
Workbook book=
Workbook.getWorkbook(new File("测试.xls")); //获得第一个工作表对象
Sheet sheet=book.getSheet(0); System.out.println("共有"+sheet.getColumns()+"列!"); //输出 “共有6列”
System.out.println("共有"+sheet.getRows()+"行!"); //输出 “共有7行”
//要求xls模板和数据表字段要一一对应
Connection con=null;
Statement stmt=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();//加载驱动程序
String url="jdbc:mysql://localhost:3306/dhgl1";
con=DriverManager.getConnection(url,"root","root"); //建立连接
System.out.println("数据库连接成功,准备插入数据......");
stmt=con.createStatement();
String sql="";
for (int j=1;j<sheet.getRows();j++)//从第二行开始插入(第一行为表头)
{
Cell cell[]=sheet.getRow(j); //获取第j+1行数据
System.out.println("chang du shi : "+cell.length); //如果第67(最后一列)行数据为空,则输出66,若不为空则输出为67,不知道为什么??
for (int i=0;i<cell.length;i++)
cell[i]=sheet.getCell(i, j);//获取第i列第j行数据
//18-22 34 52
float cell18=Float.parseFloat(cell[18].getContents());
float cell19=Float.parseFloat(cell[19].getContents());
float cell20=Float.parseFloat(cell[20].getContents());
float cell21=Float.parseFloat(cell[21].getContents());
float cell22=Float.parseFloat(cell[22].getContents());
float cell34=Float.parseFloat(cell[34].getContents());
float cell52=Float.parseFloat(cell[52].getContents());
System.out.println("正在插入第"+(j+1)+"行数据......");
sql="insert into biyes values('"+cell[0].getContents()+"','"+cell[1].getContents()+"','"+cell[2].getContents()+
"','"+cell[3].getContents()+"','"+cell[4].getContents()+"','"+cell[5].getContents()+
"','"+cell[6].getContents()+"','"+cell[7].getContents()+"','"+cell[8].getContents()+
"','"+cell[9].getContents()+"','"+cell[10].getContents()+"','"+cell[11].getContents()+
"','"+cell[12].getContents()+"','"+cell[13].getContents()+"','"+cell[14].getContents()+
"','"+cell[15].getContents()+"','"+cell[16].getContents()+"','"+cell[17].getContents()+
"',"+cell18+","+cell19+","+cell20+
","+cell21+","+cell22+",'"+cell[23].getContents()+
"','"+cell[24].getContents()+"','"+cell[25].getContents()+"','"+cell[26].getContents()+
"','"+cell[27].getContents()+"','"+cell[28].getContents()+"','"+cell[29].getContents()+
"','"+cell[30].getContents()+"','"+cell[31].getContents()+"','"+cell[32].getContents()+
"','"+cell[33].getContents()+"',"+cell34+",'"+cell[35].getContents()+
"','"+cell[36].getContents()+"','"+cell[37].getContents()+"','"+cell[38].getContents()+
"','"+cell[39].getContents()+"','"+cell[40].getContents()+"','"+cell[41].getContents()+
"','"+cell[42].getContents()+"','"+cell[43].getContents()+"','"+cell[44].getContents()+
"','"+cell[45].getContents()+"','"+cell[46].getContents()+"','"+cell[47].getContents()+
"','"+cell[48].getContents()+"','"+cell[49].getContents()+"','"+cell[50].getContents()+
"','"+cell[51].getContents()+"',"+cell52+",'"+cell[53].getContents()+
"','"+cell[54].getContents()+"','"+cell[55].getContents()+"','"+cell[56].getContents()+
"','"+cell[57].getContents()+"','"+cell[58].getContents()+"','"+cell[59].getContents()+
"','"+cell[60].getContents()+"','"+cell[61].getContents()+"','"+cell[62].getContents()+
"','"+cell[63].getContents()+"','"+cell[64].getContents()+"','"+cell[65].getContents()+
"','"+cell[66].getContents()+"')";
System.out.println("字符串是: "+sql);
stmt.executeUpdate(sql);
}
// System.out.println("数据插入完成,正在关闭数据库......");
book.close();
//rs.close();
//stmt.close();
//con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
} 现在的问题是如果第67(最后一列)行数据为空,则输出66,若不为空则输出为67,不知道为什么??
大家能帮帮我吗?我在这里先谢谢大家了!
其中excel中有67个字段,第一行为表头,所以从第二行读取,依次写到数据库中。
程序代码如下:
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ReadXLS
{
public static void main(String args[])
{
try
{
//打开文件
Workbook book=
Workbook.getWorkbook(new File("测试.xls")); //获得第一个工作表对象
Sheet sheet=book.getSheet(0); System.out.println("共有"+sheet.getColumns()+"列!"); //输出 “共有6列”
System.out.println("共有"+sheet.getRows()+"行!"); //输出 “共有7行”
//要求xls模板和数据表字段要一一对应
Connection con=null;
Statement stmt=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();//加载驱动程序
String url="jdbc:mysql://localhost:3306/dhgl1";
con=DriverManager.getConnection(url,"root","root"); //建立连接
System.out.println("数据库连接成功,准备插入数据......");
stmt=con.createStatement();
String sql="";
for (int j=1;j<sheet.getRows();j++)//从第二行开始插入(第一行为表头)
{
Cell cell[]=sheet.getRow(j); //获取第j+1行数据
System.out.println("chang du shi : "+cell.length); //如果第67(最后一列)行数据为空,则输出66,若不为空则输出为67,不知道为什么??
for (int i=0;i<cell.length;i++)
cell[i]=sheet.getCell(i, j);//获取第i列第j行数据
//18-22 34 52
float cell18=Float.parseFloat(cell[18].getContents());
float cell19=Float.parseFloat(cell[19].getContents());
float cell20=Float.parseFloat(cell[20].getContents());
float cell21=Float.parseFloat(cell[21].getContents());
float cell22=Float.parseFloat(cell[22].getContents());
float cell34=Float.parseFloat(cell[34].getContents());
float cell52=Float.parseFloat(cell[52].getContents());
System.out.println("正在插入第"+(j+1)+"行数据......");
sql="insert into biyes values('"+cell[0].getContents()+"','"+cell[1].getContents()+"','"+cell[2].getContents()+
"','"+cell[3].getContents()+"','"+cell[4].getContents()+"','"+cell[5].getContents()+
"','"+cell[6].getContents()+"','"+cell[7].getContents()+"','"+cell[8].getContents()+
"','"+cell[9].getContents()+"','"+cell[10].getContents()+"','"+cell[11].getContents()+
"','"+cell[12].getContents()+"','"+cell[13].getContents()+"','"+cell[14].getContents()+
"','"+cell[15].getContents()+"','"+cell[16].getContents()+"','"+cell[17].getContents()+
"',"+cell18+","+cell19+","+cell20+
","+cell21+","+cell22+",'"+cell[23].getContents()+
"','"+cell[24].getContents()+"','"+cell[25].getContents()+"','"+cell[26].getContents()+
"','"+cell[27].getContents()+"','"+cell[28].getContents()+"','"+cell[29].getContents()+
"','"+cell[30].getContents()+"','"+cell[31].getContents()+"','"+cell[32].getContents()+
"','"+cell[33].getContents()+"',"+cell34+",'"+cell[35].getContents()+
"','"+cell[36].getContents()+"','"+cell[37].getContents()+"','"+cell[38].getContents()+
"','"+cell[39].getContents()+"','"+cell[40].getContents()+"','"+cell[41].getContents()+
"','"+cell[42].getContents()+"','"+cell[43].getContents()+"','"+cell[44].getContents()+
"','"+cell[45].getContents()+"','"+cell[46].getContents()+"','"+cell[47].getContents()+
"','"+cell[48].getContents()+"','"+cell[49].getContents()+"','"+cell[50].getContents()+
"','"+cell[51].getContents()+"',"+cell52+",'"+cell[53].getContents()+
"','"+cell[54].getContents()+"','"+cell[55].getContents()+"','"+cell[56].getContents()+
"','"+cell[57].getContents()+"','"+cell[58].getContents()+"','"+cell[59].getContents()+
"','"+cell[60].getContents()+"','"+cell[61].getContents()+"','"+cell[62].getContents()+
"','"+cell[63].getContents()+"','"+cell[64].getContents()+"','"+cell[65].getContents()+
"','"+cell[66].getContents()+"')";
System.out.println("字符串是: "+sql);
stmt.executeUpdate(sql);
}
// System.out.println("数据插入完成,正在关闭数据库......");
book.close();
//rs.close();
//stmt.close();
//con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
} 现在的问题是如果第67(最后一列)行数据为空,则输出66,若不为空则输出为67,不知道为什么??
大家能帮帮我吗?我在这里先谢谢大家了!
解决方案 »
- 学了java学什么数据库比较好?
- 求java基础知识练习题
- java中有几种方法可以实现一个线程?
- 请问java如何实现从硬盘读入一个图片,显示并且能够在原图片基础上进行修改?
- super的用法;
- 求教用ant执行sql语句时遇到的错误!
- 请问如何在j2se桌面程序中调用Excel的打印功能....在线等候...
- 日文系统中java 的路径怎么编译通过!
- 请问一下rowset是个什么东西,见内容
- JAVA要使用sql server 2000 该怎么做,将JAVA写成application吗? 有什么好书吗?
- 注意到手机上的键盘和不断变化的屏幕吗,大学知道这键盘是怎么和这么多的屏幕建立关系的呢??
- java中如何将一个字符串写入excel的指定位置
row.getCell((short) 1);是得到一个单元格的数据
最后一行数据为空,那么他循环的次数不就只有66次吗?
==================================================================
这个问题很奇怪,67是最后一列还是最后一行? "67(最后一列)行"是什么意思啊?
cell[i][67]?????
System.out.println("chang du shi : "+cell.length);
=========================================================
你最后一列为空,这里不就是66,lz到底什么意思,郁闷,现在我得理解能力怎么退化的厉害
(67列有表头呀?即使每一行的67列为空,则应该输出空值?可为什么会出现越界错误呢??)
cell[66].getContents()//输出第67列的值麻烦大家了,呵呵!不甚感激
谢谢!
http://blog.csdn.net/bovy/archive/2007/04/06/1554447.aspx