Skip to content

Commit 49000d4

Browse files
committed
Swtich to dhatim.fastexcel, as we don't need the extra features in Apache POI.
This also saves a lot of space in the output JAR.
1 parent 6c9e96d commit 49000d4

7 files changed

Lines changed: 82 additions & 168 deletions

File tree

pom.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,10 @@
6464
<scope>test</scope>
6565
</dependency>
6666

67-
<dependency>
68-
<groupId>org.apache.poi</groupId>
69-
<artifactId>poi</artifactId>
70-
<version>${poi.version}</version>
71-
</dependency>
7267
<dependency>
73-
<groupId>org.apache.poi</groupId>
74-
<artifactId>poi-ooxml</artifactId>
75-
<version>${poi.version}</version>
68+
<groupId>org.dhatim</groupId>
69+
<artifactId>fastexcel</artifactId>
70+
<version>0.9.3</version>
7671
</dependency>
7772

7873
<dependency>

src/main/java/com/comphenix/rema1000/Application.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ private static OutputStream getOutput(ArgumentParser parser) throws IOException
142142
private static void writeOutput(DestinationFormat format, OutputStream output, DataRoot dataRoot) throws IOException {
143143
switch (format) {
144144
case XLSX:
145-
new ExcelWriter(ExcelWriter.Format.XLSX).write(output, dataRoot);
146-
break;
147-
case XLS:
148-
new ExcelWriter(ExcelWriter.Format.XLS).write(output, dataRoot);
145+
new ExcelWriter().write(output, dataRoot);
149146
break;
150147
case SQL:
151148
new SqlWriter().write(output, dataRoot);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.comphenix.rema1000.io.excel;
2+
3+
import org.dhatim.fastexcel.Worksheet;
4+
5+
import java.sql.Date;
6+
import java.time.Instant;
7+
import java.time.ZonedDateTime;
8+
9+
@FunctionalInterface
10+
public interface CellStyle {
11+
/**
12+
* Apply the current excel style to the given work sheet.
13+
* @param worksheet the work sheet.
14+
* @param row the row.
15+
* @param column the column.
16+
*/
17+
public void apply(Worksheet worksheet, int row, int column);
18+
19+
/**
20+
* Write the given (optionally) styled value to the cell.
21+
* @param worksheet the worksheet.
22+
* @param row the row.
23+
* @param column the column.
24+
* @param value the value to write.
25+
* @param style the style.
26+
*/
27+
public static void writeStyled(Worksheet worksheet, int row, int column, Object value, CellStyle style) {
28+
if (value instanceof Instant) {
29+
worksheet.value(row, column, Date.from((Instant) value));
30+
} else if (value instanceof Boolean) {
31+
worksheet.value(row, column, (Boolean)value ? "True" : "False");
32+
} else {
33+
worksheet.value(row, column, value);
34+
}
35+
36+
if (style != null) {
37+
style.apply(worksheet, row, column);
38+
}
39+
}
40+
}

src/main/java/com/comphenix/rema1000/io/excel/ExcelCells.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/main/java/com/comphenix/rema1000/io/excel/ExcelTableWriter.java

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
package com.comphenix.rema1000.io.excel;
22

33
import com.comphenix.rema1000.io.AbstractTableWriter;
4-
import org.apache.poi.ss.usermodel.Cell;
5-
import org.apache.poi.ss.usermodel.CellType;
6-
import org.apache.poi.ss.usermodel.Row;
7-
import org.apache.poi.ss.usermodel.Sheet;
4+
import org.dhatim.fastexcel.Worksheet;
85

6+
import java.time.Instant;
7+
import java.util.Date;
98
import java.util.Objects;
109

1110
public class ExcelTableWriter extends AbstractTableWriter {
1211
private final WorkbookStyle workbookStyle;
13-
private final Sheet sheet;
12+
private final Worksheet sheet;
1413

1514
// Position of the header row
1615
private int headerOffset;
1716
// Position of the first data row
1817
private int dataOffset;
1918

20-
private Row headerRow;
21-
private Row dataRow;
22-
2319
// Index of the current row (initially -1)
2420
private int dataIndex = -1;
2521

26-
public ExcelTableWriter(WorkbookStyle workbookStyle, Sheet sheet) {
22+
public ExcelTableWriter(WorkbookStyle workbookStyle, Worksheet sheet) {
2723
this(workbookStyle, sheet, 0, 1);
2824
}
2925

30-
public ExcelTableWriter(WorkbookStyle workbookStyle, Sheet sheet, int headerOffset, int dataOffset) {
26+
public ExcelTableWriter(WorkbookStyle workbookStyle, Worksheet sheet, int headerOffset, int dataOffset) {
3127
if (headerOffset < 0) {
3228
throw new IllegalArgumentException("headerOffset cannot be negative");
3329
}
@@ -42,7 +38,6 @@ public ExcelTableWriter(WorkbookStyle workbookStyle, Sheet sheet, int headerOffs
4238

4339
@Override
4440
public void incrementRow() {
45-
this.dataRow = null;
4641
this.dataIndex++;
4742
}
4843

@@ -53,35 +48,16 @@ public int getDataRowCount() {
5348

5449
@Override
5550
protected void onHeaderCreated(String headerName, int headerIndex) {
56-
Cell headerCell = getHeaderRow().createCell(headerIndex, CellType.STRING);
57-
headerCell.setCellStyle(workbookStyle.getHeaderStyle());
58-
headerCell.setCellValue(headerName);
51+
CellStyle.writeStyled(sheet, headerOffset, headerIndex, headerName, workbookStyle.getHeaderStyle());
5952
}
6053

6154
@Override
6255
protected void onWriteValue(int headerIndex, Object value, Class<?> type) {
63-
ExcelCells.writeCell(workbookStyle, getDataRow().createCell(headerIndex), value, type);
64-
}
65-
66-
private Row getHeaderRow() {
67-
Row result = headerRow;
68-
69-
if (result == null) {
70-
result = headerRow = sheet.createRow(headerOffset);
71-
}
72-
return result;
73-
}
74-
75-
private Row getDataRow() {
76-
Row result = dataRow;
77-
78-
if (result == null) {
79-
if (dataIndex < 0) {
80-
throw new IllegalStateException("Must call incrementRow() first");
81-
}
82-
result = dataRow = sheet.createRow(dataOffset + dataIndex);
56+
if (dataIndex < 0) {
57+
throw new IllegalStateException("Must call incrementRow() first");
8358
}
84-
return result;
59+
CellStyle style = Instant.class.equals(type) || Date.class.equals(type) ? workbookStyle.getDateStyle() : null;
60+
CellStyle.writeStyled(sheet, dataOffset + dataIndex, headerIndex, value, style);
8561
}
8662

8763
@Override

src/main/java/com/comphenix/rema1000/io/excel/ExcelWriter.java

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,48 @@
77
import com.comphenix.rema1000.model.TopListMetadata;
88
import com.comphenix.rema1000.model.Transaction;
99
import com.comphenix.rema1000.model.TransactionsInfo;
10-
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
11-
import org.apache.poi.ss.usermodel.*;
12-
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
10+
import org.dhatim.fastexcel.Workbook;
11+
import org.dhatim.fastexcel.Worksheet;
1312

1413
import java.io.IOException;
1514
import java.io.OutputStream;
1615
import java.util.List;
1716
import java.util.Objects;
1817

1918
public class ExcelWriter extends DataWriter<DataRoot> {
20-
public enum Format {
21-
XLSX,
22-
XLS
23-
}
24-
25-
private Format format;
2619
private DataTableConverter tableConverter = new DataTableConverter();
2720

28-
public ExcelWriter(Format format) {
29-
this.format = Objects.requireNonNull(format, "format cannot be NULL");
30-
}
31-
3221
@Override
3322
public void write(OutputStream output, DataRoot data) throws IOException {
34-
try (Workbook workbook = createWorkbook()) {
35-
WorkbookStyle workbookStyle = createWorkbookStyle(workbook);
36-
37-
writeTransactionsInfo(workbookStyle, workbook.createSheet("Info"), data.getTransactionsInfo());
23+
Workbook workbook = new Workbook(output, "RemaTransactionParser", "1.0");
24+
WorkbookStyle workbookStyle = createWorkbookStyle();
3825

39-
writeTopList(workbookStyle, workbook.createSheet("TopList"),
40-
data.getTopList());
41-
writeTransactions(workbookStyle, workbook.createSheet("Transactions"),
42-
data.getTransactionsInfo().getTransactionList());
43-
writeTransactionsPayments(workbookStyle, workbook.createSheet("Transactions Payments"),
44-
data.getTransactionsInfo().getTransactionList());
45-
writeTransactionUsedOffers(workbookStyle, workbook.createSheet("Used Offers"),
46-
data.getTransactionsInfo().getTransactionList());
26+
writeTransactionsInfo(workbookStyle, workbook.newWorksheet("Info"), data.getTransactionsInfo());
4727

48-
workbook.write(output);
49-
}
50-
}
28+
writeTopList(workbookStyle, workbook.newWorksheet("TopList"),
29+
data.getTopList());
30+
writeTransactions(workbookStyle, workbook.newWorksheet("Transactions"),
31+
data.getTransactionsInfo().getTransactionList());
32+
writeTransactionsPayments(workbookStyle, workbook.newWorksheet("Transactions Payments"),
33+
data.getTransactionsInfo().getTransactionList());
34+
writeTransactionUsedOffers(workbookStyle, workbook.newWorksheet("Used Offers"),
35+
data.getTransactionsInfo().getTransactionList());
5136

52-
private Workbook createWorkbook() {
53-
return format == Format.XLSX ? new XSSFWorkbook() : new HSSFWorkbook();
37+
workbook.finish();
5438
}
5539

56-
private void writeTransactionsInfo(WorkbookStyle workbookStyle, Sheet info, TransactionsInfo transactionsInfo) {
40+
private void writeTransactionsInfo(WorkbookStyle workbookStyle, Worksheet info, TransactionsInfo transactionsInfo) {
5741
writeInfoLine(workbookStyle, info, 0, "Bonus Total", transactionsInfo.getBonusTotal());
5842
writeInfoLine(workbookStyle, info, 1, "Discount Total", transactionsInfo.getDiscountTotal());
5943
writeInfoLine(workbookStyle, info, 2, "Purchase Total", transactionsInfo.getPurchaseTotal());
6044
}
6145

62-
private void writeInfoLine(WorkbookStyle style, Sheet sheet, int rowIndex, String infoName, Object infoValue) {
63-
Row row = sheet.createRow(rowIndex);
64-
Cell headerCell = row.createCell(0);
65-
headerCell.setCellStyle(style.getHeaderStyle());
66-
headerCell.setCellValue(infoName);
67-
68-
ExcelCells.writeCell(style, row.createCell(1), infoValue, infoValue != null ? infoValue.getClass() : Object.class);
46+
private void writeInfoLine(WorkbookStyle style, Worksheet sheet, int rowIndex, String infoName, Object infoValue) {
47+
CellStyle.writeStyled(sheet, rowIndex, 0, infoName, style.getHeaderStyle());
48+
CellStyle.writeStyled(sheet, rowIndex, 1, infoValue, null);
6949
}
7050

71-
private void writeTransactionsPayments(WorkbookStyle workbookStyle, Sheet sheet, List<Transaction> transactionList) throws IOException {
51+
private void writeTransactionsPayments(WorkbookStyle workbookStyle, Worksheet sheet, List<Transaction> transactionList) throws IOException {
7252
if (transactionList == null || transactionList.isEmpty()) {
7353
return;
7454
}
@@ -77,7 +57,7 @@ private void writeTransactionsPayments(WorkbookStyle workbookStyle, Sheet sheet,
7757
}
7858
}
7959

80-
private void writeTransactionUsedOffers(WorkbookStyle workbookStyle, Sheet sheet, List<Transaction> transactionList) throws IOException {
60+
private void writeTransactionUsedOffers(WorkbookStyle workbookStyle, Worksheet sheet, List<Transaction> transactionList) throws IOException {
8161
if (transactionList == null || transactionList.isEmpty()) {
8262
return;
8363
}
@@ -86,12 +66,12 @@ private void writeTransactionUsedOffers(WorkbookStyle workbookStyle, Sheet sheet
8666
}
8767
}
8868

89-
private void writeTopList(WorkbookStyle workbookStyle, Sheet sheet, TopListMetadata metadata) throws IOException {
69+
private void writeTopList(WorkbookStyle workbookStyle, Worksheet sheet, TopListMetadata metadata) throws IOException {
9070
TableWriter writer = new ExcelTableWriter(workbookStyle, sheet);
9171
tableConverter.writeTableTopList(writer, metadata);
9272
}
9373

94-
private void writeTransactions(WorkbookStyle workbookStyle, Sheet sheet, List<Transaction> transactionList) throws IOException {
74+
private void writeTransactions(WorkbookStyle workbookStyle, Worksheet sheet, List<Transaction> transactionList) throws IOException {
9575
if (transactionList == null || transactionList.isEmpty()) {
9676
return;
9777
}
@@ -100,20 +80,10 @@ private void writeTransactions(WorkbookStyle workbookStyle, Sheet sheet, List<Tr
10080
}
10181
}
10282

103-
private WorkbookStyle createWorkbookStyle(Workbook workbook) {
104-
CellStyle headerStyle = createHeaderStyle(workbook);
105-
CellStyle dateStyle = workbook.createCellStyle();
106-
dateStyle.setDataFormat((short)0x16);
107-
108-
return new WorkbookStyle(headerStyle, dateStyle);
109-
}
110-
111-
private CellStyle createHeaderStyle(Workbook workbook) {
112-
CellStyle headerStyle = workbook.createCellStyle();
113-
114-
Font headerFont = workbook.createFont();
115-
headerFont.setBold(true);
116-
headerStyle.setFont(headerFont);
117-
return headerStyle;
83+
private WorkbookStyle createWorkbookStyle() {
84+
return new WorkbookStyle(
85+
(sheet, r, c) -> sheet.style(r, c).bold().set(),
86+
(sheet, r, c) -> sheet.style(r, c).format(Integer.toString(0x16)).set()
87+
);
11888
}
11989
}

src/main/java/com/comphenix/rema1000/io/excel/WorkbookStyle.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.comphenix.rema1000.io.excel;
22

3-
import org.apache.poi.ss.usermodel.CellStyle;
4-
53
public class WorkbookStyle {
64
private CellStyle headerStyle;
75
private CellStyle dateStyle;

0 commit comments

Comments
 (0)