XLSReader 수정

master
mjkhan21 6 months ago
parent 858a3fdb95
commit 25e686287c

@ -30,6 +30,7 @@ public abstract class XLS extends AbstractComponent {
protected Row row;
/**현재 처리 중인 cell*/
protected Cell cell;
protected boolean sxssf;
/** InputStream .
* @param <T> XLS
@ -67,8 +68,9 @@ public abstract class XLS extends AbstractComponent {
try {
workbook = newWorkbook(null);
if (workbook instanceof SXSSFWorkbook swb)
swb.setCompressTempFiles(true);
sxssf = workbook instanceof SXSSFWorkbook;
if (sxssf)
((SXSSFWorkbook)workbook).setCompressTempFiles(true);
return workbook;
} catch (Exception e) {
throw runtimeException(e);
@ -93,12 +95,13 @@ public abstract class XLS extends AbstractComponent {
try {
sheet = workbook().getSheetAt(index);
} catch (Exception e) {
sheet = workbook().createSheet();
if (sheet instanceof SXSSFSheet sst)
sst.setRandomAccessWindowSize(rowAccessWindowSize);
if (sxssf) {
sheet = workbook().createSheet();
((SXSSFSheet)sheet).setRandomAccessWindowSize(rowAccessWindowSize);
}
}
if (!sheet.equals(worksheet)) {
if (!equals(sheet, worksheet)) {
clear(false);
worksheet = sheet;
}
@ -116,13 +119,12 @@ public abstract class XLS extends AbstractComponent {
try {
Workbook wb = workbook();
sheet = wb.getSheet(name);
if (sheet == null) {
if (sheet == null && sxssf) {
sheet = wb.createSheet(name);
if (sheet instanceof SXSSFSheet sst)
sst.setRandomAccessWindowSize(rowAccessWindowSize);
((SXSSFSheet)sheet).setRandomAccessWindowSize(rowAccessWindowSize);
}
if (!sheet.equals(worksheet)) {
if (!equals(sheet, worksheet)) {
clear(false);
worksheet = sheet;
}
@ -139,7 +141,9 @@ public abstract class XLS extends AbstractComponent {
* @return XLS
*/
public XLS row(int index) {
row = ifEmpty(worksheet.getRow(index), () -> worksheet.createRow(index));
row = worksheet.getRow(index);
if (row == null && sxssf)
row = worksheet.createRow(index);
return this;
}
@ -149,7 +153,9 @@ public abstract class XLS extends AbstractComponent {
* @return XLS
*/
public XLS col(int index) {
cell = ifEmpty(row.getCell(index), () -> row.createCell(index));
cell = row != null ? row.getCell(index) : null;
if (cell == null && sxssf)
cell = row.createCell(index);
return this;
}
@ -162,6 +168,10 @@ public abstract class XLS extends AbstractComponent {
return row(row).col(col);
}
public Position cellPosition() {
return cell != null ? new Position(cell.getRowIndex(), cell.getColumnIndex()) : Position.NULL;
}
/** XLS row, cell, worksheet .
* @param all
* <ul><li> true</li>
@ -177,6 +187,26 @@ public abstract class XLS extends AbstractComponent {
rowAccessWindowSize = 128;
}
public static class Position {
private static final Position NULL = new Position(-1, -1);
private int row;
private int col;
public Position(int row, int col) {
this.row = row;
this.col = col;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
}
/**
* @author mjkhan
*/

@ -2,6 +2,11 @@ package cokr.xit.base.docs.xls;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
@ -46,7 +51,7 @@ public class XLSReader extends XLS {
return row(row).col(col);
}
public String valueOf(Cell cell) {
public String value(Cell cell) {
return switch (cell.getCellType()) {
case STRING -> cell.getStringCellValue();
case NUMERIC -> DateUtil.isCellDateFormatted(cell) ?
@ -57,4 +62,56 @@ public class XLSReader extends XLS {
default -> "";
};
}
public String currentValue() {
if (cell == null)
return null;
return value(cell);
}
public String value(int row, int col) {
return cell(row, col).currentValue();
}
public List<String> rowValues(int index) {
row(index);
if (row == null)
return null;
return IntStream.range(row.getFirstCellNum(), row.getLastCellNum()).boxed()
.map(num -> cell(index, num).currentValue())
.toList();
}
public <T> T setRowValues(List<String> values, Supplier<T> target, ValueSetter<T> setter) {
T t = target.get();
for (int i = 0, size = values.size(); i < size; ++i) {
setter.set(t, i, values.get(i));
}
return t;
}
public <T> T setRowValues(int index, Supplier<T> target, ValueSetter<T> setter) {
return setRowValues(rowValues(index), target, setter);
}
public <T> Stream<T> setValues(int row, Supplier<T> target, ValueSetter<T> setter, BiPredicate<Integer, List<String>> terminate) {
Stream.Builder<T> builder = Stream.builder();
while (true) {
List<String> rowValues = rowValues(row);
if (terminate.test(row, rowValues))
break;
T t = setRowValues(rowValues, target, setter);
builder.add(t);
++row;
}
return builder.build();
}
@FunctionalInterface
public static interface ValueSetter<T> {
void set(T target, int index, String val);
}
}

@ -179,15 +179,54 @@ public class XLSTest {
xlsx.autoWidth();
xlsx.write(file("xlsx-test.xlsx"));
xlsx.write(file("xlsx-write-test.xlsx"));
}
@Test
void read() {
int row = 0,
col = 0;
XLSReader xlsx = new XLSReader()
.open(new File("src\\test\\resources\\file\\xlsx-test.xlsx"))
.open(new File("src\\test\\resources\\xlsx-read-test.xlsx"))
.worksheet(0)
.cell(0, 0);
.cell(row, col);
String val = xlsx.currentValue();
XLSReader.Position cellPosition = xlsx.cellPosition();
System.out.println(String.format("(%d, %d) = %s", cellPosition.getRow(), cellPosition.getCol(), val));
val = xlsx.value(row = 1, col = 0);
cellPosition = xlsx.cellPosition();
System.out.println(String.format("(%d, %d) = %s", cellPosition.getRow(), cellPosition.getCol(), val));
val = xlsx.value(row = 2, col = 1);
cellPosition = xlsx.cellPosition();
System.out.println(String.format("(%d, %d) = %s", cellPosition.getRow(), cellPosition.getCol(), val));
val = xlsx.value(row = 3, col = 1);
cellPosition = xlsx.cellPosition();
System.out.println(String.format("(%d, %d) = %s", cellPosition.getRow(), cellPosition.getCol(), val));
System.out.println(xlsx.rowValues(row = 6));
cellPosition = xlsx.cellPosition();
System.out.println(String.format("cellPosition = (%d, %d)", cellPosition.getRow(), cellPosition.getCol()));
XLSReader.ValueSetter<DataObject> setter = (target, index, str) -> {
switch (index) {
case 0: target.put("field1", str); break;
case 1: target.put("field2", str); break;
case 2: target.put("field3", str); break;
case 3: target.put("date", str); break;
case 6: target.put("etc", str); break;
}
};
DataObject dataobject = xlsx.setRowValues(row = 6, DataObject::new, setter);
System.out.println(dataobject);
xlsx.setValues(row, DataObject::new, setter, (index, values) -> values == null).forEach(System.out::println);
cellPosition = xlsx.cellPosition();
System.out.println(String.format("cellPosition = (%d, %d)", cellPosition.getRow(), cellPosition.getCol()));
}
static class TestObject {

Loading…
Cancel
Save