永发信息网

Java POI读取Excel的时候怎么按列读取

答案:2  悬赏:50  手机版
解决时间 2021-02-01 13:41
  • 提问者网友:感性作祟
  • 2021-01-31 21:01
Java POI读取Excel的时候怎么按列读取
最佳答案
  • 五星知识达人网友:你可爱的野爹
  • 2021-01-31 21:17
按不按列读取不重要,你是想在解析完成后输出(或返回)时数据按照列的先后顺序是吧,你可以还是按行循环读取,再把读取的各个行中的cell值放入各个ArrayList里,如cell0放入ArrayList0里,最后,你想取那一列的数据,就遍历那个ArrayList
全部回答
  • 1楼网友:人间朝暮
  • 2021-01-31 22:30
按列读取的方法: string pathname = "e:\\files\\title.xlsx"; file file = new file(pathname); inputstream in = new fileinputstream(file); //得到整个excel对象 xssfworkbook excel = new xssfworkbook(in); //获取整个excel有多少个sheet int sheets = excel.getnumberofsheets(); //便利第一个sheet map colmap = new hashmap(); for(int i = 0 ; i < sheets ; i++ ){ xssfsheet sheet = excel.getsheetat(i); if(sheet == null){ continue; } int mergedregions = sheet.getnummergedregions(); xssfrow row2 = sheet.getrow(0); map category = new hashmap(); for(int j = 0 ; j < mergedregions; j++ ){ cellrangeaddress rangeaddress = sheet.getmergedregion(j); int firstrow = rangeaddress.getfirstcolumn(); int lastrow = rangeaddress.getlastcolumn(); category.put(rangeaddress.getfirstcolumn(), rangeaddress.getlastcolumn()+"-"+row2.getcell(firstrow).tostring()); } //便利每一行 for( int rownum = 1 ; rownum <= sheet.getlastrownum() ; rownum++ ){ system.out.println(); xssfrow row = sheet.getrow(rownum); if(row == null){ continue; } short lastcellnum = row.getlastcellnum(); string cate = ""; integer maxindex = 0; for( int col = row.getfirstcellnum() ; col < lastcellnum ; col++ ){ xssfcell cell = row.getcell(col); if(cell == null ){ continue; } if("".equals(cell.tostring())){ continue; } int columnindex = cell.getcolumnindex(); string string = category.get(columnindex); if(string != null && !string.equals("")){ string[] split = string.split("-"); cate = split[1]; maxindex = integer.parseint(split[0]); system.out.println(cate+"<-->"+cell.tostring()); }else { //如果当前便利的列编号小于等于合并单元格的结束,说明分类还是上面的分类名称 if(columnindex<=maxindex){ system.out.println(cate+"<-->"+cell.tostring()); }else { system.out.println("分类未知"+"<-->"+cell.tostring()); } } } } } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯