Maven introduces 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 tools
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(); /** * Merge cell values * * @param sheet * @param cell * @param excelMap */ public static void getMergedRegionValue(Sheet sheet, Cell cell, Map<String, Object> excelMap) { // Get the number of merged cells in a sheet int sheetmergerCount = sheet.getNumMergedRegions(); // Convenient for merging Cell for ( int i = 0; i < sheetmergerCount; i++ ) { // Get the merged cell CellRangeAddress ca = sheet.getMergedRegion(i); // Get the start row, end row, start column of the merged cell, end column int firstC = ca.getFirstColumn(); int firstR = ca.getFirstRow(); if (cell.getColumnIndex() == firstC && cell.getRowIndex() == firstR) { System.out.println( "Number" + (cell.getRowIndex() + 1) + "Row" + (cell.getColumnIndex() + 1) + "The content of the column is: " + getCellValue(cell)); excelMap.put((cell.getRowIndex() + 1) + "-" + (cell.getColumnIndex() + 1), getCellValue(cell)); } } } /** * Determine if cells are being merged * * @param sheet * @param cell * @return */ public static boolean isMergedRegion(Sheet sheet, Cell cell) { // Get how many merged cells in a sheet int sheetMergerCount = sheet.getNumMergedRegions(); for ( int i = 0; i < sheetMergerCount; i++ ) { // The specific merged cell CellRangeAddress ca = sheet.getMergedRegion(i); // The starting column of the merged cell, the ending column; the starting row, the ending row. int firstC = ca.getFirstColumn(); int lastC = ca.getLastColumn(); int firstR =ca.getFirstRow(); int lastR = ca.getLastRow(); // Determine whether the cell is within the range of merged cells, if so, return true if ((cell.getColumnIndex() <= lastC && cell. getColumnIndex() >= firstC) && (cell.getRowIndex() <= lastR && cell.getRowIndex() >= firstR)) { return true ; } } return false; } /** * @param filePath file path * @param sheetIndex sheet * @param startRowIndex start row (not included) * @return */ private static Map<String, Object> getExcelValue(String filePath, int sheetIndex, int startRowIndex) { // Save the parsed value key: row-column Map<String, Object> excelMap = new LinkedHashMap<> () ; try { // create a file for Excel workbook Workbook book = null ; try { // .xlsx 2007 version + book = new XSSFWorkbook( new FileInputStream(filePath)); } catch (Exception ex) { //.xls 2003版本 book = new HSSFWorkbook(new FileInputStream(filePath)); } // Read sheet page Sheet sheet = book.getSheetAt(sheetIndex); // Get the number of all rows in the Excel file int rows = sheet.getPhysicalNumberOfRows(); for ( int i = startRowIndex; i < rows; i++ ) { // Read the cell Row row = sheet.getRow(i); if (row != null ) { // Get all the columns in the Excel file int cells = row.getPhysicalNumberOfCells(); for ( int j = 0 ; j < cells; j++) { // Get the value of the column Cell cell = row.getCell(j); if (cell != null ) { if (isMergedRegion(sheet, cell)) { getMergedRegionValue(sheet, cell, excelMap); } else { System.out.println( "th" + (i + 1) + "row" + (j + 1) + "The content of the column is: " + getCellValue(cell)); excelMap.put((i + 1) + "-" + (j + 1), getCellValue(cell)); } } } } } } catch (FileNotFoundException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } return excelMap; } /** * Get cell value * * @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()); } } }
output result
The content of row 1, column 1 is: The content of brand row 1, column
2 is: The content of car series,
row 1, column 3 is: The content of model
row 1, column 4 is : The content of model row
1 The content of column 5 is: Price (10,000 yuan)
The content of row 1 and column 6 is: Down payment ratio
The content of row 2 and column 1 is: The content of Land Rover
row 2 and column 2 is: Xingmai row
2 The content of column 3 is: L560 2.0P
The content of line 2 and column 4 is: 2.0P AWD SWB S
The content of line 2 and column 5 is: 399
The content of line 2 and column 6 is: 20%
Line 3 The content of column 1 is: Land Rover
, the content of row 3, column 2 is: Into EV, the content of
row 3, column 3 is: Range Rover Evoque, the content of row 3, column
4 is: Convertible 2.0LP Convertible 4WD HSE The content of
the 3rd row and the 5th column of the Dynamic 2017 model is: 599
The content of the 3rd row and the 6th column is: 25%
1-1: brand
1-2: car series
1-3: model
1-4: car model
1- 5: Price (ten thousand yuan)
1-6: Down payment ratio
2-1: Land Rover
2-2:
Xingmai 2-3: L560 2.0P
2-4: 2.0P AWD SWB S
2-5: 399
2-6: 20%
3-1: Land Rover
3-2: Into EV
3-3: Range Rover Evoque
3-4: Convertible 2.0LP Convertible 4WD HSE Dynamic 2017
3-5: 599
3-6: 25%