mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
e80ef50246
commit
dd3ea66a95
@ -13,6 +13,7 @@
|
|||||||
package org.dromara.hutool.poi.excel;
|
package org.dromara.hutool.poi.excel;
|
||||||
|
|
||||||
import org.dromara.hutool.core.collection.ListUtil;
|
import org.dromara.hutool.core.collection.ListUtil;
|
||||||
|
import org.dromara.hutool.core.lang.Console;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.poi.excel.cell.CellEditor;
|
import org.dromara.hutool.poi.excel.cell.CellEditor;
|
||||||
import org.dromara.hutool.poi.excel.cell.CellUtil;
|
import org.dromara.hutool.poi.excel.cell.CellUtil;
|
||||||
|
@ -47,7 +47,17 @@ public class Excel07SaxReader implements ExcelSaxReader<Excel07SaxReader> {
|
|||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
*/
|
*/
|
||||||
public Excel07SaxReader(final RowHandler rowHandler) {
|
public Excel07SaxReader(final RowHandler rowHandler) {
|
||||||
this.handler = new SheetDataSaxHandler(rowHandler);
|
this(rowHandler, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param rowHandler 行处理器
|
||||||
|
* @param padCellAtEndOfRow 是否对齐数据,即在行尾补充null cell
|
||||||
|
*/
|
||||||
|
public Excel07SaxReader(final RowHandler rowHandler, final boolean padCellAtEndOfRow) {
|
||||||
|
this.handler = new SheetDataSaxHandler(rowHandler, padCellAtEndOfRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.poi.excel.sax;
|
package org.dromara.hutool.poi.excel.sax;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.lang.Console;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
import org.dromara.hutool.poi.excel.cell.values.FormulaCellValue;
|
import org.dromara.hutool.poi.excel.cell.values.FormulaCellValue;
|
||||||
@ -37,6 +38,13 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class SheetDataSaxHandler extends DefaultHandler {
|
public class SheetDataSaxHandler extends DefaultHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 行处理器
|
||||||
|
*/
|
||||||
|
protected RowHandler rowHandler;
|
||||||
|
// 配置项:是否对齐数据,即在行尾补充null cell
|
||||||
|
private final boolean padCellAtEndOfRow;
|
||||||
|
|
||||||
// 单元格的格式表,对应style.xml
|
// 单元格的格式表,对应style.xml
|
||||||
protected StylesTable stylesTable;
|
protected StylesTable stylesTable;
|
||||||
// excel 2007 的共享字符串表,对应sharedString.xml
|
// excel 2007 的共享字符串表,对应sharedString.xml
|
||||||
@ -78,16 +86,13 @@ public class SheetDataSaxHandler extends DefaultHandler {
|
|||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
|
* @param padCellAtEndOfRow 是否对齐数据,即在行尾补充null cell
|
||||||
*/
|
*/
|
||||||
public SheetDataSaxHandler(final RowHandler rowHandler) {
|
public SheetDataSaxHandler(final RowHandler rowHandler, final boolean padCellAtEndOfRow) {
|
||||||
this.rowHandler = rowHandler;
|
this.rowHandler = rowHandler;
|
||||||
|
this.padCellAtEndOfRow = padCellAtEndOfRow;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 行处理器
|
|
||||||
*/
|
|
||||||
protected RowHandler rowHandler;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置行处理器
|
* 设置行处理器
|
||||||
*
|
*
|
||||||
@ -228,8 +233,8 @@ public class SheetDataSaxHandler extends DefaultHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 补全一行尾部可能缺失的单元格
|
// 补全一行尾部可能缺失的单元格
|
||||||
if (maxCellCoordinate != null) {
|
if (padCellAtEndOfRow && maxCellCoordinate != null) {
|
||||||
fillBlankCell(curCoordinate, maxCellCoordinate, true);
|
padCell(curCoordinate, maxCellCoordinate, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
rowHandler.handle(sheetIndex, rowNumber, rowCellList);
|
rowHandler.handle(sheetIndex, rowNumber, rowCellList);
|
||||||
@ -251,7 +256,7 @@ public class SheetDataSaxHandler extends DefaultHandler {
|
|||||||
*/
|
*/
|
||||||
private void endCell() {
|
private void endCell() {
|
||||||
// 补全单元格之间的空格
|
// 补全单元格之间的空格
|
||||||
fillBlankCell(preCoordinate, curCoordinate, false);
|
padCell(preCoordinate, curCoordinate, false);
|
||||||
|
|
||||||
final String contentStr = StrUtil.trim(lastContent);
|
final String contentStr = StrUtil.trim(lastContent);
|
||||||
Object value = ExcelSaxUtil.getDataValue(this.cellDataType, contentStr, this.sharedStrings, this.numFmtString);
|
Object value = ExcelSaxUtil.getDataValue(this.cellDataType, contentStr, this.sharedStrings, this.numFmtString);
|
||||||
@ -279,14 +284,14 @@ public class SheetDataSaxHandler extends DefaultHandler {
|
|||||||
* @param curCoordinate 当前单元格坐标
|
* @param curCoordinate 当前单元格坐标
|
||||||
* @param isEnd 是否为最后一个单元格
|
* @param isEnd 是否为最后一个单元格
|
||||||
*/
|
*/
|
||||||
private void fillBlankCell(final String preCoordinate, final String curCoordinate, final boolean isEnd) {
|
private void padCell(final String preCoordinate, final String curCoordinate, final boolean isEnd) {
|
||||||
if (!curCoordinate.equals(preCoordinate)) {
|
if (!curCoordinate.equals(preCoordinate)) {
|
||||||
int len = ExcelSaxUtil.countNullCell(preCoordinate, curCoordinate);
|
int len = ExcelSaxUtil.countNullCell(preCoordinate, curCoordinate);
|
||||||
if (isEnd) {
|
if (isEnd) {
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
while (len-- > 0) {
|
while (len-- > 0) {
|
||||||
addCellValue(curCell++, StrUtil.EMPTY);
|
addCellValue(curCell++, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,11 +166,16 @@ public class ExcelSaxReadTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formulaRead07Test() {
|
public void formulaRead07Test() {
|
||||||
|
// since 6.0.0修改
|
||||||
|
// 默认不在行尾对齐单元格,因此只读取了有第二个值的行
|
||||||
final List<Object> rows = new ArrayList<>();
|
final List<Object> rows = new ArrayList<>();
|
||||||
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) ->
|
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) -> {
|
||||||
rows.add(list.get(1)));
|
if(list.size() > 1){
|
||||||
|
rows.add(list.get(1));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
final FormulaCellValue value = (FormulaCellValue) rows.get(3);
|
final FormulaCellValue value = (FormulaCellValue) rows.get(1);
|
||||||
Assertions.assertEquals(50L, value.getResult());
|
Assertions.assertEquals(50L, value.getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,12 +216,13 @@ public class ExcelSaxReadTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
//@Disabled
|
||||||
public void readBlankTest() {
|
public void readBlankTest() {
|
||||||
final File file = new File("D:/test/b.xlsx");
|
final File file = FileUtil.file("aaa.xlsx");
|
||||||
|
|
||||||
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log));
|
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> Console.log(rowList));
|
||||||
|
|
||||||
|
Console.log("-------------------------------------");
|
||||||
ExcelUtil.getReader(file).read().forEach(Console::log);
|
ExcelUtil.getReader(file).read().forEach(Console::log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user