POI Excel解析

Maven 引入POIjava

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.13</version>
        </dependency>

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

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.13</version>
        </dependency>

excel 工具類apache

package com.iris.controller.hello;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author
 * @date Created in 2018/5/2
 * @see
 */
public class PoiExcelUtil {

    private static DataFormatter formatter = new DataFormatter();

    /**
     * 合併單元格值
     *
     * @param sheet
     * @param cell
     * @param excelMap
     */
    public static void getMergedRegionValue(Sheet sheet, Cell cell, Map<String, Object> excelMap) {
        // 得到一個 sheet 中合併單元格的數量
        int sheetmergerCount = sheet.getNumMergedRegions();
        // 便利合併單元格
        for (int i = 0; i < sheetmergerCount; i++) {
            // 得到合併單元格
            CellRangeAddress ca = sheet.getMergedRegion(i);
            // 得到合併單元格的起始行, 結束行, 起始列, 結束列
            int firstC = ca.getFirstColumn();
            int firstR = ca.getFirstRow();

            if (cell.getColumnIndex() == firstC && cell.getRowIndex() == firstR) {
                System.out.println("第" + (cell.getRowIndex() + 1) + "行 第" + (cell.getColumnIndex() + 1) + "列 的內容是: "
                        + getCellValue(cell));
                excelMap.put((cell.getRowIndex() + 1) + "-" + (cell.getColumnIndex() + 1), getCellValue(cell));
            }

        }
    }

    /**
     * 判斷是否在合併單元格
     *
     * @param sheet
     * @param cell
     * @return
     */
    public static boolean isMergedRegion(Sheet sheet, Cell cell) {
        // 獲得一個sheet中有多少個合併單元格
        int sheetMergerCount = sheet.getNumMergedRegions();
        for (int i = 0; i < sheetMergerCount; i++) {
            // 具體的合併單元格
            CellRangeAddress ca = sheet.getMergedRegion(i);
            // 合併單元格的起始列, 結束列;起始行, 結束行。
            int firstC = ca.getFirstColumn();
            int lastC = ca.getLastColumn();
            int firstR = ca.getFirstRow();
            int lastR = ca.getLastRow();
            // 判斷該單元格是否在合併單元格範圍以內, 若是是, 則返回 true
            if ((cell.getColumnIndex() <= lastC && cell.getColumnIndex() >= firstC) && (cell.getRowIndex() <= lastR && cell.getRowIndex() >= firstR)) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param filePath      文件路徑
     * @param sheetIndex    sheet
     * @param startRowIndex 開始行(不包含)
     * @return
     */
    private static Map<String, Object> getExcelValue(String filePath, int sheetIndex, int startRowIndex) {
        //保存解析的值 key: 行-列
        Map<String, Object> excelMap = new LinkedHashMap<>();
        try {
            // 建立對Excel工做簿文件
            Workbook book = null;
            try {
                //.xlsx 2007版本+
                book = new XSSFWorkbook(new FileInputStream(filePath));
            } catch (Exception ex) {
                //.xls 2003版本
                book = new HSSFWorkbook(new FileInputStream(filePath));
            }
            //讀取sheet頁
            Sheet sheet = book.getSheetAt(sheetIndex);
            // 獲取到Excel文件中的全部行數
            int rows = sheet.getPhysicalNumberOfRows();
            for (int i = startRowIndex; i < rows; i++) {
                // 讀取單元格
                Row row = sheet.getRow(i);
                if (row != null) {
                    // 獲取到Excel文件中的全部的列
                    int cells = row.getPhysicalNumberOfCells();
                    for (int j = 0; j < cells; j++) {
                        // 獲取到列的值
                        Cell cell = row.getCell(j);
                        if (cell != null) {
                            if (isMergedRegion(sheet, cell)) {
                                getMergedRegionValue(sheet, cell, excelMap);
                            } else {
                                System.out.println("第" + (i + 1) + "行 第" + (j + 1) + "列 的內容是: " + getCellValue(cell));
                                excelMap.put((i + 1) + "-" + (j + 1), getCellValue(cell));
                            }

                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return excelMap;
    }

    /**
     * 獲取 cell 值
     *
     * @param cell
     * @return
     */
    private static String getCellValue(Cell cell) {
        return formatter.formatCellValue(cell);
    }

    public static void main(String[] args) throws Exception {
        String file = "E://excel.xlsx";
        Map<String, Object> excelValue = getExcelValue(file, 0, 0);
        for (Map.Entry<String, Object> entry : excelValue.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}

輸出結果xss

第1行 第1列 的內容是: 品牌
第1行 第2列 的內容是: 車系
第1行 第3列 的內容是: 車型
第1行 第4列 的內容是: 車款
第1行 第5列 的內容是: 價格(萬元)
第1行 第6列 的內容是: 首付比例
第2行 第1列 的內容是: 路虎
第2行 第2列 的內容是: 星脈
第2行 第3列 的內容是: L560 2.0P
第2行 第4列 的內容是: 2.0P AWD SWB S
第2行 第5列 的內容是: 399
第2行 第6列 的內容是: 20%
第3行 第1列 的內容是: 路虎
第3行 第2列 的內容是: 進EV
第3行 第3列 的內容是: Range Rover Evoque
第3行 第4列 的內容是: 敞篷版 2.0L P Convertible 4WD HSE Dynamic 2017款
第3行 第5列 的內容是: 599
第3行 第6列 的內容是: 25%
1-1:品牌
1-2:車系
1-3:車型
1-4:車款
1-5:價格(萬元)
1-6:首付比例
2-1:路虎
2-2:星脈
2-3:L560 2.0P
2-4:2.0P AWD SWB S
2-5:399
2-6:20%
3-1:路虎
3-2:進EV
3-3:Range Rover Evoque
3-4:敞篷版 2.0L P Convertible 4WD HSE Dynamic 2017款
3-5:599
3-6:25%工具

相關文章
相關標籤/搜索