You cannot get a string value from a numeric cell 问题报错提示。
解决办法请见下面红色字体内容!
在读取excle表格存入数据库时候出现了这个问题package db;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Excel {
public static DbConn DB = new DbConn();
public static void main(String[] args) throws Exception {
int num = 5;
int i = 0;
int b = 0;
int j = 0;
try {
Connection con = DB.getConnection();
String sql = "insert into b(v_area_name,v_phonenum,v_month) values(?,?,?)";
PreparedStatement pre = null;
pre = con.prepareStatement(sql);
List list = new ArrayList();
InputStream in = new FileInputStream("d:/c.xls");
HSSFWorkbook jlw = new HSSFWorkbook(in);
HSSFSheet sh = jlw.getSheetAt(1);
if (sh == null) {
in.close();
} else {
int number = sh.getPhysicalNumberOfRows();
if (number < 1) {
in.close();
} else {
HSSFRow row = null;
try{
while (i < number) {
for(;i<number;i++){
row = sh.getRow(i);
pre.setString(1, row.getCell((short)0).getStringCellValue());
pre.setString(2, String.valueOf((long)row.getCell((short)1).getNumericCellValue()));//这样就可以解决了,如果数据比较小,用int也行
pre.setString(3,"200909");
pre.addBatch();
j++;
if (j % num == 0) {
pre.executeBatch();
pre.clearParameters();
pre.clearBatch();
b = number - j;
}
}
}
}catch(Exception e){
e.printStackTrace();
}
if (b < num) {
System.out.println(b);
pre.executeBatch();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}