java excel 文件导入通用接口

图书推荐:Java面向对象编程 9787121025389 电子工业出版社

 


public class ExcelImport {
   
   /**
    * 消息模板的表头
    */
   public static String[] TITLES = {"用户ID","消息标题","消息内容"};
   
   public static List<String[]> getExcelData(MultipartFile file) {
      List<String[]> list = new ArrayList<String[]>();
      try {
         POIFSFileSystem pois = new POIFSFileSystem(file.getInputStream());
         // 新建WorkBook
         HSSFWorkbook wb = new HSSFWorkbook(pois);
         // 获取Sheet(工作薄)总个数
         int sheetNumber = wb.getNumberOfSheets();
         for (int i = 0; i < sheetNumber; i++) {
            // 获取Sheet(工作薄)
            HSSFSheet sheet = wb.getSheetAt(i);
            // 开始行数
            int firstRow = sheet.getFirstRowNum();
            // 结束行数
            int lastRow = sheet.getLastRowNum();
            // 判断该Sheet(工作薄)是否为空
            boolean isEmpty = false;
            if (firstRow == lastRow) {
               isEmpty = true;
            }

            if (!isEmpty) {
               for (int j = firstRow + 1; j <= lastRow; j++) {
                  // 获取一行
                  HSSFRow row = sheet.getRow(j);
                  // 开始列数
                  int firstCell = row.getFirstCellNum();
                  // 结束列数
                  int lastCell = row.getLastCellNum();
                  // 判断该行是否为空
                  String[] value = new String[lastCell];
                  if (firstCell != lastCell) {
                     for (int k = firstCell; k < lastCell; k++) {

                        // 获取一个单元格
                        HSSFCell cell = row.getCell(k);
                        Object str = null;
                        // 获取单元格,值的类型
                        int cellType = cell.getCellType();

                        if (cellType == 0) {
                           java.text.DecimalFormat df = new DecimalFormat("########");
                           str = df.format(cell.getNumericCellValue());
                        } else if (cellType == 1) {
                           str = cell.getStringCellValue();
                        } else if (cellType == 2) {
                        } else if (cellType == 4) {
                           str = cell.getBooleanCellValue();
                        }
                        value[k] = (String) str;
                     }

                  }
                  // 每一行循环完对应的就是一个用户故事的所有属性全部拿到
                  list.add(value);
               }

            }

         }
      } catch (IOException e) {

         e.printStackTrace();
      }
      return list;
   }

   /**
    *
    * @param file
    * @param startRow 开始行 0开始
    * @param endRow 结束行 0开始 不包含
    * @param startCell 开始列 0开始
    * @param endCell 结束列 0开始 不包含
    * @param sheetAt 第一个sheet 0开始
    * @return
    */
   public static List<String[]> getExcelData(MultipartFile file, Integer startRow, Integer endRow, Integer startCell, Integer endCell, int sheetAt) {
      List<String[]> list = new ArrayList<String[]>();
      try {
         Workbook wb = WorkbookFactory.create(file.getInputStream());//适配针对excel 2003 和 excel 2007
         // 获取Sheet(工作薄)
         Sheet sheet = wb.getSheetAt(sheetAt);
         // 开始行数
         int firstRow = sheet.getFirstRowNum();
         if(startRow != null){
            firstRow = startRow;
         }
         // 结束行数
         int lastRow = sheet.getLastRowNum()+1;
         if(endRow != null){
            lastRow = endRow;
         }
         // 判断该Sheet(工作薄)是否为空
         boolean isEmpty = false;
         if (firstRow == lastRow) {
            isEmpty = true;
         }

         if (!isEmpty) {
            for (int j = firstRow; j < lastRow; j++) {
               // 获取一行
//                XSSFRow row = sheet.getRow(j);
               Row row=sheet.getRow(j);
               if(row == null) {
                  throw new Exception("第" + (j + 1) + "行为空");
               }
               // 开始列数
               int firstCell = row.getFirstCellNum();
               if(startCell != null){
                  firstCell = startCell;
               }
               // 结束列数
               int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells();
               if(endCell != null){
                  coloumNum = endCell;
               }
               int lastCell = row.getLastCellNum();
               // 判断该行是否为空
               String[] value = new String[coloumNum];
               if (firstCell != lastCell) {
                  for (int k = firstCell; k < coloumNum; k++) {
                     // 获取一个单元格
                     Cell cell = row.getCell(k);
                     if(cell==null){
                        value[k] = "";
                        continue;
                     }
                     cell.setCellType(Cell.CELL_TYPE_STRING);
                     value[k] = cell.getStringCellValue();
                  }

               }
               // 每一行循环完对应的就是一个用户故事的所有属性全部拿到
               list.add(value);
            }

         }
      } catch (IOException e) {
         try {
            throw new Exception("非法模板{表头和模板表头不一致}")  ;
         } catch (Exception e1) {
            //e1.printStackTrace();
            log.error("非法模板{表头和模板表头不一致");
         }
      }catch (Exception e){
         try {
            throw new Exception("模板不符合要求,请下载模板");
         } catch (Exception e1) {
            //e1.printStackTrace();
            log.error("模板不符合要求,请下载模板");
         }
      }
      return list;
   }
   
   
   private static boolean isValidTitle(Row row) {
      // 开始列数
      int firstCell = row.getFirstCellNum();
      int lastCell = row.getLastCellNum();
      // 判断该行是否为空
      if (firstCell != lastCell) {
         for (int i = firstCell; i < lastCell; i++) {
            
            // 获取一个单元格
            Cell cell = row.getCell(i);
            if(cell == null){
               return false;
            }
            Object str = null;
            // 获取单元格,值的类型
            int cellType = cell.getCellType();
             if (cellType == 1) {
               str = cell.getStringCellValue();
            } 
            
             if(!StringUtils.equalsIgnoreCase(TITLES[i], str.toString().trim())) {
                return false;
             }
         }
         return true;
      } else {
         return false;
      }
      
   }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值