mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题
This commit is contained in:
@@ -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,11 +150,11 @@ 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){
|
||||
if (list.size() > 1) {
|
||||
rows.add(list.get(1));
|
||||
} else{
|
||||
} else {
|
||||
rows.add("");
|
||||
}
|
||||
});
|
||||
@@ -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));
|
||||
|
||||
@@ -220,7 +219,7 @@ public class ExcelSaxReadTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void readXlsmTest(){
|
||||
public void readXlsmTest() {
|
||||
ExcelUtil.readBySax("d:/test/WhiteListTemplate.xlsm", -1,
|
||||
(sheetIndex, rowIndex, rowlist) -> Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist));
|
||||
}
|
||||
|
@@ -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,39 +50,35 @@ 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{
|
||||
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));
|
||||
}
|
||||
final String path = "readBySax.xls";
|
||||
final AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0);
|
||||
ExcelUtil.readBySax(path, -1, new RowHandler() {
|
||||
@Override
|
||||
public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
|
||||
//Console.log("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed() {
|
||||
doAfterAllAnalysedTime.addAndGet(1);
|
||||
}
|
||||
});
|
||||
}catch (Exception ex){
|
||||
ex.printStackTrace();
|
||||
}
|
||||
@Override
|
||||
public void doAfterAllAnalysed() {
|
||||
doAfterAllAnalysedTime.addAndGet(1);
|
||||
}
|
||||
});
|
||||
//总共2个sheet页,读取所有sheet时,一共执行doAfterAllAnalysed2次。
|
||||
Assert.assertEquals(2, doAfterAllAnalysedTime.intValue());
|
||||
}
|
||||
|
Reference in New Issue
Block a user