poi 合并单元格

  1. /** 
  2.      * 获取合并单元格的值 
  3.      *  
  4.      * @param sheet 
  5.      * @param row 
  6.      * @param column 
  7.      * @return 
  8.      */  
  9.     public static String getMergedRegionValue(Sheet sheet, int row, int column) {  
  10.         int sheetMergeCount = sheet.getNumMergedRegions();  
  11.   
  12.         for (int i = 0; i < sheetMergeCount; i++) {  
  13.             CellRangeAddress ca = sheet.getMergedRegion(i);  
  14.             int firstColumn = ca.getFirstColumn();  
  15.             int lastColumn = ca.getLastColumn();  
  16.             int firstRow = ca.getFirstRow();  
  17.             int lastRow = ca.getLastRow();  
  18.   
  19.             if (row >= firstRow && row <= lastRow) {  
  20.   
  21.                 if (column >= firstColumn && column <= lastColumn) {  
  22.                     Row fRow = sheet.getRow(firstRow);  
  23.                     Cell fCell = fRow.getCell(firstColumn);  
  24.                     return getCellValue(fCell);  
  25.                 }  
  26.             }  
  27.         }  
  28.   
  29.         return null;  
  30.     }  
  31.   
  32.     /** 
  33.      * 如果excel是wps格式,获取合并单元格的cell时,cell会是null,此时不能用该方法,请用getMergedRegionValue(Sheet sheet, int row, int column) 
  34.      * @description 
  35.      * @author liuzhenpeng 
  36.      * @date 2017年2月16日 
  37.      * @param sheet 
  38.      * @param cell 
    /** 
         * 获取合并单元格的值 
         *  
         * @param sheet 
         * @param row 
         * @param column 
         * @return 
         */  
        public static String getMergedRegionValue(Sheet sheet, int row, int column) {  
            int sheetMergeCount = sheet.getNumMergedRegions();  
      
            for (int i = 0; i < sheetMergeCount; i++) {  
                CellRangeAddress ca = sheet.getMergedRegion(i);  
                int firstColumn = ca.getFirstColumn();  
                int lastColumn = ca.getLastColumn();  
                int firstRow = ca.getFirstRow();  
                int lastRow = ca.getLastRow();  
      
                if (row >= firstRow && row <= lastRow) {  
      
                    if (column >= firstColumn && column <= lastColumn) {  
                        Row fRow = sheet.getRow(firstRow);  
                        Cell fCell = fRow.getCell(firstColumn);  
                        return getCellValue(fCell);  
                    }  
                }  
            }  
      
            return null;  
        }  
      
        /** 
         * 如果excel是wps格式,获取合并单元格的cell时,cell会是null,此时不能用该方法,请用getMergedRegionValue(Sheet sheet, int row, int column) 
         * @description 
         * @author liuzhenpeng 
         * @date 2017年2月16日 
         * @param sheet 
         * @param cell 
         * @return 
         */  
        public static String getMergedRegionValue(Sheet sheet, Cell cell) {  
            return getMergedRegionValue(sheet, cell.getRowIndex(),  
                    cell.getColumnIndex());  
        }  
      
        /** 
         * 判断合并了行 
         *  
         * @param sheet 
         * @param row 
         * @param column 
         * @return 
         */  
        public static boolean isMergedRow(Sheet sheet, int row, int column) {  
            int sheetMergeCount = sheet.getNumMergedRegions();  
            for (int i = 0; i < sheetMergeCount; i++) {  
                CellRangeAddress range = sheet.getMergedRegion(i);  
                int firstColumn = range.getFirstColumn();  
                int lastColumn = range.getLastColumn();  
                int firstRow = range.getFirstRow();  
                int lastRow = range.getLastRow();  
                if (row == firstRow && row == lastRow) {  
                    if (column >= firstColumn && column <= lastColumn) {  
                        return true;  
                    }  
                }  
            }  
            return false;  
        }  
      
        /** 
         * 判断指定的单元格是否是合并单元格 
         *  
         * @param sheet 
         *            工作表 
         * @param row 
         *            行下标 
         * @param column 
         *            列下标 
         * @return 
         */  
        public static boolean isMergedRegion(Sheet sheet, int row, int column) {  
            int sheetMergeCount = sheet.getNumMergedRegions();  
            for (int i = 0; i < sheetMergeCount; i++) {  
                CellRangeAddress range = sheet.getMergedRegion(i);  
                int firstColumn = range.getFirstColumn();  
                int lastColumn = range.getLastColumn();  
                int firstRow = range.getFirstRow();  
                int lastRow = range.getLastRow();  
                if (row >= firstRow && row <= lastRow) {  
                    if (column >= firstColumn && column <= lastColumn) {  
                        return true;  
                    }  
                }  
            }  
            return false;  
        }  
      
        /** 
         * 如果excel是wps格式,获取合并单元格的cell时,cell会是null,此时不能用该方法,请用isMergedRegion(Sheet sheet, int row, int column) 
         * @description 
         * @author liuzhenpeng 
         * @date 2017年2月16日 
         * @param sheet 
         * @param cell 
         * @return 
         */  
        public static boolean isMergedRegion(Sheet sheet, Cell cell) {  
            int row = cell.getRowIndex();  
            int column = cell.getColumnIndex();  
            return isMergedRegion(sheet, row, column);  
        }  
      
        public static boolean isCellInRegion(int rowIndex, int colIndex,  
                Region region) {  
            if (rowIndex >= region.getFirstRow() && rowIndex <= region.getLastRow()) {  
                if (colIndex >= region.getFirstColumn()  
                        && colIndex <= region.getLastColumn()) {  
                    return true;  
                }  
            }  
            return false;  
        }  
      
        public static boolean isCellInRegion(Cell cell, Region region) {  
            return isCellInRegion(cell.getRowIndex(), cell.getColumnIndex(), region);  
        }  
      
        public static Region getMergedRegion(Sheet sheet, int rowIndex, int colIndex) {  
            int sheetMergeCount = sheet.getNumMergedRegions();  
            for (int i = 0; i < sheetMergeCount; i++) {  
                CellRangeAddress range = sheet.getMergedRegion(i);  
                int firstColumn = range.getFirstColumn();  
                int lastColumn = range.getLastColumn();  
                int firstRow = range.getFirstRow();  
                int lastRow = range.getLastRow();  
                if (rowIndex >= firstRow && rowIndex <= lastRow) {  
                    if (colIndex >= firstColumn && colIndex <= lastColumn) {  
                        Region region = new Region();  
                        region.setFirstRow(firstRow);  
                        region.setLastRow(lastRow);  
                        region.setFirstColumn(firstColumn);  
                        region.setLastColumn(lastColumn);  
                        return region;  
                    }  
                }  
            }  
            return null;  
        }  
      
        public static Region getMergedRegion(Sheet sheet, Cell cell) {  
            return getMergedRegion(sheet, cell.getRowIndex(), cell.getColumnIndex());  
        }  
      
        /** 
         * 判断sheet页中是否含有合并单元格 
         *  
         * @param sheet 
         * @return 
         */  
        public static boolean hasMerged(Sheet sheet) {  
            return sheet.getNumMergedRegions() > 0 ? true : false;  
        }  
      
        /** 
         * 合并单元格 
         *  
         * @param sheet 
         * @param firstRow 
         *            开始行 
         * @param lastRow 
         *            结束行 
         * @param firstCol 
         *            开始列 
         * @param lastCol 
         *            结束列 
         */  
        public static void mergeRegion(Sheet sheet, int firstRow, int lastRow,  
                int firstCol, int lastCol) {  
            sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol,  
                    lastCol));  
        }  
      
        /** 
         * 获取单元格的值 
         *  
         * @param cell 
         * @return 
         */  
        public static String getCellValue(Cell cell) {  
      
            if (cell == null)  
                return "";  
      
            if (cell.getCellType() == Cell.CELL_TYPE_STRING) {  
      
                return cell.getStringCellValue();  
      
            } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {  
      
                return String.valueOf(cell.getBooleanCellValue());  
      
            } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {  
                  
                return cell.getCellFormula();  
      
            } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {  
                return String.valueOf(cell.getNumericCellValue());  
            }  
            return "";  
        }  
      
        public static ExcelInputStreamDto getUploadExcelInputStream(  
                HttpServletRequest request, Long maxExcelFileSize)  
                throws IOException {  
            String[] allowExtensions = { ".et", ".ett", ".xls", ".xlsx" };  
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(  
                    request.getSession().getServletContext());  
            // 判断 request 是否有文件上传,即多部分请求  
            if (multipartResolver.isMultipart(request)) {  
                // 转换成多部分request  
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;  
      
                MultipartFile file = multiRequest.getFile("import");  
      
                if (file == null || file.getSize() == 0) {  
                    throw new ServiceException("文件不存在");  
                }  
      
                if (maxExcelFileSize != null && file.getSize() > maxExcelFileSize) {  
                    throw new ServiceException("文件过大");  
                }  
                boolean extMatch = false;  
                ExcelFormat excelFormat = null;  
                for (String ext : allowExtensions) {  
                    if (file.getOriginalFilename().endsWith(ext)) {  
                        extMatch = true;  
                        if (".xls".equalsIgnoreCase(ext)) {  
                            excelFormat = ExcelFormat.xls;  
                        } else if (".xlsx".equalsIgnoreCase(ext)) {  
                            excelFormat = ExcelFormat.xlsx;  
                        } else if (".et".equalsIgnoreCase(ext)  
                                || ".ett".equalsIgnoreCase(ext)) {  
                            // WPS的et和ett格式可能内部是xls,也可能是xlsx,只能通过读取文件头判断  
                            if (file.getSize() < 2) {  
                                // 如果文件小于2字节,无法判断文件头,则直接返回格式不正确  
                                throw new ServiceException("不正确的文件格式");  
                            }  
                            byte[] fileHeaderBytes = new byte[2];  
                            InputStream is = file.getInputStream();  
                            is.read(fileHeaderBytes, 0, 2);  
                            String fileHeaderHex = GetTypeByHead  
                                    .bytesToHexString(fileHeaderBytes);  
                            if ("504B".equalsIgnoreCase(fileHeaderHex)) {  
                                excelFormat = ExcelFormat.xlsx;  
                            } else if ("D0CF".equalsIgnoreCase(fileHeaderHex)) {  
                                excelFormat = ExcelFormat.xls;  
                            }  
                        } else {  
                            throw new ServiceException("不正确的文件格式");  
                        }  
                        break;  
                    }  
                }  
                if (!extMatch) {  
                    throw new ServiceException("不正确的文件格式");  
                }  
                ExcelInputStreamDto result = new ExcelInputStreamDto();  
                result.setExcelFormat(excelFormat);  
                result.setInputStream(file.getInputStream());  
                return result;  
            }  
            throw new ServiceException("不正确的请求");  
        }  
      
        /** 
         * 判断Row(行)是否为空行(行本身为null或行中的单元格全部为null) 
         * @param row 
         * @return 
         */  
        public static boolean isEmptyRow(Row row) {  
            if (row != null) {  
                short lastCellNum = row.getLastCellNum();  
                if (lastCellNum == 0) {// 如果不存在单元格则返回true  
                    return true;  
                } else {  
                    // 空单元格的个数  
                    int emptyCellNum = 0;  
                    for (int i = 0; i < lastCellNum; i++) {  
                        Cell cell = row.getCell(i);  
                        if (isEmptyCell(cell)) {  
                            emptyCellNum++;  
                        }  
                    }  
                    if (emptyCellNum == lastCellNum) {  
                        return true;  
                    }  
                }  
            } else {  
                return true;  
            }  
            return false;  
        }  
      
        /** 
         * 判断Row(行)是否存在空的单元格或者这行是否存在单元格 
         * @param row 
         * @return 
         */  
        public static boolean rowContianEmptyCell(Row row) {  
            if (row != null) {  
                short lastCellNum = row.getLastCellNum();  
                if (lastCellNum == 0) {// 如果不存在单元格则返回true  
                    return true;  
                } else {  
                    for (int i = 0; i < lastCellNum; i++) {  
                        Cell cell = row.getCell(i);  
                        if (isEmptyCell(cell)) {  
                            return true;  
                        }  
                    }  
                }  
            } else {  
                return true;  
            }  
            return false;  
        }  
      
        /** 
         * 判断Sheet是否存在空的行或存在空数据的行 
         * @param sheet 
         * @return 
         */  
        public static boolean sheetContainEmptyRow(Sheet sheet) {  
            if (sheet != null) {  
                int lastRowNum = sheet.getLastRowNum();  
                if (lastRowNum == 0) {// 如果不存在sheet则返回true  
                    return true;  
                } else {  
                    for (int i = 0; i < lastRowNum; i++) {  
                        Row row = sheet.getRow(i);  
                        if (isEmptyRow(row)) {  
                            return true;  
                        }  
                    }  
                }  
            } else {  
                return true;  
            }  
            return false;  
        }  
      
        /** 
         * 基于指定列数判断Sheet是否存在空的行或存在空数据的行 
         * @param sheet 
         * @param columnNum 
         * @return 
         */  
        public static boolean sheetContainEmptyRow(Sheet sheet, int columnNum) {  
            if (sheet != null) {  
                int lastRowNum = sheet.getLastRowNum();  
                if (lastRowNum == 0) {// 如果不存在sheet则返回true  
                    return true;  
                } else {  
                    if (lastRowNum >= columnNum) {  
                        for (int i = 0; i < columnNum; i++) {  
                            Row row = sheet.getRow(i);  
                            if (isEmptyRow(row)) {  
                                return true;  
                            }  
                        }  
                    }else{  
                        return true;  
                    }  
                }  
            } else {  
                return true;  
            }  
            return false;  
        }  
        /** 
         * 获取表格中空行的行号 
         * @param sheet 
         * @return 
         */  
        public static List<Integer> getEmptyRowNos(Sheet sheet){  
            List<Integer> list=new ArrayList<>();  
            if (sheet != null) {  
                int lastRowNum = sheet.getLastRowNum();  
                if (lastRowNum != 0) {// 如果不存在sheet则返回true  
                    for (int i = 0; i < lastRowNum; i++) {  
                        Row row = sheet.getRow(i);  
                        if (isEmptyRow(row)) {  
                            list.add(i);  
                        }  
                    }  
                }   
            }  
            return list;  
        }  
          
        /** 
         * 判断Cell(单元格)是否为空 
         *  
         * @param cell 
         * @return 
         */  
        public static boolean isEmptyCell(Cell cell) {  
            String cellContent = getCellValue(cell);  
            if(StringUtils.hasText(cellContent)){  
                return false;  
            } else{  
                return true;  
            }  
        }  
      
        /** 
         * 关闭workbook 
         *  
         * @param workbook 
         */  
        public static void closeWorkbook(Workbook workbook) {  
            if (workbook != null) {  
                try {  
                    workbook.close();  
                } catch (IOException e) {  
                    LOG.error("关闭workbook失败", e);  
                }  
            }  
        }  
          
        public static void addDataToRow(Row row,List<String> values){  
            if(values!=null && !values.isEmpty()){  
                for (int i=0;i<values.size();i++) {  
                    Cell cell=row.createCell(i);  
                    cell.setCellValue(values.get(i));  
                }  
            }  
        }  
    } 

     

  39.      * @return 
  40.      */  
  41.     public static String getMergedRegionValue(Sheet sheet, Cell cell) {  
  42.         return getMergedRegionValue(sheet, cell.getRowIndex(),  
  43.                 cell.getColumnIndex());  
  44.     }  
  45.   
  46.     /** 
  47.      * 判断合并了行 
  48.      *  
  49.      * @param sheet 
  50.      * @param row 
  51.      * @param column 
  52.      * @return 
  53.      */  
  54.     public static boolean isMergedRow(Sheet sheet, int row, int column) {  
  55.         int sheetMergeCount = sheet.getNumMergedRegions();  
  56.         for (int i = 0; i < sheetMergeCount; i++) {  
  57.             CellRangeAddress range = sheet.getMergedRegion(i);  
  58.             int firstColumn = range.getFirstColumn();  
  59.             int lastColumn = range.getLastColumn();  
  60.             int firstRow = range.getFirstRow();  
  61.             int lastRow = range.getLastRow();  
  62.             if (row == firstRow && row == lastRow) {  
  63.                 if (column >= firstColumn && column <= lastColumn) {  
  64.                     return true;  
  65.                 }  
  66.             }  
  67.         }  
  68.         return false;  
  69.     }  
  70.   
  71.     /** 
  72.      * 判断指定的单元格是否是合并单元格 
  73.      *  
  74.      * @param sheet 
  75.      *            工作表 
  76.      * @param row 
  77.      *            行下标 
  78.      * @param column 
  79.      *            列下标 
  80.      * @return 
  81.      */  
  82.     public static boolean isMergedRegion(Sheet sheet, int row, int column) {  
  83.         int sheetMergeCount = sheet.getNumMergedRegions();  
  84.         for (int i = 0; i < sheetMergeCount; i++) {  
  85.             CellRangeAddress range = sheet.getMergedRegion(i);  
  86.             int firstColumn = range.getFirstColumn();  
  87.             int lastColumn = range.getLastColumn();  
  88.             int firstRow = range.getFirstRow();  
  89.             int lastRow = range.getLastRow();  
  90.             if (row >= firstRow && row <= lastRow) {  
  91.                 if (column >= firstColumn && column <= lastColumn) {  
  92.                     return true;  
  93.                 }  
  94.             }  
  95.         }  
  96.         return false;  
  97.     }  
  98.   
  99.     /** 
  100.      * 如果excel是wps格式,获取合并单元格的cell时,cell会是null,此时不能用该方法,请用isMergedRegion(Sheet sheet, int row, int column) 
  101.      * @description 
  102.      * @author liuzhenpeng 
  103.      * @date 2017年2月16日 
  104.      * @param sheet 
  105.      * @param cell 
  106.      * @return 
  107.      */  
  108.     public static boolean isMergedRegion(Sheet sheet, Cell cell) {  
  109.         int row = cell.getRowIndex();  
  110.         int column = cell.getColumnIndex();  
  111.         return isMergedRegion(sheet, row, column);  
  112.     }  
  113.   
  114.     public static boolean isCellInRegion(int rowIndex, int colIndex,  
  115.             Region region) {  
  116.         if (rowIndex >= region.getFirstRow() && rowIndex <= region.getLastRow()) {  
  117.             if (colIndex >= region.getFirstColumn()  
  118.                     && colIndex <= region.getLastColumn()) {  
  119.                 return true;  
  120.             }  
  121.         }  
  122.         return false;  
  123.     }  
  124.   
  125.     public static boolean isCellInRegion(Cell cell, Region region) {  
  126.         return isCellInRegion(cell.getRowIndex(), cell.getColumnIndex(), region);  
  127.     }  
  128.   
  129.     public static Region getMergedRegion(Sheet sheet, int rowIndex, int colIndex) {  
  130.         int sheetMergeCount = sheet.getNumMergedRegions();  
  131.         for (int i = 0; i < sheetMergeCount; i++) {  
  132.             CellRangeAddress range = sheet.getMergedRegion(i);  
  133.             int firstColumn = range.getFirstColumn();  
  134.             int lastColumn = range.getLastColumn();  
  135.             int firstRow = range.getFirstRow();  
  136.             int lastRow = range.getLastRow();  
  137.             if (rowIndex >= firstRow && rowIndex <= lastRow) {  
  138.                 if (colIndex >= firstColumn && colIndex <= lastColumn) {  
  139.                     Region region = new Region();  
  140.                     region.setFirstRow(firstRow);  
  141.                     region.setLastRow(lastRow);  
  142.                     region.setFirstColumn(firstColumn);  
  143.                     region.setLastColumn(lastColumn);  
  144.                     return region;  
  145.                 }  
  146.             }  
  147.         }  
  148.         return null;  
  149.     }  
  150.   
  151.     public static Region getMergedRegion(Sheet sheet, Cell cell) {  
  152.         return getMergedRegion(sheet, cell.getRowIndex(), cell.getColumnIndex());  
  153.     }  
  154.   
  155.     /** 
  156.      * 判断sheet页中是否含有合并单元格 
  157.      *  
  158.      * @param sheet 
  159.      * @return 
  160.      */  
  161.     public static boolean hasMerged(Sheet sheet) {  
  162.         return sheet.getNumMergedRegions() > 0 ? true : false;  
  163.     }  
  164.   
  165.     /** 
  166.      * 合并单元格 
  167.      *  
  168.      * @param sheet 
  169.      * @param firstRow 
  170.      *            开始行 
  171.      * @param lastRow 
  172.      *            结束行 
  173.      * @param firstCol 
  174.      *            开始列 
  175.      * @param lastCol 
  176.      *            结束列 
  177.      */  
  178.     public static void mergeRegion(Sheet sheet, int firstRow, int lastRow,  
  179.             int firstCol, int lastCol) {  
  180.         sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol,  
  181.                 lastCol));  
  182.     }  
  183.   
  184.     /** 
  185.      * 获取单元格的值 
  186.      *  
  187.      * @param cell 
  188.      * @return 
  189.      */  
  190.     public static String getCellValue(Cell cell) {  
  191.   
  192.         if (cell == null)  
  193.             return "";  
  194.   
  195.         if (cell.getCellType() == Cell.CELL_TYPE_STRING) {  
  196.   
  197.             return cell.getStringCellValue();  
  198.   
  199.         } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {  
  200.   
  201.             return String.valueOf(cell.getBooleanCellValue());  
  202.   
  203.         } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {  
  204.               
  205.             return cell.getCellFormula();  
  206.   
  207.         } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {  
  208.             return String.valueOf(cell.getNumericCellValue());  
  209.         }  
  210.         return "";  
  211.     }  
  212.   
  213.     public static ExcelInputStreamDto getUploadExcelInputStream(  
  214.             HttpServletRequest request, Long maxExcelFileSize)  
  215.             throws IOException {  
  216.         String[] allowExtensions = { ".et", ".ett", ".xls", ".xlsx" };  
  217.         CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(  
  218.                 request.getSession().getServletContext());  
  219.         // 判断 request 是否有文件上传,即多部分请求  
  220.         if (multipartResolver.isMultipart(request)) {  
  221.             // 转换成多部分request  
  222.             MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;  
  223.   
  224.             MultipartFile file = multiRequest.getFile("import");  
  225.   
  226.             if (file == null || file.getSize() == 0) {  
  227.                 throw new ServiceException("文件不存在");  
  228.             }  
  229.   
  230.             if (maxExcelFileSize != null && file.getSize() > maxExcelFileSize) {  
  231.                 throw new ServiceException("文件过大");  
  232.             }  
  233.             boolean extMatch = false;  
  234.             ExcelFormat excelFormat = null;  
  235.             for (String ext : allowExtensions) {  
  236.                 if (file.getOriginalFilename().endsWith(ext)) {  
  237.                     extMatch = true;  
  238.                     if (".xls".equalsIgnoreCase(ext)) {  
  239.                         excelFormat = ExcelFormat.xls;  
  240.                     } else if (".xlsx".equalsIgnoreCase(ext)) {  
  241.                         excelFormat = ExcelFormat.xlsx;  
  242.                     } else if (".et".equalsIgnoreCase(ext)  
  243.                             || ".ett".equalsIgnoreCase(ext)) {  
  244.                         // WPS的et和ett格式可能内部是xls,也可能是xlsx,只能通过读取文件头判断  
  245.                         if (file.getSize() < 2) {  
  246.                             // 如果文件小于2字节,无法判断文件头,则直接返回格式不正确  
  247.                             throw new ServiceException("不正确的文件格式");  
  248.                         }  
  249.                         byte[] fileHeaderBytes = new byte[2];  
  250.                         InputStream is = file.getInputStream();  
  251.                         is.read(fileHeaderBytes, 0, 2);  
  252.                         String fileHeaderHex = GetTypeByHead  
  253.                                 .bytesToHexString(fileHeaderBytes);  
  254.                         if ("504B".equalsIgnoreCase(fileHeaderHex)) {  
  255.                             excelFormat = ExcelFormat.xlsx;  
  256.                         } else if ("D0CF".equalsIgnoreCase(fileHeaderHex)) {  
  257.                             excelFormat = ExcelFormat.xls;  
  258.                         }  
  259.                     } else {  
  260.                         throw new ServiceException("不正确的文件格式");  
  261.                     }  
  262.                     break;  
  263.                 }  
  264.             }  
  265.             if (!extMatch) {  
  266.                 throw new ServiceException("不正确的文件格式");  
  267.             }  
  268.             ExcelInputStreamDto result = new ExcelInputStreamDto();  
  269.             result.setExcelFormat(excelFormat);  
  270.             result.setInputStream(file.getInputStream());  
  271.             return result;  
  272.         }  
  273.         throw new ServiceException("不正确的请求");  
  274.     }  
  275.   
  276.     /** 
  277.      * 判断Row(行)是否为空行(行本身为null或行中的单元格全部为null) 
  278.      * @param row 
  279.      * @return 
  280.      */  
  281.     public static boolean isEmptyRow(Row row) {  
  282.         if (row != null) {  
  283.             short lastCellNum = row.getLastCellNum();  
  284.             if (lastCellNum == 0) {// 如果不存在单元格则返回true  
  285.                 return true;  
  286.             } else {  
  287.                 // 空单元格的个数  
  288.                 int emptyCellNum = 0;  
  289.                 for (int i = 0; i < lastCellNum; i++) {  
  290.                     Cell cell = row.getCell(i);  
  291.                     if (isEmptyCell(cell)) {  
  292.                         emptyCellNum++;  
  293.                     }  
  294.                 }  
  295.                 if (emptyCellNum == lastCellNum) {  
  296.                     return true;  
  297.                 }  
  298.             }  
  299.         } else {  
  300.             return true;  
  301.         }  
  302.         return false;  
  303.     }  
  304.   
  305.     /** 
  306.      * 判断Row(行)是否存在空的单元格或者这行是否存在单元格 
  307.      * @param row 
  308.      * @return 
  309.      */  
  310.     public static boolean rowContianEmptyCell(Row row) {  
  311.         if (row != null) {  
  312.             short lastCellNum = row.getLastCellNum();  
  313.             if (lastCellNum == 0) {// 如果不存在单元格则返回true  
  314.                 return true;  
  315.             } else {  
  316.                 for (int i = 0; i < lastCellNum; i++) {  
  317.                     Cell cell = row.getCell(i);  
  318.                     if (isEmptyCell(cell)) {  
  319.                         return true;  
  320.                     }  
  321.                 }  
  322.             }  
  323.         } else {  
  324.             return true;  
  325.         }  
  326.         return false;  
  327.     }  
  328.   
  329.     /** 
  330.      * 判断Sheet是否存在空的行或存在空数据的行 
  331.      * @param sheet 
  332.      * @return 
  333.      */  
  334.     public static boolean sheetContainEmptyRow(Sheet sheet) {  
  335.         if (sheet != null) {  
  336.             int lastRowNum = sheet.getLastRowNum();  
  337.             if (lastRowNum == 0) {// 如果不存在sheet则返回true  
  338.                 return true;  
  339.             } else {  
  340.                 for (int i = 0; i < lastRowNum; i++) {  
  341.                     Row row = sheet.getRow(i);  
  342.                     if (isEmptyRow(row)) {  
  343.                         return true;  
  344.                     }  
  345.                 }  
  346.             }  
  347.         } else {  
  348.             return true;  
  349.         }  
  350.         return false;  
  351.     }  
  352.   
  353.     /** 
  354.      * 基于指定列数判断Sheet是否存在空的行或存在空数据的行 
  355.      * @param sheet 
  356.      * @param columnNum 
  357.      * @return 
  358.      */  
  359.     public static boolean sheetContainEmptyRow(Sheet sheet, int columnNum) {  
  360.         if (sheet != null) {  
  361.             int lastRowNum = sheet.getLastRowNum();  
  362.             if (lastRowNum == 0) {// 如果不存在sheet则返回true  
  363.                 return true;  
  364.             } else {  
  365.                 if (lastRowNum >= columnNum) {  
  366.                     for (int i = 0; i < columnNum; i++) {  
  367.                         Row row = sheet.getRow(i);  
  368.                         if (isEmptyRow(row)) {  
  369.                             return true;  
  370.                         }  
  371.                     }  
  372.                 }else{  
  373.                     return true;  
  374.                 }  
  375.             }  
  376.         } else {  
  377.             return true;  
  378.         }  
  379.         return false;  
  380.     }  
  381.     /** 
  382.      * 获取表格中空行的行号 
  383.      * @param sheet 
  384.      * @return 
  385.      */  
  386.     public static List<Integer> getEmptyRowNos(Sheet sheet){  
  387.         List<Integer> list=new ArrayList<>();  
  388.         if (sheet != null) {  
  389.             int lastRowNum = sheet.getLastRowNum();  
  390.             if (lastRowNum != 0) {// 如果不存在sheet则返回true  
  391.                 for (int i = 0; i < lastRowNum; i++) {  
  392.                     Row row = sheet.getRow(i);  
  393.                     if (isEmptyRow(row)) {  
  394.                         list.add(i);  
  395.                     }  
  396.                 }  
  397.             }   
  398.         }  
  399.         return list;  
  400.     }  
  401.       
  402.     /** 
  403.      * 判断Cell(单元格)是否为空 
  404.      *  
  405.      * @param cell 
  406.      * @return 
  407.      */  
  408.     public static boolean isEmptyCell(Cell cell) {  
  409.         String cellContent = getCellValue(cell);  
  410.         if(StringUtils.hasText(cellContent)){  
  411.             return false;  
  412.         } else{  
  413.             return true;  
  414.         }  
  415.     }  
  416.   
  417.     /** 
  418.      * 关闭workbook 
  419.      *  
  420.      * @param workbook 
  421.      */  
  422.     public static void closeWorkbook(Workbook workbook) {  
  423.         if (workbook != null) {  
  424.             try {  
  425.                 workbook.close();  
  426.             } catch (IOException e) {  
  427.                 LOG.error("关闭workbook失败", e);  
  428.             }  
  429.         }  
  430.     }  
  431.       
  432.     public static void addDataToRow(Row row,List<String> values){  
  433.         if(values!=null && !values.isEmpty()){  
  434.             for (int i=0;i<values.size();i++) {  
  435.                 Cell cell=row.createCell(i);  
  436.                 cell.setCellValue(values.get(i));  
  437.             }  
  438.         }  
  439.     }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值