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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章