|
@@ -113,10 +113,10 @@ public class ExcelUtils {
|
|
int rowsNum = sheet.getPhysicalNumberOfRows();
|
|
int rowsNum = sheet.getPhysicalNumberOfRows();
|
|
if (rowsNum <= 1) return dataList;
|
|
if (rowsNum <= 1) return dataList;
|
|
//页头行
|
|
//页头行
|
|
- Row rowHead = sheet.getRow(headerRowNum);
|
|
|
|
|
|
+ Row headerRow = sheet.getRow(Math.max(headerRowNum - 1, 0));
|
|
//字段映射
|
|
//字段映射
|
|
Map<Integer, String> fieldMap = new HashMap<>();
|
|
Map<Integer, String> fieldMap = new HashMap<>();
|
|
- for (Cell cell : rowHead) {
|
|
|
|
|
|
+ for (Cell cell : headerRow) {
|
|
//检查线程中断,并响应
|
|
//检查线程中断,并响应
|
|
if (Thread.interrupted()) throw new InterruptedException();
|
|
if (Thread.interrupted()) throw new InterruptedException();
|
|
//获取值,如不为空,则存入索引关系
|
|
//获取值,如不为空,则存入索引关系
|
|
@@ -130,7 +130,7 @@ public class ExcelUtils {
|
|
//检查线程中断,并响应
|
|
//检查线程中断,并响应
|
|
if (Thread.interrupted()) throw new InterruptedException();
|
|
if (Thread.interrupted()) throw new InterruptedException();
|
|
//跳过非数据行
|
|
//跳过非数据行
|
|
- if (row == null || row.getRowNum() < dataRowNum) continue;
|
|
|
|
|
|
+ if (row == null || row.getRowNum() < (dataRowNum - 1)) continue;
|
|
//跳过空行
|
|
//跳过空行
|
|
if (IntStream.range(row.getFirstCellNum(), row.getLastCellNum())
|
|
if (IntStream.range(row.getFirstCellNum(), row.getLastCellNum())
|
|
.mapToObj(row::getCell)
|
|
.mapToObj(row::getCell)
|
|
@@ -231,7 +231,7 @@ public class ExcelUtils {
|
|
int rowsNum = sheet.getPhysicalNumberOfRows();
|
|
int rowsNum = sheet.getPhysicalNumberOfRows();
|
|
if (rowsNum <= 1) return dataList;
|
|
if (rowsNum <= 1) return dataList;
|
|
//页头行
|
|
//页头行
|
|
- Row headerRow = sheet.getRow(headerRowNum);
|
|
|
|
|
|
+ Row headerRow = sheet.getRow(Math.max(headerRowNum - 1, 0));
|
|
//字段映射
|
|
//字段映射
|
|
Map<Integer, Field> fieldMap = Arrays.stream(clazz.getDeclaredFields())
|
|
Map<Integer, Field> fieldMap = Arrays.stream(clazz.getDeclaredFields())
|
|
//过滤需要导出的字段
|
|
//过滤需要导出的字段
|
|
@@ -251,8 +251,8 @@ public class ExcelUtils {
|
|
));
|
|
));
|
|
//遍历行
|
|
//遍历行
|
|
for (Row row : sheet) {
|
|
for (Row row : sheet) {
|
|
- //跳过第非数据
|
|
|
|
- if (row == null || row.getRowNum() < dataRowNum) continue;
|
|
|
|
|
|
+ //跳过非数据
|
|
|
|
+ if (row == null || row.getRowNum() < (dataRowNum - 1)) continue;
|
|
//跳过空行
|
|
//跳过空行
|
|
if (IntStream.range(row.getFirstCellNum(), row.getLastCellNum())
|
|
if (IntStream.range(row.getFirstCellNum(), row.getLastCellNum())
|
|
.mapToObj(row::getCell)
|
|
.mapToObj(row::getCell)
|
|
@@ -288,9 +288,11 @@ public class ExcelUtils {
|
|
* @param datas 数据
|
|
* @param datas 数据
|
|
* @param outputStream 输出流
|
|
* @param outputStream 输出流
|
|
* @param excelEnum excel枚举
|
|
* @param excelEnum excel枚举
|
|
|
|
+ * @param headerRowNum 表头行
|
|
|
|
+ * @param dataRowNum 数据行
|
|
*/
|
|
*/
|
|
public static void writeFile(Map<String, Map<String, String>> headers, Map<String, List<Map<String, Object>>> datas
|
|
public static void writeFile(Map<String, Map<String, String>> headers, Map<String, List<Map<String, Object>>> datas
|
|
- , OutputStream outputStream, ExcelEnum excelEnum) {
|
|
|
|
|
|
+ , OutputStream outputStream, ExcelEnum excelEnum, Integer headerRowNum, Integer dataRowNum) {
|
|
//创建表
|
|
//创建表
|
|
try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new XSSFWorkbook() : new HSSFWorkbook()) {
|
|
try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new XSSFWorkbook() : new HSSFWorkbook()) {
|
|
//表头样式
|
|
//表头样式
|
|
@@ -309,7 +311,7 @@ public class ExcelUtils {
|
|
Sheet sheet = workbook.createSheet(headersEntry.getKey());
|
|
Sheet sheet = workbook.createSheet(headersEntry.getKey());
|
|
if (!datas.containsKey(headersEntry.getKey())) continue;
|
|
if (!datas.containsKey(headersEntry.getKey())) continue;
|
|
//写入页
|
|
//写入页
|
|
- writeSheet(headersEntry.getValue(), datas.get(headersEntry.getKey()), sheet, headerStyle);
|
|
|
|
|
|
+ writeSheet(headersEntry.getValue(), datas.get(headersEntry.getKey()), sheet, headerStyle, headerRowNum, dataRowNum);
|
|
}
|
|
}
|
|
//写入文件
|
|
//写入文件
|
|
workbook.write(outputStream);
|
|
workbook.write(outputStream);
|
|
@@ -324,17 +326,19 @@ public class ExcelUtils {
|
|
/**
|
|
/**
|
|
* 写入页
|
|
* 写入页
|
|
*
|
|
*
|
|
- * @param header 表头
|
|
|
|
- * @param data 数据
|
|
|
|
- * @param sheet 页
|
|
|
|
- * @param headerStyle 头样式
|
|
|
|
|
|
+ * @param header 表头
|
|
|
|
+ * @param data 数据
|
|
|
|
+ * @param sheet 页
|
|
|
|
+ * @param headerStyle 头样式
|
|
|
|
+ * @param headerRowNum 表头行
|
|
|
|
+ * @param dataRowNum 数据行
|
|
*/
|
|
*/
|
|
public static void writeSheet(Map<String, String> header, List<Map<String, Object>> data
|
|
public static void writeSheet(Map<String, String> header, List<Map<String, Object>> data
|
|
- , Sheet sheet, CellStyle headerStyle) {
|
|
|
|
|
|
+ , Sheet sheet, CellStyle headerStyle, Integer headerRowNum, Integer dataRowNum) {
|
|
//表头键
|
|
//表头键
|
|
List<String> headerKeys = new ArrayList<>();
|
|
List<String> headerKeys = new ArrayList<>();
|
|
//表头行
|
|
//表头行
|
|
- Row headRow = sheet.createRow(0);
|
|
|
|
|
|
+ Row headRow = sheet.createRow(Math.max(headerRowNum - 1, 0));
|
|
//遍历表头
|
|
//遍历表头
|
|
for (Map.Entry<String, String> headerEntry : header.entrySet()) {
|
|
for (Map.Entry<String, String> headerEntry : header.entrySet()) {
|
|
//缓存键
|
|
//缓存键
|
|
@@ -347,7 +351,7 @@ public class ExcelUtils {
|
|
//遍历数据
|
|
//遍历数据
|
|
for (int i = 0; i < data.size(); i++) {
|
|
for (int i = 0; i < data.size(); i++) {
|
|
Map<String, Object> item = data.get(i);
|
|
Map<String, Object> item = data.get(i);
|
|
- Row row = sheet.createRow(i + 1);
|
|
|
|
|
|
+ Row row = sheet.createRow(i + dataRowNum - 1);
|
|
//遍历表头键
|
|
//遍历表头键
|
|
for (int j = 0, headerKeysSize = headerKeys.size(); j < headerKeysSize; j++) {
|
|
for (int j = 0, headerKeysSize = headerKeys.size(); j < headerKeysSize; j++) {
|
|
//根据表头键,设置值
|
|
//根据表头键,设置值
|