Commit de5c100d by 黄明步

添加获取Excel表头和数据的方法

parent c950ea6e
package com.zq.model.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.cell.CellUtil;
import com.zq.common.exception.BusinessException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
/**
* @program: instrument-cloud
......@@ -81,4 +83,127 @@ public class MyExcelUtil {
}
return value;
}
/**
* 获取Excel的表头
*
* @param excelFilePath 文件路径
* @param sheetIndex 工作表索引
* @return {@link List}<{@link String}>
*/
public static List<String> getHeaders(String excelFilePath, int sheetIndex) {
List<String> headers = new ArrayList<>();
try (FileInputStream file = new FileInputStream(excelFilePath)) {
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheetAt(sheetIndex);
int rowIndex = getFirstUnmergedRowIndex(sheet);
Row headerRow = sheet.getRow(rowIndex);
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
Cell cell = headerRow.getCell(i);
String header = CellUtil.getCellValue(cell).toString();
headers.add(header);
}
} catch (IOException e) {
e.printStackTrace();
}
return headers;
}
/**
* 获取表格数据,Key-Value形式,Key为表头,Value为单元格的值
*
* @param excelFilePath 文件路径
* @param sheetIndex 工作表索引
* @return {@link List}<{@link Map}<{@link String}, {@link String}>>
*/
public static List<Map<String, String>> getData(String excelFilePath, int sheetIndex) {
List<Map<String, String>> data = new ArrayList<>();
try (FileInputStream file = new FileInputStream(excelFilePath)) {
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheetAt(sheetIndex);
int headerIndex = getFirstUnmergedRowIndex(sheet);
Row headerRow = sheet.getRow(headerIndex);
for (int rowIndex = headerIndex + 1; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
// 检查行中的所有值是否为空
boolean allValuesEmpty = true;
for (int cellIndex = 0; cellIndex < headerRow.getLastCellNum(); cellIndex++) {
Cell dataCell = row.getCell(cellIndex);
if (dataCell != null && !CellUtil.getCellValue(dataCell).toString().trim().isEmpty()) {
allValuesEmpty = false;
break;
}
}
if (!allValuesEmpty) {
Map<String, String> rowMap = new HashMap<>();
for (int cellIndex = 0; cellIndex < headerRow.getLastCellNum(); cellIndex++) {
Cell headerCell = headerRow.getCell(cellIndex);
Cell dataCell = row.getCell(cellIndex);
String header = CellUtil.getCellValue(headerCell).toString();
String value = (dataCell == null) ? "" : CellUtil.getCellValue(dataCell).toString();
rowMap.put(header, value);
}
data.add(rowMap);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
/**
* 获取第一个没有合并单元格的行坐标
*
* @param sheet
* @return int
*/
private static int getFirstUnmergedRowIndex(Sheet sheet) {
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
if (row != null) {
boolean hasMergedCells = hasMergedCellsInRow(row);
// 如果当前行没有合并单元格,返回当前行的索引
if (!hasMergedCells) {
return i;
}
}
}
// 如果所有行都有合并单元格,返回最后一行的索引
return lastRowNum;
}
/**
* 判断该行是否包含合并单元格
*
* @param row
* @return boolean
*/
private static boolean hasMergedCellsInRow(Row row) {
for (int i = 0; i < row.getSheet().getNumMergedRegions(); i++) {
CellRangeAddress mergedRegion = row.getSheet().getMergedRegion(i);
if (mergedRegion.getFirstRow() == row.getRowNum()) {
return true;
}
}
return false;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment