当前位置: 移动技术网 > IT编程>开发语言>Java > POI导入具有合并了单元格的Excel

POI导入具有合并了单元格的Excel

2018年10月25日  | 移动技术网IT编程  | 我要评论

168信息网,何平平怎么死的,八拜之交是什么意思

  poi进行单行单行地导入的数据在网上有许多的文章,但是要导入一个具有合并单元格的excel貌似比较难找。刚好最近完成了这样的一个需求,要求导入具有合并单元格的excel:

 1 /**
 2      * 读取excel数据,调用这方法开始
 3      * @param is
 4      * @param indexnum 至少需要多少列数据
 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      * 读取excel文件
 3      * @param wb
 4      * @param sheetindex sheet页下标:从0开始
 5      * @param startreadline 开始读取的行:从0开始
 6      * @param tailline 去除最后读取的行
 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           //判断是否具有合并单元格
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      * 获取合并单元格的值
 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      * 判断指定的单元格是否是合并单元格
 3      * @param sheet
 4      * @param row 行下标
 5      * @param column 列下标
 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      * 获取单元格的值
 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     }

  注意:这导入功能也适用于单行读取,直接调用 readexceltoobj() 方法即可;参数1:传入excel文件的输入流;参数2:指定你希望至少要读入多少列数据(比如传入个0,就代表:如果你有的行只有3列数据的话,那么获得的数组长度就只有3;如果你传入了10,那些只有3列的数据会自动填充空字符串给数组,使每个数组最小长度为10);

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网