mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题
This commit is contained in:
parent
0e3cf48875
commit
771f0a4809
@ -23,6 +23,7 @@
|
||||
* 【core 】 修复ClassScanner自定义classload无效问题(issue#I68TV2@Gitee)
|
||||
* 【core 】 【重要】删除XmlUtil.readObjectFromXml方法,避免漏洞(issue#2857@Github)
|
||||
* 【core 】 修复Ipv4Util.list()方法的bug(pr#929@Gitee)
|
||||
* 【poi 】 修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题。(pr#919@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -1659,35 +1659,6 @@ public class CollUtil {
|
||||
return collection == null || collection.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合是否为空。
|
||||
* 如果集合中所有元素为null或空串,也认为此集合为空。
|
||||
* @param collection
|
||||
* @return
|
||||
*/
|
||||
public static boolean isBlank(Collection<?> collection) {
|
||||
if(isEmpty(collection)){
|
||||
return true;
|
||||
}
|
||||
|
||||
for(Object o: collection){
|
||||
if(ObjectUtil.isNotEmpty(o)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合是否为非空。
|
||||
* 集合长度大于0,且所有元素中至少有一个不为null或空串。
|
||||
* @param collection
|
||||
* @return
|
||||
*/
|
||||
public static boolean isNotBlank(Collection<?> collection) {
|
||||
return false == isBlank(collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定集合为空,返回默认集合
|
||||
*
|
||||
|
@ -941,8 +941,7 @@ public class CollUtilTest {
|
||||
final List<String> list = CollUtil.unionAll(list1, list2, list3);
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
|
||||
final List<String> resList2 = CollUtil.unionAll(null, null, null);
|
||||
@SuppressWarnings("ConfusingArgumentToVarargsMethod") final List<String> resList2 = CollUtil.unionAll(null, null, null);
|
||||
Assert.assertNotNull(resList2);
|
||||
}
|
||||
|
||||
@ -973,8 +972,7 @@ public class CollUtilTest {
|
||||
public void unionAllOtherIsNullTest() {
|
||||
final List<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3);
|
||||
final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3);
|
||||
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
|
||||
final List<Integer> list = CollUtil.unionAll(list1, list2, null);
|
||||
@SuppressWarnings("ConfusingArgumentToVarargsMethod") final List<Integer> list = CollUtil.unionAll(list1, list2, null);
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertArrayEquals(
|
||||
CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(),
|
||||
@ -1047,27 +1045,4 @@ public class CollUtilTest {
|
||||
final Object first = CollUtil.getFirst(nullList);
|
||||
Assert.assertNull(first);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void blankTest() {
|
||||
List<String> strs = new ArrayList<>();
|
||||
strs.add(null);
|
||||
strs.add("");
|
||||
strs.add("");
|
||||
|
||||
boolean c = CollUtil.isBlank(strs);
|
||||
Assert.assertEquals(true, c );
|
||||
|
||||
|
||||
List<String> arrs = new ArrayList<>();
|
||||
arrs.add(null);
|
||||
arrs.add("");
|
||||
arrs.add(" ");
|
||||
arrs.add("");
|
||||
arrs.add(" a ");
|
||||
|
||||
boolean d = CollUtil.isNotBlank(arrs);
|
||||
Assert.assertEquals(true, d );
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class ExcelSaxReadTest {
|
||||
|
||||
@Test
|
||||
public void excel03Test() {
|
||||
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
reader.read("aaa.xls", 1);
|
||||
|
||||
// Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName());
|
||||
@ -60,7 +60,7 @@ public class ExcelSaxReadTest {
|
||||
|
||||
@Test
|
||||
public void excel03ByNameTest() {
|
||||
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
reader.read("aaa.xls", "校园入学");
|
||||
reader.read("aaa.xls", "sheetName:校园入学");
|
||||
}
|
||||
@ -68,7 +68,7 @@ public class ExcelSaxReadTest {
|
||||
@Test(expected = POIException.class)
|
||||
public void excel03ByNameErrorTest() {
|
||||
// sheet名称不存在则报错
|
||||
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
reader.read("aaa.xls", "校园入学1");
|
||||
}
|
||||
|
||||
@ -120,12 +120,12 @@ public class ExcelSaxReadTest {
|
||||
ExcelUtil.readBySax("d:/test/test.xlsx", -1, new RowHandler() {
|
||||
|
||||
@Override
|
||||
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {
|
||||
public void handleCell(final int sheetIndex, final long rowIndex, final int cellIndex, final Object value, final CellStyle xssfCellStyle) {
|
||||
Console.log("{} {} {}", rowIndex, cellIndex, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) {
|
||||
public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -133,17 +133,16 @@ public class ExcelSaxReadTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void handle03CellTest() {
|
||||
ExcelUtil.readBySax("d:/test/test.xls", -1, new RowHandler() {
|
||||
ExcelUtil.readBySax("test.xls", -1, new RowHandler() {
|
||||
|
||||
@Override
|
||||
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {
|
||||
Console.log("{} {} {}", rowIndex, cellIndex, value);
|
||||
public void handleCell(final int sheetIndex, final long rowIndex, final int cellIndex, final Object value, final CellStyle xssfCellStyle) {
|
||||
//Console.log("{} {} {}", rowIndex, cellIndex, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) {
|
||||
public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
|
||||
}
|
||||
}
|
||||
);
|
||||
@ -151,7 +150,7 @@ public class ExcelSaxReadTest {
|
||||
|
||||
@Test
|
||||
public void formulaRead03Test() {
|
||||
List<Object> rows = new ArrayList<>();
|
||||
final List<Object> rows = new ArrayList<>();
|
||||
ExcelUtil.readBySax("data_for_sax_test.xls", -1, (i, i1, list) -> {
|
||||
if (list.size() > 1) {
|
||||
rows.add(list.get(1));
|
||||
@ -164,7 +163,7 @@ public class ExcelSaxReadTest {
|
||||
|
||||
@Test
|
||||
public void formulaRead07Test() {
|
||||
List<Object> rows = new ArrayList<>();
|
||||
final List<Object> rows = new ArrayList<>();
|
||||
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) ->
|
||||
rows.add(list.get(1)));
|
||||
|
||||
@ -174,7 +173,7 @@ public class ExcelSaxReadTest {
|
||||
|
||||
@Test
|
||||
public void dateReadXlsTest() {
|
||||
List<String> rows = new ArrayList<>();
|
||||
final List<String> rows = new ArrayList<>();
|
||||
ExcelUtil.readBySax("data_for_sax_test.xls", 0,
|
||||
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
|
||||
);
|
||||
@ -188,7 +187,7 @@ public class ExcelSaxReadTest {
|
||||
|
||||
@Test
|
||||
public void dateReadXlsxTest() {
|
||||
List<String> rows = new ArrayList<>();
|
||||
final List<String> rows = new ArrayList<>();
|
||||
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0,
|
||||
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
|
||||
);
|
||||
@ -211,7 +210,7 @@ public class ExcelSaxReadTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void readBlankTest() {
|
||||
File file = new File("D:/test/b.xlsx");
|
||||
final File file = new File("D:/test/b.xlsx");
|
||||
|
||||
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log));
|
||||
|
||||
|
@ -1,19 +1,10 @@
|
||||
package cn.hutool.poi.excel;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.poi.excel.cell.CellLocation;
|
||||
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@ -59,29 +50,28 @@ public class ExcelUtilTest {
|
||||
|
||||
@Test
|
||||
public void readAndWriteTest() {
|
||||
ExcelReader reader = ExcelUtil.getReader("aaa.xlsx");
|
||||
ExcelWriter writer = reader.getWriter();
|
||||
final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx");
|
||||
final ExcelWriter writer = reader.getWriter();
|
||||
writer.writeCellValue(1, 2, "设置值");
|
||||
writer.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReaderByBookFilePathAndSheetNameTest() {
|
||||
ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12");
|
||||
List<Map<String, Object>> list = reader.readAll();
|
||||
final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12");
|
||||
final List<Map<String, Object>> list = reader.readAll();
|
||||
reader.close();
|
||||
Assert.assertEquals(1L, list.get(1).get("鞋码"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doAfterAllAnalysedTest() {
|
||||
String path = "readBySax.xls";
|
||||
AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0);
|
||||
try{
|
||||
final String path = "readBySax.xls";
|
||||
final AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0);
|
||||
ExcelUtil.readBySax(path, -1, new RowHandler() {
|
||||
@Override
|
||||
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) {
|
||||
System.out.println(StrUtil.format("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells));
|
||||
public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
|
||||
//Console.log("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -89,9 +79,6 @@ public class ExcelUtilTest {
|
||||
doAfterAllAnalysedTime.addAndGet(1);
|
||||
}
|
||||
});
|
||||
}catch (Exception ex){
|
||||
ex.printStackTrace();
|
||||
}
|
||||
//总共2个sheet页,读取所有sheet时,一共执行doAfterAllAnalysed2次。
|
||||
Assert.assertEquals(2, doAfterAllAnalysedTime.intValue());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user