开发者社区> 问答> 正文

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

Excel结构如图所示,我现在有两行数据,我要把这两行数据给对应起来.第一行的数据,表示的是一个分类,第二行是这个分类下的子类.比如性别分类有男和女,年龄段分类有青年,中年,老年.后面依次.我现在读取Excel的时候,怎么才能把他们对应起来呢.我的想法是按照列读取,但是POI好像没有按列读取的方法.或者谁有更好的建议

展开
收起
蛮大人123 2016-03-06 10:18:25 10548 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Random;
    
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class TestPOIP1Title {
        public static void main(String[] args) throws Exception {
            
            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<String,String> colMap = new HashMap<String, String>();
            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<Integer,String> category = new HashMap<Integer, String>();
                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());
                            }
                        }
                    }
                }
            }
        }
    }

    运行的结果如下:
    3

    2019-07-17 18:54:09
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载