mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-15 20:49:32 +08:00
add test
This commit is contained in:
parent
23d23c2de6
commit
db36ce0c3c
@ -105,13 +105,13 @@ public class ExcelConfig {
|
||||
* @param headerList 原标题列表
|
||||
* @return 转换别名列表
|
||||
*/
|
||||
public List<String> aliasHeader(final List<Object> headerList) {
|
||||
public List<Object> aliasHeader(final List<Object> headerList) {
|
||||
if (CollUtil.isEmpty(headerList)) {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
final int size = headerList.size();
|
||||
final List<String> result = new ArrayList<>(size);
|
||||
final List<Object> result = new ArrayList<>(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
result.add(aliasHeader(headerList.get(i), i));
|
||||
}
|
||||
@ -125,16 +125,15 @@ public class ExcelConfig {
|
||||
* @param index 标题所在列号,当标题为空时,列号对应的字母便是header
|
||||
* @return 转换别名列表
|
||||
*/
|
||||
public String aliasHeader(final Object headerObj, final int index) {
|
||||
public Object aliasHeader(final Object headerObj, final int index) {
|
||||
if (null == headerObj) {
|
||||
return CellReferenceUtil.indexToColName(index);
|
||||
}
|
||||
|
||||
final String header = headerObj.toString();
|
||||
if (null != this.headerAlias) {
|
||||
return ObjUtil.defaultIfNull(this.headerAlias.get(header), header);
|
||||
return ObjUtil.defaultIfNull(this.headerAlias.get(headerObj.toString()), headerObj);
|
||||
}
|
||||
return header;
|
||||
return headerObj;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -246,7 +246,7 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelReadConfig> {
|
||||
*
|
||||
* @return Map的列表
|
||||
*/
|
||||
public List<Map<String, Object>> readAll() {
|
||||
public List<Map<Object, Object>> readAll() {
|
||||
return read(0, 1, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
@ -259,7 +259,7 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelReadConfig> {
|
||||
* @param endRowIndex 读取结束行(包含,从0开始计数)
|
||||
* @return Map的列表
|
||||
*/
|
||||
public List<Map<String, Object>> read(final int headerRowIndex, final int startRowIndex, final int endRowIndex) {
|
||||
public List<Map<Object, Object>> read(final int headerRowIndex, final int startRowIndex, final int endRowIndex) {
|
||||
final MapSheetReader reader = new MapSheetReader(headerRowIndex, startRowIndex, endRowIndex);
|
||||
reader.setExcelConfig(this.config);
|
||||
return read(reader);
|
||||
|
@ -53,14 +53,14 @@ public class BeanSheetReader<T> implements SheetReader<List<T>> {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<T> read(final Sheet sheet) {
|
||||
final List<Map<String, Object>> mapList = mapSheetReader.read(sheet);
|
||||
final List<Map<Object, Object>> mapList = mapSheetReader.read(sheet);
|
||||
if (Map.class.isAssignableFrom(this.beanClass)) {
|
||||
return (List<T>) mapList;
|
||||
}
|
||||
|
||||
final List<T> beanList = new ArrayList<>(mapList.size());
|
||||
final CopyOptions copyOptions = CopyOptions.of().setIgnoreError(true);
|
||||
for (final Map<String, Object> map : mapList) {
|
||||
for (final Map<Object, Object> map : mapList) {
|
||||
beanList.add(BeanUtil.toBean(map, this.beanClass, copyOptions));
|
||||
}
|
||||
return beanList;
|
||||
|
@ -63,7 +63,7 @@ public class ListSheetReader extends AbstractSheetReader<List<List<Object>>> {
|
||||
if (CollUtil.isNotEmpty(rowList) || !ignoreEmptyRow) {
|
||||
if (aliasFirstLine && i == startRowIndex) {
|
||||
// 第一行作为标题行,替换别名
|
||||
rowList = Convert.toList(Object.class, this.config.aliasHeader(rowList));
|
||||
rowList = this.config.aliasHeader(rowList);
|
||||
}
|
||||
resultList.add(rowList);
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ import java.util.Map;
|
||||
* @author looly
|
||||
* @since 5.4.4
|
||||
*/
|
||||
public class MapSheetReader extends AbstractSheetReader<List<Map<String, Object>>> {
|
||||
public class MapSheetReader extends AbstractSheetReader<List<Map<Object, Object>>> {
|
||||
|
||||
private final int headerRowIndex;
|
||||
|
||||
@ -50,7 +50,7 @@ public class MapSheetReader extends AbstractSheetReader<List<Map<String, Object>
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, Object>> read(final Sheet sheet) {
|
||||
public List<Map<Object, Object>> read(final Sheet sheet) {
|
||||
// 边界判断
|
||||
final int firstRowNum = sheet.getFirstRowNum();
|
||||
final int lastRowNum = sheet.getLastRowNum();
|
||||
@ -73,9 +73,9 @@ public class MapSheetReader extends AbstractSheetReader<List<Map<String, Object>
|
||||
final int endRowIndex = Math.min(this.cellRangeAddress.getLastRow(), lastRowNum);// 读取结束行(包含)
|
||||
|
||||
// 读取header
|
||||
final List<String> headerList = this.config.aliasHeader(readRow(sheet, headerRowIndex));
|
||||
final List<Object> headerList = this.config.aliasHeader(readRow(sheet, headerRowIndex));
|
||||
|
||||
final List<Map<String, Object>> result = new ArrayList<>(endRowIndex - startRowIndex + 1);
|
||||
final List<Map<Object, Object>> result = new ArrayList<>(endRowIndex - startRowIndex + 1);
|
||||
final boolean ignoreEmptyRow = this.config.isIgnoreEmptyRow();
|
||||
List<Object> rowList;
|
||||
for (int i = startRowIndex; i <= endRowIndex; i++) {
|
||||
|
@ -72,7 +72,7 @@ public class ExcelUtilTest {
|
||||
@Test
|
||||
public void getReaderByBookFilePathAndSheetNameTest() {
|
||||
final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12");
|
||||
final List<Map<String, Object>> list = reader.readAll();
|
||||
final List<Map<Object, Object>> list = reader.readAll();
|
||||
reader.close();
|
||||
Assertions.assertEquals(1L, list.get(1).get("鞋码"));
|
||||
}
|
||||
|
@ -58,8 +58,8 @@ public class ExcelReadTest {
|
||||
Assertions.assertEquals("checkPerm", read.get(0).get(2));
|
||||
Assertions.assertEquals("allotAuditPerm", read.get(0).get(3));
|
||||
|
||||
final List<Map<String, Object>> readAll = reader.readAll();
|
||||
for (final Map<String, Object> map : readAll) {
|
||||
final List<Map<Object, Object>> readAll = reader.readAll();
|
||||
for (final Map<Object, Object> map : readAll) {
|
||||
Assertions.assertTrue(map.containsKey("userName"));
|
||||
Assertions.assertTrue(map.containsKey("storageName"));
|
||||
Assertions.assertTrue(map.containsKey("checkPerm"));
|
||||
@ -70,7 +70,7 @@ public class ExcelReadTest {
|
||||
@Test
|
||||
public void excelReadTestOfEmptyLine() {
|
||||
final ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("priceIndex.xls"));
|
||||
final List<Map<String, Object>> readAll = reader.readAll();
|
||||
final List<Map<Object, Object>> readAll = reader.readAll();
|
||||
|
||||
Assertions.assertEquals(4, readAll.size());
|
||||
}
|
||||
@ -137,7 +137,7 @@ public class ExcelReadTest {
|
||||
@Test
|
||||
public void excelReadToMapListTest() {
|
||||
final ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
|
||||
final List<Map<String, Object>> readAll = reader.readAll();
|
||||
final List<Map<Object, Object>> readAll = reader.readAll();
|
||||
|
||||
Assertions.assertEquals("张三", readAll.get(0).get("姓名"));
|
||||
Assertions.assertEquals("男", readAll.get(0).get("性别"));
|
||||
@ -241,7 +241,7 @@ public class ExcelReadTest {
|
||||
@Disabled
|
||||
public void readEmptyTest(){
|
||||
final ExcelReader reader = ExcelUtil.getReader("d:/test/issue.xlsx");
|
||||
final List<Map<String, Object>> maps = reader.readAll();
|
||||
final List<Map<Object, Object>> maps = reader.readAll();
|
||||
Console.log(maps);
|
||||
}
|
||||
|
||||
@ -274,14 +274,14 @@ public class ExcelReadTest {
|
||||
public void readIssueTest() {
|
||||
//https://gitee.com/dromara/hutool/issues/I5OSFC
|
||||
final ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("read.xlsx"));
|
||||
final List<Map<String, Object>> read = reader.read(1,2,2);
|
||||
for (final Map<String, Object> map : read) {
|
||||
final List<Map<Object, Object>> read = reader.read(1,2,2);
|
||||
for (final Map<Object, Object> map : read) {
|
||||
Console.log(map);
|
||||
}
|
||||
//超出lastIndex 抛出相应提示:startRowIndex row index 4 is greater than last row index 2.
|
||||
//而非:Illegal Capacity: -1
|
||||
try {
|
||||
final List<Map<String, Object>> readGreaterIndex = reader.read(1,4,4);
|
||||
final List<Map<Object, Object>> readGreaterIndex = reader.read(1,4,4);
|
||||
} catch (final Exception e) {
|
||||
Console.log(e.toString());
|
||||
}
|
||||
|
@ -16,8 +16,8 @@ public class IssueI8PT9ZTest {
|
||||
@Disabled
|
||||
void readTest() {
|
||||
final ExcelReader reader = ExcelUtil.getReader("d:/test/test.xlsx");
|
||||
final List<Map<String, Object>> read = reader.read(2, 4, Integer.MAX_VALUE);
|
||||
for (final Map<String, Object> stringObjectMap : read) {
|
||||
final List<Map<Object, Object>> read = reader.read(2, 4, Integer.MAX_VALUE);
|
||||
for (final Map<Object, Object> stringObjectMap : read) {
|
||||
Console.log(stringObjectMap);
|
||||
}
|
||||
|
||||
|
@ -12,15 +12,16 @@
|
||||
|
||||
package org.dromara.hutool.poi.excel.writer;
|
||||
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.poi.excel.ExcelUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TemplateContextTest {
|
||||
@Test
|
||||
void readTemplate() {
|
||||
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/template.xlsx");
|
||||
final ExcelWriter writer = ExcelUtil.getWriter("template.xlsx");
|
||||
final TemplateContext templateContext = new TemplateContext(writer.getSheet());
|
||||
Console.log(templateContext);
|
||||
Assertions.assertNotNull(templateContext.getCell("date"));
|
||||
Assertions.assertNotNull(templateContext.getCell(".month"));
|
||||
}
|
||||
}
|
||||
|
BIN
hutool-poi/src/test/resources/template.xlsx
Normal file
BIN
hutool-poi/src/test/resources/template.xlsx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user