diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelReader.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelReader.java index bac3a4caa..de4dd6835 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelReader.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelReader.java @@ -50,6 +50,16 @@ public class ExcelReader extends ExcelBase { this(FileUtil.file(excelFilePath), sheetIndex); } + /** + * 构造 + * + * @param excelFilePath Excel文件路径,绝对路径或相对于ClassPath路径 + * @param sheetName sheet名,第一个默认是sheet1 + */ + public ExcelReader(String excelFilePath, String sheetName) { + this(FileUtil.file(excelFilePath), sheetName); + } + /** * 构造(读写方式读取) * diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java index 8db7e1ced..b15823eb5 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java @@ -154,6 +154,22 @@ public class ExcelUtil { } } + /** + * 获取Excel读取器,通过调用{@link ExcelReader}的read或readXXX方法读取Excel内容 + * + * @param bookFilePath Excel文件路径,绝对路径或相对于ClassPath路径 + * @param sheetName sheet名,第一个默认是sheet1 + * @return {@link ExcelReader} + * @since 3.1.1 + */ + public static ExcelReader getReader(String bookFilePath, String sheetName) { + try { + return new ExcelReader(bookFilePath, sheetName); + } catch (NoClassDefFoundError e) { + throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG); + } + } + /** * 获取Excel读取器,通过调用{@link ExcelReader}的read或readXXX方法读取Excel内容 * diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java index 0be195739..2b66d5754 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java @@ -4,6 +4,9 @@ import cn.hutool.poi.excel.cell.CellLocation; import org.junit.Assert; import org.junit.Test; +import java.util.List; +import java.util.Map; + public class ExcelUtilTest { @Test @@ -50,4 +53,12 @@ public class ExcelUtilTest { writer.writeCellValue(1, 2, "设置值"); writer.close(); } + + @Test + public void getReaderByBookFilePathAndSheetNameTest() { + ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12"); + List> list = reader.readAll(); + reader.close(); + Assert.assertEquals(1L, list.get(1).get("鞋码")); + } } diff --git a/hutool-poi/src/test/resources/aaa.xlsx b/hutool-poi/src/test/resources/aaa.xlsx index 97ba4bfad..ac6d3814b 100644 Binary files a/hutool-poi/src/test/resources/aaa.xlsx and b/hutool-poi/src/test/resources/aaa.xlsx differ