Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/main/java/com/monitorjbl/xlsx/StreamingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,11 @@ public Builder sstCacheSizeBytes(int sstCacheSizeBytes) {
* @throws com.monitorjbl.xlsx.exceptions.ReadException if there is an issue reading the stream
*/
public Workbook open(InputStream is) {
StreamingWorkbookReader workbook = new StreamingWorkbookReader(this);
workbook.init(is);
return new StreamingWorkbook(workbook);
StreamingWorkbookReader workbookReader = new StreamingWorkbookReader(this);
StreamingWorkbook streamingWorkbook = new StreamingWorkbook(workbookReader);
workbookReader.setStreamingWorkbook(streamingWorkbook);
workbookReader.init(is);
return streamingWorkbook;
}

/**
Expand All @@ -262,9 +264,11 @@ public Workbook open(InputStream is) {
* @throws com.monitorjbl.xlsx.exceptions.ReadException if there is an issue reading the file
*/
public Workbook open(File file) {
StreamingWorkbookReader workbook = new StreamingWorkbookReader(this);
workbook.init(file);
return new StreamingWorkbook(workbook);
StreamingWorkbookReader workbookReader = new StreamingWorkbookReader(this);
StreamingWorkbook streamingWorkbook = new StreamingWorkbook(workbookReader);
workbookReader.setStreamingWorkbook(streamingWorkbook);
workbookReader.init(file);
return streamingWorkbook;
}

/**
Expand Down Expand Up @@ -350,7 +354,7 @@ public StreamingReader read(File f) {

XMLEventReader parser = StaxHelper.newXMLInputFactory().createXMLEventReader(sheet);

return new StreamingReader(new StreamingWorkbookReader(sst, sstCache, pkg, new StreamingSheetReader(sst, styles, parser, use1904Dates, rowCacheSize),
return new StreamingReader(new StreamingWorkbookReader(sst, sstCache, pkg, new StreamingSheetReader(null, sst, styles, parser, use1904Dates, rowCacheSize),
this));
} catch(IOException e) {
throw new OpenException("Failed to open file", e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/monitorjbl/xlsx/impl/StreamingSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ public Drawing createDrawingPatriarch() {
*/
@Override
public Workbook getWorkbook() {
throw new UnsupportedOperationException();
return reader.getStreamingWorkbook();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
public class StreamingSheetReader implements Iterable<Row> {
private static final Logger log = LoggerFactory.getLogger(StreamingSheetReader.class);


private final StreamingWorkbook streamingWorkbook;
private final SharedStringsTable sst;
private final StylesTable stylesTable;
private final XMLEventReader parser;
Expand All @@ -52,8 +54,9 @@ public class StreamingSheetReader implements Iterable<Row> {
private StreamingCell currentCell;
private boolean use1904Dates;

public StreamingSheetReader(SharedStringsTable sst, StylesTable stylesTable, XMLEventReader parser,
public StreamingSheetReader(StreamingWorkbook streamingWorkbook, SharedStringsTable sst, StylesTable stylesTable, XMLEventReader parser,
final boolean use1904Dates, int rowCacheSize) {
this.streamingWorkbook = streamingWorkbook;
this.sst = sst;
this.stylesTable = stylesTable;
this.parser = parser;
Expand All @@ -65,6 +68,10 @@ void setSheet(StreamingSheet sheet) {
this.sheet = sheet;
}

public StreamingWorkbook getStreamingWorkbook() {
return streamingWorkbook;
}

/**
* Read through a number of rows equal to the rowCacheSize field or until there is no more data to read
*
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.EvaluationWorkbook;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.SheetVisibility;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

public class StreamingWorkbook implements Workbook, AutoCloseable {
public class StreamingWorkbook implements Workbook, Date1904Support, AutoCloseable {
private final StreamingWorkbookReader reader;

public StreamingWorkbook(StreamingWorkbookReader reader) {
Expand Down Expand Up @@ -505,6 +497,11 @@ public int addOlePackage(byte[] bytes, String s, String s1, String s2) throws IO
throw new UnsupportedOperationException();
}

@Override
public boolean isDate1904() {
return reader.isUse1904Dates();
}

@Override
public EvaluationWorkbook createEvaluationWorkbook() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class StreamingWorkbookReader implements Iterable<Sheet>, AutoCloseable {
private OPCPackage pkg;
private SharedStringsTable sst;
private boolean use1904Dates = false;
private StreamingWorkbook streamingWorkbook;

/**
* This constructor exists only so the StreamingReader can instantiate
Expand Down Expand Up @@ -82,6 +83,10 @@ public StreamingSheetReader first() {
return sheets.get(0).getReader();
}

public void setStreamingWorkbook(StreamingWorkbook streamingWorkbook) {
this.streamingWorkbook = streamingWorkbook;
}

public void init(InputStream is) {
File f = null;
try {
Expand Down Expand Up @@ -159,7 +164,8 @@ void loadSheets(XSSFReader reader, SharedStringsTable sst, StylesTable stylesTab
int i = 0;
for(URI uri : sheetStreams.keySet()) {
XMLEventReader parser = StaxHelper.newXMLInputFactory().createXMLEventReader(sheetStreams.get(uri));
sheets.add(new StreamingSheet(sheetProperties.get(i++).get("name"), new StreamingSheetReader(sst, stylesTable, parser, use1904Dates, rowCacheSize)));
sheets.add(new StreamingSheet(sheetProperties.get(i++).get("name"),
new StreamingSheetReader(streamingWorkbook, sst, stylesTable, parser, use1904Dates, rowCacheSize)));
}
}

Expand Down Expand Up @@ -213,6 +219,10 @@ public void close() throws IOException {
}
}

public boolean isUse1904Dates() {
return use1904Dates;
}

static class StreamingSheetIterator implements Iterator<Sheet> {
private final Iterator<StreamingSheet> iterator;

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/monitorjbl/xlsx/DataFormatterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.monitorjbl.xlsx;

import org.apache.poi.ss.usermodel.*;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Locale;


public class DataFormatterTest {

}
51 changes: 45 additions & 6 deletions src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import com.monitorjbl.xlsx.exceptions.MissingSheetException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -218,6 +213,50 @@ public void testGetDateCellValue1904() throws Exception {
}
}

@Test
public void testDataFormatter1900() throws Exception {
try(
InputStream is = new FileInputStream(new File("src/test/resources/data_types.xlsx"));
Workbook wb = StreamingReader.builder().open(is);
) {

List<List<Cell>> obj = new ArrayList<>();

for(Row r : wb.getSheetAt(0)) {
List<Cell> o = new ArrayList<>();
for(Cell c : r) {
o.add(c);
}
obj.add(o);
}

DataFormatter dataFormatter = new DataFormatter(Locale.ENGLISH);
assertEquals("1/1/2014", dataFormatter.formatCellValue(obj.get(4).get(1)));
}
}

@Test
public void testDataFormatter1904() throws Exception {
try(
InputStream is = new FileInputStream(new File("src/test/resources/1904Dates.xlsx"));
Workbook wb = StreamingReader.builder().open(is);
) {

List<List<Cell>> obj = new ArrayList<>();

for(Row r : wb.getSheetAt(0)) {
List<Cell> o = new ArrayList<>();
for(Cell c : r) {
o.add(c);
}
obj.add(o);
}

DataFormatter dataFormatter = new DataFormatter(Locale.ENGLISH);
assertEquals("10/14/1991", dataFormatter.formatCellValue(obj.get(1).get(5)));
}
}

@Test
public void testGetFirstCellNum() throws Exception {
try(
Expand Down