java将excel文件中的数据导入到mySql数据库中 完整代码

如题所述

//根据excel文件,生成输入流
FileInputStream fis = new FileInputStream(new File("D:\\aa.xlsx"));
//用输入流生成poi 对象,以读取excel内容
//excel2007以上用XSSFWorkbook,2003用HSSFWorkbook
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);//取第一个sheet
String str1 = sheet.getRow(0).getCell(0).getStringCellValue();//取第一行第一列字符值
//getDateCellValue();日期类型 getNumericCellValue()数字类型;getHyperlink()超链接 等等()
//接下来 选择性插入数据库吧,明白?追问

不明白
TestEntity entity = new TestEntity(); entity.setNum1(val[0]); entity.setNum2(val[1]); TestMethod method = new TestMethod(); 这个是啥啥意思 找不到 这是网上的 method.Add(entity);

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-20
String rpaht="C:\\Users\\temp\\Desktop\\合格.xls";
int addUnit_new=-1;
String[] excleAddToDB = new XLStoDB().ExcleAddToDB(rpaht);
System.out.println("长度:"+excleAddToDB.length);
for (int j = 0; j < excleAddToDB.length; j++) {
String[] split = excleAddToDB[j].split(";");
int Category_id=-1;
int IndustryName_id=-1;
Unit unit=new Unit() ;//定义个对象
for (int i = 0; i < split.length; i++) {
if(i==0){//公司名称
unit.setName(split[i]) ;
}else if(i==1){//政区ID
unit.setDistrict_id(Integer.parseInt(split[i]));
}else if(i==2){//街道id
unit.setStreet_id(Integer.parseInt(split[i]));
}else if(i==3){//门牌号
unit.setHouseNumber(Integer.parseInt(split[i]));
}else if(i==4){//法人
unit.setLegalPerson(split[i]) ;
}else if(i==5){//电话
unit.setPhone(split[i]) ;
}else if(i==6){//行业id
String[] splitciid=split[i].split(",");
Category_id=Integer.parseInt(splitciid[0]);
IndustryName_id=Integer.parseInt(splitciid[1]);
System.out.println(excleAddToDB[i]);
}else if(i==7){//邮编
unit.setPostcode(Integer.parseInt(split[i])) ;
}else if(i==8){//注册时间
java.util.Date registerTime =null;
try{
registerTime=new SimpleDateFormat("yyyy-MM-dd").parse(split[i]);
}catch(Exception e){
e.printStackTrace();
}
unit.setRegisterTime(registerTime) ;
}else if(i==9){//备注
unit.setRemark(split[i]);
}else if(i==10){//经纬度
String[] splitlnglat = split[i].split(",");
unit.setLat(splitlnglat[1]);
unit.setLng(splitlnglat[0]);
}
}
InsertDateDAO dd=new InsertDateDAO();
addUnit_new = dd.addUnit_new(unit,IndustryName_id,Category_id);

}
第2个回答  2012-06-18
提供思路,excel文件用HSSFWork 操作,每一行对应数据库表一条记录,多行插入即可追问

我要具体代码

追答

相似回答