java把excel文件导入数据库报错!!
public void getDate() {
List liststu = new ArrayList();
// 找到导入的文件
InputStream is = Date.class.getClassLoader().getResourceAsStream(
"N3单词分类表(1).xls");
try {
// 创建工作簿
Workbook wb = Workbook.getWorkbook(is);
// 创建工作表
jxl.Sheet sheet = wb.getSheet(0);
String content = null;
for (int i = 0; i < sheet.getRows(); i++) {
Characters characters = new Characters();
for (int j = 0; j < sheet.getColumns(); j++) {
content = sheet.getCell(j, i).getContents();
System.out.print(content);
if (characters.getId() == 0) {
characters.setId(Integer.parseInt(sheet.getCell(j, i)
.getContents().trim()));
continue;
}
if (characters.getPronunciation() == null) {
characters.setPronunciation(sheet.getCell(j, i)
.getContents());
continue;
}
if (characters.getCharacters() == null) {
characters.setCharacters(sheet.getCell(j, i)
.getContents());
continue;
}
if (characters.getMeaning() == null) {
characters
.setMeaning(sheet.getCell(j, i).getContents());
continue;
}
}
insert(characters);
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(Characters characters) {
String sql = "insert into characters values(" + characters.getId()
+ ",'" + characters.getPronunciation() + "','"
+ characters.getCharacters() + "','" + characters.getMeaning()
+ "')";
ConnCOnnection conn = new ConnCOnnection();
Connection con = conn.getConnect();
try {
Statement state = con.createStatement();
state.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:497)
at net.company.dao.Date.getDate(Date.java:41)
at net.company.server.ExcelServer.getDate(ExcelServer.java:10)
at net.company.servlet.ExcelServlet.ExcelToMySql(ExcelServlet.java:40)
at net.company.servlet.ExcelServlet.doPost(ExcelServlet.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
答案:字符串不能软换为数字类型.第一种exel应该有字符串,第二种你在程序中判断下当前要转换的是为空
if (characters.getId() == 0) {
characters.setId(Integer.parseInt(sheet.getCell(j, i).getContents().trim()));
sheet.getCell(j, i).getContents().得到的内容为空,所以会报这个错
其他:你先检查Excel 中id 那一列是否有空值(我觉得这种可能比较大),
如果没有你可以设断点单步调试,看看sheet.getCell(j, i).getContents().trim()的值是否为“” 不知道
上一个:excel导入sql问题
下一个:Java 怎么把服务器中的图片导出到excel(图片的路径保存在数据库中的)