poi读取excel内容

第一、引入包,这里使用的的是maven,其他工具自己百度。

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.1</version>
        </dependency>

第二、工具方法。

/**
     * 读取指定 excel 文件
     *
     * @param inputFilePath 绝对文件路径
     * @param rowBegin      开始读取的行,注意从 1 开始
     * @return
     */
    public static Map<String, String> readDataFromExcel(String inputFilePath, int rowBegin) {
        Map<String, String> areas = new HashMap<>();
        FileInputStream fileInput = null;//创建文件输入流
        XSSFWorkbook wb = null;//由输入流文件得到工作簿对象
        try {
            fileInput = new FileInputStream(inputFilePath);
            wb = new XSSFWorkbook(fileInput);
            XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
            int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
            //rowBegin代表要开始读取的行号,下面这个循环的作用是读取每一行内容
            for (int i = rowBegin; i <= lastRowNum; ++i) {
                XSSFRow row = sheet.getRow(i);//获取每一行
                //因为我的文件是固定的,所以直接坐标获取了
                String id = row.getCell(0).getStringCellValue().replace("CN", "");
                String name = row.getCell(2).getStringCellValue();
                String pro = row.getCell(7).getStringCellValue();
                String city = row.getCell(9).getStringCellValue();
                String key = pro + "省-" + city + "市-" + name;
                if (areas.containsKey(key)) {
                    //重复的自己处理
                                        //System.out.println(key);
                }
                areas.put(key, id);
                // 注释掉的是常规循环过程
                /*int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号,即总列数
                for (int j=0; j<columnNum; ++j) {
                    XSSFCell cell = row.getCell(j);//获取每个单元格
                   if (CellType.NUMERIC.equals(cell.getCellType())) {
                        System.out.printf("%.0f\t", cell.getNumericCellValue());
                    } else {
                        System.out.printf("%s\t", cell.getStringCellValue());
                    }
                }
                System.out.println();*/
            }
        } catch (Exception e) {
            logger.error("读取 地址 excel 文件错误!");
            e.printStackTrace();
        } finally {
            if (null != wb) {
                try {
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fileInput) {
                try {
                    fileInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return areas;
    }

猜你喜欢

转载自blog.51cto.com/yuqian2203/2468001