POI imports Excel with merged cells

Keywords: Java Excel

Data imported by POI in one line and one line has many articles on the Internet, but it seems difficult to import an excel with merged cells. Just recently completed a requirement to import excel with merged cells:

 1 /**
 2      * Read excel data and call this method to start
 3      * @param is
 4      * @param indexNum At least how many columns of data are required
 5      */
 6     public static List<Object[]> readExcelToObj(InputStream is,int indexNum) {
 7 
 8         Workbook wb = null;
 9         List<Object[]> objArrList = null;
10         try {
11             objArrList = new ArrayList<>();
12             wb = WorkbookFactory.create(is);
13             readExcel(wb, 0, 0, 0,objArrList,indexNum);
14         } catch (InvalidFormatException e) {
15             e.printStackTrace();
16         } catch (IOException e) {
17             e.printStackTrace();
18         }
19         return objArrList;
20     }
 1 /**
 2      * Read excel file
 3      * @param wb
 4      * @param sheetIndex sheet Page Subscription: Starting from 0
 5      * @param startReadLine Start reading rows: start at 0
 6      * @param tailLine Remove the last read line
 7      */
 8     public static void readExcel(Workbook wb,int sheetIndex, int startReadLine, int tailLine, List<Object[]> objArrList, int indexNum) {
 9         Sheet sheet = wb.getSheetAt(sheetIndex);
10         Row row = null;
11 
12         for(int i=startReadLine; i<sheet.getLastRowNum()-tailLine+1; i++) {
13             row = sheet.getRow(i);
14             List<Object> objList = new ArrayList<>();
15             for(int j = 0 ; j<row.getLastCellNum();j++) {
16                 //for(Cell c : row) {
17                 Cell c = row.getCell(j);
18                 if(c==null){
19                     objList.add("");
20                     continue;
21                 }
22                 boolean isMerge = isMergedRegion(sheet, i, c.getColumnIndex());
23           //Determine whether a merged cell exists
24                 if(isMerge) {
25                     String rs = getMergedRegionValue(sheet, row.getRowNum(), c.getColumnIndex());
26                     objList.add(rs);
27                 }else {
28                     objList.add(getCellValue(c));
29                 }
30 
31             }
32             while(objList.size()<indexNum){
33                 objList.add("");
34             }
35             objArrList.add(objList.toArray());
36         }
37     }
 1 /**
 2      * Get the value of the merged cell
 3      * @param sheet
 4      * @param row
 5      * @param column
 6      * @return
 7      */
 8     public static String getMergedRegionValue(Sheet sheet ,int row , int column){
 9         int sheetMergeCount = sheet.getNumMergedRegions();
10 
11         for(int i = 0 ; i < sheetMergeCount ; i++){
12             CellRangeAddress ca = sheet.getMergedRegion(i);
13             int firstColumn = ca.getFirstColumn();
14             int lastColumn = ca.getLastColumn();
15             int firstRow = ca.getFirstRow();
16             int lastRow = ca.getLastRow();
17 
18             if(row >= firstRow && row <= lastRow){
19 
20                 if(column >= firstColumn && column <= lastColumn){
21                     Row fRow = sheet.getRow(firstRow);
22                     Cell fCell = fRow.getCell(firstColumn);
23                     return getCellValue(fCell) ;
24                 }
25             }
26         }
27 
28         return null ;
29     }
 1 /**
 2      * Determine whether the specified cell is a merged cell or not?
 3      * @param sheet
 4      * @param row row subscript
 5      * @param column column subscript
 6      * @return
 7      */
 8     public static boolean isMergedRegion(Sheet sheet,int row ,int column) {
 9         int sheetMergeCount = sheet.getNumMergedRegions();
10         for (int i = 0; i < sheetMergeCount; i++) {
11             CellRangeAddress range = sheet.getMergedRegion(i);
12             int firstColumn = range.getFirstColumn();
13             int lastColumn = range.getLastColumn();
14             int firstRow = range.getFirstRow();
15             int lastRow = range.getLastRow();
16             if(row >= firstRow && row <= lastRow){
17                 if(column >= firstColumn && column <= lastColumn){
18                     return true;
19                 }
20             }
21         }
22         return false;
23     }
 1 /**
 2      * Get the value of the cell
 3      * @param cell
 4      * @return
 5      */
 6     public static String getCellValue(Cell cell){
 7 
 8         if(cell == null) return "";
 9 
10         if(cell.getCellType() == Cell.CELL_TYPE_STRING){
11 
12             return cell.getStringCellValue();
13 
14         }else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
15 
16             return String.valueOf(cell.getBooleanCellValue());
17 
18         }else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
19 
20             return cell.getCellFormula() ;
21 
22         }else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
23 
24             return String.valueOf(cell.getNumericCellValue());
25 
26         }
27         return "";
28     }

Note: This import function is also suitable for single-line reading, just call readExcel ToObj () method directly; parameter 1: input stream into excel file; parameter 2: specify how many columns of data you want to read at least (e.g. input 0, which means: if you have only three columns of data in all rows, the length of the array you get is only 3; if you pass in 10, those are only 3 columns. Three columns of data will automatically fill empty strings into the array, so that the minimum length of each array is 10;

Posted by cityboy101 on Fri, 25 Jan 2019 09:27:13 -0800