Merge pull request #2213 from 120011676/v5-dev

V5 dev
This commit is contained in:
Golden Looly 2022-03-24 22:20:07 +08:00 committed by GitHub
commit 98e46d7634
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 0 deletions

View File

@ -50,6 +50,16 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
this(FileUtil.file(excelFilePath), sheetIndex); this(FileUtil.file(excelFilePath), sheetIndex);
} }
/**
* 构造
*
* @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径
* @param sheetName sheet名第一个默认是sheet1
*/
public ExcelReader(String excelFilePath, String sheetName) {
this(FileUtil.file(excelFilePath), sheetName);
}
/** /**
* 构造读写方式读取 * 构造读写方式读取
* *

View File

@ -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内容 * 获取Excel读取器通过调用{@link ExcelReader}的read或readXXX方法读取Excel内容
* *

View File

@ -4,6 +4,9 @@ import cn.hutool.poi.excel.cell.CellLocation;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.List;
import java.util.Map;
public class ExcelUtilTest { public class ExcelUtilTest {
@Test @Test
@ -50,4 +53,12 @@ public class ExcelUtilTest {
writer.writeCellValue(1, 2, "设置值"); writer.writeCellValue(1, 2, "设置值");
writer.close(); writer.close();
} }
@Test
public void getReaderByBookFilePathAndSheetNameTest() {
ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12");
List<Map<String, Object>> list = reader.readAll();
reader.close();
Assert.assertEquals(1L, list.get(1).get("鞋码"));
}
} }