From 0bc68d87a6e827183c056e06cefefb30a14ee2d9 Mon Sep 17 00:00:00 2001 From: yxx <1392859652@qq.com> Date: Thu, 11 Aug 2022 10:39:33 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E5=AF=BC=E5=85=A5=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E8=A1=8C=E8=A1=A8=E5=A4=B4=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kernel/office/api/OfficeExcelApi.java | 13 +++++++++- .../kernel/office/excel/OfficeExcel.java | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) 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..51e75878f 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) {