POI Excel解析
Maven 引入POI
<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 工具類
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) {// 獲得一個(gè) sheet 中合并單元格的數(shù)量int sheetmergerCount = sheet.getNumMergedRegions();// 便利合并單元格for (int i = 0; i < sheetmergerCount; i++) {// 獲得合并單元格CellRangeAddress ca = sheet.getMergedRegion(i);// 獲得合并單元格的起始行, 結(jié)束行, 起始列, 結(jié)束列int firstC = ca.getFirstColumn();int firstR = ca.getFirstRow();if (cell.getColumnIndex() == firstC && cell.getRowIndex() == firstR) {System.out.println("第" + (cell.getRowIndex() + 1) + "行 第" + (cell.getColumnIndex() + 1) + "列 的內(nèi)容是: "+ 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) {// 得到一個(gè)sheet中有多少個(gè)合并單元格int sheetMergerCount = sheet.getNumMergedRegions();for (int i = 0; i < sheetMergerCount; i++) {// 具體的合并單元格CellRangeAddress ca = sheet.getMergedRegion(i);// 合并單元格的起始列, 結(jié)束列;起始行, 結(jié)束行。int firstC = ca.getFirstColumn();int lastC = ca.getLastColumn();int firstR = ca.getFirstRow();int lastR = ca.getLastRow();// 判斷該單元格是否在合并單元格范圍之內(nèi), 如果是, 則返回 trueif ((cell.getColumnIndex() <= lastC && cell.getColumnIndex() >= firstC) && (cell.getRowIndex() <= lastR && cell.getRowIndex() >= firstR)) {return true;}}return false;}/*** @param filePath 文件路徑* @param sheetIndex sheet* @param startRowIndex 開(kāi)始行(不包含)* @return*/private static Map<String, Object> getExcelValue(String filePath, int sheetIndex, int startRowIndex) {//保存解析的值 key: 行-列Map<String, Object> excelMap = new LinkedHashMap<>();try {// 創(chuàng)建對(duì)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頁(yè)Sheet sheet = book.getSheetAt(sheetIndex);// 獲取到Excel文件中的所有行數(shù)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) + "列 的內(nèi)容是: " + 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());}} }輸出結(jié)果
第1行 第1列 的內(nèi)容是: 品牌
第1行 第2列 的內(nèi)容是: 車系
第1行 第3列 的內(nèi)容是: 車型
第1行 第4列 的內(nèi)容是: 車款
第1行 第5列 的內(nèi)容是: 價(jià)格(萬(wàn)元)
第1行 第6列 的內(nèi)容是: 首付比例
第2行 第1列 的內(nèi)容是: 路虎
第2行 第2列 的內(nèi)容是: 星脈
第2行 第3列 的內(nèi)容是: L560 2.0P
第2行 第4列 的內(nèi)容是: 2.0P AWD SWB S
第2行 第5列 的內(nèi)容是: 399
第2行 第6列 的內(nèi)容是: 20%
第3行 第1列 的內(nèi)容是: 路虎
第3行 第2列 的內(nèi)容是: 進(jìn)EV
第3行 第3列 的內(nèi)容是: Range Rover Evoque
第3行 第4列 的內(nèi)容是: 敞篷版 2.0L P Convertible 4WD HSE Dynamic 2017款
第3行 第5列 的內(nèi)容是: 599
第3行 第6列 的內(nèi)容是: 25%
1-1:品牌
1-2:車系
1-3:車型
1-4:車款
1-5:價(jià)格(萬(wàn)元)
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:進(jìn)EV
3-3:Range Rover Evoque
3-4:敞篷版 2.0L P Convertible 4WD HSE Dynamic 2017款
3-5:599
3-6:25%
轉(zhuǎn)載于:https://www.cnblogs.com/liu-king/p/8982013.html
總結(jié)
以上是生活随笔為你收集整理的POI Excel解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 课时40:类与对象:一些相关的BIF
- 下一篇: maven私服配置