java:关于将EXCEL数据导入mysql数据库

入口:

public class importBespokeCustomer {
 
 MainPartimportBean mb = new MainPartimportBean();
 public void importExcel() throws FileNotFoundException{
  File file = new File("D:\\work.xlsx");
  if (file.isFile() && file.exists()) { //判断文件是否存在
         mb.insertDB(new FileInputStream(file));
     }
 }
 
 public void importsbdExcel() throws FileNotFoundException{
  File file = new File("D:\\work3.xlsx");
  if (file.isFile() && file.exists()) { //判断文件是否存在
         mb.insertsbdDB(new FileInputStream(file));
     }
 }
}

处理excel表格数据

public class MainPartimportBean{
 public void insertDB(InputStream fp) {
  try{
   Workbook workbook = null;
   workbook = new XSSFWorkbook(fp);
    /*     try {
          workbook = new XSSFWorkbook(fp);
         } catch (Exception ex) {
          workbook = new HSSFWorkbook(fp);
          ex.printStackTrace();
         }*/
         Sheet sheet = workbook.getSheetAt(0);// 得到工作表
   Row row = null;// 对应excel的行
   Cell cell = null;// 对应excel的列
   //String Var="";
   row = sheet.getRow((short)0);
   int totalRow = sheet.getLastRowNum();// 得到excel的总记录条数
   String besId = "";
   String suggect = "";
   String serial_number = "";
   Date report_date = null ;
   String report_unit = "";
   String inspect_unit = "";
   String sept9 ="";
   String shox2 ="";
   String ptger4 = "";
   String idh1 = "";
   String mg7 = "";
   String sncg = "";
   String cea = "";
   String afp = "";
   String feiAi="";
   String jiezhichangAi="";
   String weiAi="";
   String shiguanAi="";
   String ganAi="";
   
   for (short i = 1; i <=totalRow; i++) {
    MainBean mb = new MainBean();
    MainPart mp1 = new MainPart();
    row = sheet.getRow(i);
    cell = row.getCell((short)0);
    if(cell!=null)
    besId = cell.getRichStringCellValue().toString();
    mp1.setBesId(besId);
    
    cell = row.getCell((short)1);
    if(cell!=null)
     suggect =cell.getRichStringCellValue().toString();
    mp1.setSuggect(suggect);
    
    cell = row.getCell((short)2);
    if(cell!=null)
    serial_number =  cell.getRichStringCellValue().toString();
    mp1.setSerial_number(serial_number);
    
    cell = row.getCell((short)3);
    if(cell!=null)
     report_date = cell.getDateCellValue();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String format = sdf.format(report_date);
    //String replace_date= report_date.replace("/", "-");
    mp1.setReport_date(format);
    
    cell = row.getCell((short)4);
    if(cell!=null)
     report_unit =  cell.getRichStringCellValue().toString();
    mp1.setReport_unit(report_unit);
    
    cell = row.getCell((short)5);
    if(cell!=null)
     inspect_unit =  cell.getRichStringCellValue().toString();
    mp1.setInspect_unit(inspect_unit);
    
    cell = row.getCell((short)6);
    if(cell!=null)
     sept9 =  String.valueOf((int) cell.getNumericCellValue());
    mp1.setSept9(sept9);
    
    cell = row.getCell((short)7);
    if(cell!=null)
     shox2 =  String.valueOf((int) cell.getNumericCellValue());
    mp1.setShox2(shox2);
    
    cell = row.getCell((short)8);
    if(cell!=null)
     ptger4 =  String.valueOf((int) cell.getNumericCellValue());
    mp1.setPtger4(ptger4);
    
    cell = row.getCell((short)9);
    if(cell!=null)
     idh1 =  String.valueOf((int) cell.getNumericCellValue());
    mp1.setIdh1(idh1);
    
    cell = row.getCell((short)10);
    if(cell!=null)
     mg7 =  String.valueOf((int) cell.getNumericCellValue());
    mp1.setMg7(mg7);
    
    cell = row.getCell((short)11);
    if(cell!=null)
     sncg =  String.valueOf((int) cell.getNumericCellValue());
    mp1.setSncg(sncg);
    
    cell = row.getCell((short)12);
    if(cell!=null)
     cea =  String.valueOf((int) cell.getNumericCellValue());
    mp1.setCea(cea);
    
    cell = row.getCell((short)13);
    if(cell!=null)
     afp =  String.valueOf((int) cell.getNumericCellValue());
    mp1.setAfp(afp);
    
    cell = row.getCell((short)14);
    if(cell!=null)
     feiAi =  cell.getRichStringCellValue().toString();
    mp1.setFeiAi(feiAi);
    
    cell = row.getCell((short)15);
    if(cell!=null)
     jiezhichangAi =  cell.getRichStringCellValue().toString();
    mp1.setJiezhichangAi(jiezhichangAi);
    
    cell = row.getCell((short)16);
    if(cell!=null)
     weiAi =  cell.getRichStringCellValue().toString();
    mp1.setWeiAi(weiAi);
    
    cell = row.getCell((short)17);
    if(cell!=null)
     shiguanAi =  cell.getRichStringCellValue().toString();
    mp1.setShiguanAi(shiguanAi);
    
    cell = row.getCell((short)18);
    if(cell!=null)
     ganAi =  cell.getRichStringCellValue().toString();
    mp1.setGanAi(ganAi);
    
    mb.newMainUpdata(mp1);
   }
 
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

实体类:
public class MainPart{
 private String besId;
 private String suggect;
 private String checkAmount;
 private String detectonResult;
 private String serial_number;
 private String report_date;
 private String report_unit;
 private String inspect_unit;
 private String sept9;
 public String getCheckAmount() {
  return checkAmount;
 }
 public void setCheckAmount(String checkAmount) {
  this.checkAmount = checkAmount;
 }
 public String getDetectonResult() {
  return detectonResult;
 }
 public void setDetectonResult(String detectonResult) {
  this.detectonResult = detectonResult;
 }
 private String shox2;
 private String ptger4;
 private String idh1;
 private String mg7;
 private String sncg;
 private String cea;
 private String afp;
 private String feiAi;
 private String jiezhichangAi;
 private String weiAi;
 private String shiguanAi;
 private String ganAi;
 public String getBesId() {
  return besId;
 }
 public void setBesId(String besId) {
  this.besId = besId;
 }
 public String getSuggect() {
  return suggect;
 }
 public void setSuggect(String suggect) {
  this.suggect = suggect;
 }
 public String getSerial_number() {
  return serial_number;
 }
 public void setSerial_number(String serial_number) {
  this.serial_number = serial_number;
 }
 public String getReport_date() {
  return report_date;
 }
 public void setReport_date(String report_date) {
  this.report_date = report_date;
 }
 public String getReport_unit() {
  return report_unit;
 }
 public void setReport_unit(String report_unit) {
  this.report_unit = report_unit;
 }
 public String getInspect_unit() {
  return inspect_unit;
 }
 public void setInspect_unit(String inspect_unit) {
  this.inspect_unit = inspect_unit;
 }
 public String getSept9() {
  return sept9;
 }
 public void setSept9(String sept9) {
  this.sept9 = sept9;
 }
 public String getShox2() {
  return shox2;
 }
 public void setShox2(String shox2) {
  this.shox2 = shox2;
 }
 public String getPtger4() {
  return ptger4;
 }
 public void setPtger4(String ptger4) {
  this.ptger4 = ptger4;
 }
 public String getIdh1() {
  return idh1;
 }
 public void setIdh1(String idh1) {
  this.idh1 = idh1;
 }
 public String getMg7() {
  return mg7;
 }
 public void setMg7(String mg7) {
  this.mg7 = mg7;
 }
 public String getSncg() {
  return sncg;
 }
 public void setSncg(String sncg) {
  this.sncg = sncg;
 }
 public String getCea() {
  return cea;
 }
 public void setCea(String cea) {
  this.cea = cea;
 }
 public String getAfp() {
  return afp;
 }
 public void setAfp(String afp) {
  this.afp = afp;
 }
 public String getFeiAi() {
  return feiAi;
 }
 public void setFeiAi(String feiAi) {
  this.feiAi = feiAi;
 }
 public String getJiezhichangAi() {
  return jiezhichangAi;
 }
 public void setJiezhichangAi(String jiezhichangAi) {
  this.jiezhichangAi = jiezhichangAi;
 }
 public String getWeiAi() {
  return weiAi;
 }
 public void setWeiAi(String weiAi) {
  this.weiAi = weiAi;
 }
 public String getShiguanAi() {
  return shiguanAi;
 }
 public void setShiguanAi(String shiguanAi) {
  this.shiguanAi = shiguanAi;
 }
 public String getGanAi() {
  return ganAi;
 }
 public void setGanAi(String ganAi) {
  this.ganAi = ganAi;
 }
 @Override
 public String toString() {
  return "MainPart [besId=" + besId + ", suggect=" + suggect
    + ", checkAmount=" + checkAmount + ", detectonResult="
    + detectonResult + ", serial_number=" + serial_number
    + ", report_date=" + report_date + ", report_unit="
    + report_unit + ", inspect_unit=" + inspect_unit + ", sept9="
    + sept9 + ", shox2=" + shox2 + ", ptger4=" + ptger4 + ", idh1="
    + idh1 + ", mg7=" + mg7 + ", sncg=" + sncg + ", cea=" + cea
    + ", afp=" + afp + ", feiAi=" + feiAi + ", jiezhichangAi="
    + jiezhichangAi + ", weiAi=" + weiAi + ", shiguanAi="
    + shiguanAi + ", ganAi=" + ganAi + ", getCheckAmount()="
    + getCheckAmount() + ", getDetectonResult()="
    + getDetectonResult() + ", getBesId()=" + getBesId()
    + ", getSuggect()=" + getSuggect() + ", getSerial_number()="
    + getSerial_number() + ", getReport_date()=" + getReport_date()
    + ", getReport_unit()=" + getReport_unit()
    + ", getInspect_unit()=" + getInspect_unit() + ", getSept9()="
    + getSept9() + ", getShox2()=" + getShox2() + ", getPtger4()="
    + getPtger4() + ", getIdh1()=" + getIdh1() + ", getMg7()="
    + getMg7() + ", getSncg()=" + getSncg() + ", getCea()="
    + getCea() + ", getAfp()=" + getAfp() + ", getFeiAi()="
    + getFeiAi() + ", getJiezhichangAi()=" + getJiezhichangAi()
    + ", getWeiAi()=" + getWeiAi() + ", getShiguanAi()="
    + getShiguanAi() + ", getGanAi()=" + getGanAi()
    + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
    + ", toString()=" + super.toString() + "]";
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值