求助java读取excel文件,再写到数据库中遇到了问题!
我想用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,不知道为什么??
大家能帮帮我吗?我在这里先谢谢大家了!
[解决办法]
没用过,帮顶
建议看文档,或者FAQ
[解决办法]
HSSFRow row = sheet.getRow(i);才是得到一行的数据
row.getCell((short) 1);是得到一个单元格的数据
最后一行数据为空,那么他循环的次数不就只有66次吗?
[解决办法]
现在的问题是如果第67(最后一列)行数据为空,则输出66,若不为空则输出为67,不知道为什么??
==================================================================
这个问题很奇怪,67是最后一列还是最后一行? "67(最后一列)行 "是什么意思啊?
cell[i][67]?????
System.out.println( "chang du shi : "+cell.length);
=========================================================
你最后一列为空,这里不就是66,lz到底什么意思,郁闷,现在我得理解能力怎么退化的厉害
[解决办法]
晕!好长哦没看明白!
[解决办法]
把Word/Excel/Power Point嵌入到Java程序内
http://blog.csdn.net/bovy/archive/2007/04/06/1554447.aspx