mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!919 修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题。
Merge pull request !919 from hellozrh/v5-dev
This commit is contained in:
commit
0e3cf48875
@ -1659,6 +1659,35 @@ 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定集合为空,返回默认集合
|
||||
*
|
||||
|
@ -1047,4 +1047,27 @@ 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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
package cn.hutool.poi.excel.sax;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.log.StaticLog;
|
||||
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
||||
import cn.hutool.poi.exceptions.POIException;
|
||||
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener;
|
||||
@ -216,7 +219,10 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
||||
if(this.rid < 0 && null != this.sheetName){
|
||||
throw new POIException("Sheet [{}] not exist!", this.sheetName);
|
||||
}
|
||||
processLastCellSheet();
|
||||
if(this.curRid != -1 && isProcessCurrentSheet()) {
|
||||
//只有在当前指定的sheet中,才触发结束事件,且curId=-1时也不处理,避免重复调用
|
||||
processLastCellSheet();
|
||||
}
|
||||
} else if (isProcessCurrentSheet()) {
|
||||
if (record instanceof MissingCellDummyRecord) {
|
||||
// 空值的操作
|
||||
|
@ -1,11 +1,22 @@
|
||||
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;
|
||||
|
||||
public class ExcelUtilTest {
|
||||
|
||||
@ -61,4 +72,28 @@ public class ExcelUtilTest {
|
||||
reader.close();
|
||||
Assert.assertEquals(1L, list.get(1).get("鞋码"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doAfterAllAnalysedTest() {
|
||||
String path = "readBySax.xls";
|
||||
AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0);
|
||||
try{
|
||||
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));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed() {
|
||||
doAfterAllAnalysedTime.addAndGet(1);
|
||||
}
|
||||
});
|
||||
}catch (Exception ex){
|
||||
ex.printStackTrace();
|
||||
}
|
||||
//总共2个sheet页,读取所有sheet时,一共执行doAfterAllAnalysed2次。
|
||||
Assert.assertEquals(2, doAfterAllAnalysedTime.intValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
BIN
hutool-poi/src/test/resources/readBySax.xls
Normal file
BIN
hutool-poi/src/test/resources/readBySax.xls
Normal file
Binary file not shown.
BIN
hutool-poi/src/test/resources/test.xls
Normal file
BIN
hutool-poi/src/test/resources/test.xls
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user