diff --git a/kernel-d-office/office-api/src/main/java/cn/stylefeng/roses/kernel/office/api/OfficeExcelApi.java b/kernel-d-office/office-api/src/main/java/cn/stylefeng/roses/kernel/office/api/OfficeExcelApi.java index 93ecd055c..215db69c0 100644 --- a/kernel-d-office/office-api/src/main/java/cn/stylefeng/roses/kernel/office/api/OfficeExcelApi.java +++ b/kernel-d-office/office-api/src/main/java/cn/stylefeng/roses/kernel/office/api/OfficeExcelApi.java @@ -61,10 +61,21 @@ public interface OfficeExcelApi { * @param inputStream 流输入Excel文件的流对象 * @param clazz 每行数据转换成的对象类 * @return 对象类List集合 - * * @author luojie * @date 2020/11/4 13:54 */ List easyReadToList(InputStream inputStream, Class clazz); + /** + * 简单的读取Excel文件并返回实体类List集合-针对多行表头 + * + * @param inputStream 流输入Excel文件的流对象 + * @param clazz 每行数据转换成的对象类 + * @param rowNum 表头所占行数 + * @return 对象类List集合 + * @author yxx + * @date 2022/8/11 10:37 + */ + List easyReadToList(InputStream inputStream, Integer rowNum, Class clazz); + } diff --git a/kernel-d-office/office-sdk-excel/src/main/java/cn/stylefeng/roses/kernel/office/excel/OfficeExcel.java b/kernel-d-office/office-sdk-excel/src/main/java/cn/stylefeng/roses/kernel/office/excel/OfficeExcel.java index 50bf7cbcc..85d90f4c8 100644 --- a/kernel-d-office/office-sdk-excel/src/main/java/cn/stylefeng/roses/kernel/office/excel/OfficeExcel.java +++ b/kernel-d-office/office-sdk-excel/src/main/java/cn/stylefeng/roses/kernel/office/excel/OfficeExcel.java @@ -77,6 +77,30 @@ public class OfficeExcel implements OfficeExcelApi { return readListener.getDataList(); } + @Override + public List easyReadToList(InputStream inputStream, Integer rowNum, Class clazz) { + if (inputStream == null) { + return new ArrayList(); + } + + // 创建一个简单的数据监听器 + SimpleDataListener readListener = new SimpleDataListener(); + + // 读取文件 + try { + EasyExcel.read(inputStream, clazz, readListener).sheet().headRowNumber(rowNum).doRead(); + } catch (Exception e) { + log.error(e.getMessage()); + + // 组装提示信息 + String userTip = OfficeExceptionEnum.OFFICE_ERROR.getUserTip(); + String finalUserTip = StrUtil.format(userTip, e.getMessage()); + throw new OfficeException(OfficeExceptionEnum.OFFICE_ERROR.getErrorCode(), finalUserTip); + } + + return readListener.getDataList(); + } + @Override public void easyWriteToFile(ExcelExportParam excelExportParam) { @@ -122,7 +146,7 @@ public class OfficeExcel implements OfficeExcelApi { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode(excelExportParam.getFileName(), "UTF-8").replaceAll("\\+", "%20"); - response.setHeader("Content-disposition", String.format("%s%s", "attachment;filename*=utf-8''", fileName, excelTypeEnum.getValue())); + response.setHeader("Content-disposition", String.format("%s%s%s", "attachment;filename*=utf-8''", fileName, excelTypeEnum.getValue())); EasyExcel.write(response.getOutputStream(), excelExportParam.getClazz()).excelType(excelTypeEnum).sheet(excelExportParam.getSheetName()).doWrite(excelExportParam.getDataList());