Java 通过Poi解析数据到EXCEL

傻瓜式操作步骤:你要是再看不懂,实在是无语了

1.前台界面FILE控件:

       //主要是这一行
      <input type="file" id="excelFile" name="excelFile" style="width:0px;" size="60" οnchange="copyFilepathvalue();" 

2.请求地址后,进入到Controller层,进入到方法

  MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  MultipartFile byteFile = multipartRequest.getFile("excelFile");
//就是前台界面控件的Name
  String originalFileName=byteFile.getOriginalFilename();
  InputStream is = byteFile.getInputStream(); 
//解析流
  templateAdapter = theChiefDispatch.getTemplateAdaptation("templateAdapter"); 
//不是重点,无需多看
  templateAdapter.twice_resolve(is,flowProcessTpl); //进入方法了  

 int columnStart=0;//列开始
 int rowStart=4;//行开始
 int columnSurplus = 1;//剩余不需要解析的列行
 int rowSurplus = 1;//剩余不需要解析的行数

public FlowProcessTpl twice_resolve(InputStream is) throws ExcelNotResolveException {
   Workbook wb;
    try {
    int len = is.available();
    byte[] excelFile = new byte[len];
    is.read(excelFile);
    ByteArrayInputStream bin= new ByteArrayInputStream(excelFile);
    wb = WorkbookFactory.create(bin);
    // 获取第一张工作表Sheet 
     Sheet sheet = wb.getSheetAt(0); 
     String title = sheet.getRow(0).getCell(0).getStringCellValue();
    
     flowProcess.setTpl(excelFile);  
//将这个EXCEL存放到数据库的BLOB中  实体属性为 byte[]类型的
   
  flowProcess.setReserve1(title);  //设置表头
     Row rowName = sheet.getRow(rowStart-3);//保存列信息

    解析列
       for(int colNum = columnStart + 3;colNum < rowName.getLastCellNum();colNum++){
        Cell cell = rowName.getCell(colNum);
                 
        if(colNum==rowName.getLastCellNum()-1){
         flowProcess.setAnaly_name(getValue(cell));
        }      
       Row rowScale = sheet.getRow(rowStart-2);
       int index = 0 ;
       for(int colNum = columnStart + 3;colNum < rowScale.getLastCellNum();colNum++){
        Cell cell = rowScale.getCell(colNum);
             
        if(colNum!=rowScale.getLastCellNum()-1){
         ColInfo colInfo = list.get(index++);
         if(StringUtils.isNotBlank(getValue(cell))){
          colInfo.setColScale(getValue(cell));
         }
        }
        
       }
       
       //保存描述信息
       Row rowDscribe = sheet.getRow(rowStart-1);
       index = 0;
       for(int colNum = columnStart + 3;colNum < rowDscribe.getLastCellNum();colNum++){
        Cell cell = rowDscribe.getCell(colNum);
        ColInfo colInfo=new ColInfo();
        if(cell == null){
         continue;
        }
        if(colNum==rowScale.getLastCellNum()-1){
         flowProcess.setAnaly_descri(getValue(cell));
        }else{
         colInfo = list.get(index++);
         colInfo.setDescribe(getValue(cell));
        }
        if(colNum!=rowScale.getLastCellNum()-1){
         colInfoService.saveColInfo(colInfo);
        }
         }
       
       Integer i = 0;
    
  // 循环行Row  
      for(int rowNum = rowStart; rowNum <= sheet.getLastRowNum()-rowSurplus; rowNum++){ 
        Row row = sheet.getRow( rowNum);  
       
 //分别要导入的三列
        Cell taskNameCell = row.getCell( columnStart);
        Cell explainCell = row.getCell( columnStart + 1 );
        Cell unitCell = row.getCell( columnStart + 2 );
         i++;
        }
       is.close();
        }   catch (Exception e){
    if(flowProcess.getId() != null ){
     flowProcessTplService.removeFlowProcessTpl(new String[]{flowProcess.getId().toString()});  //删除不正确的模板数据
    }
    ExcelNotResolveException ee=new ExcelNotResolveException("不支持的excel文件模板,请重新上传!");
    e.printStackTrace();
    if(e instanceof ExcelNotResolveException) ee= (ExcelNotResolveException)e;
    throw ee;
   } 
  }

下面为要导入模板的样式

 

 

为导入成功后,读取数据

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值