19 package org.turro.jpa.export;
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.function.BiFunction;
26 import java.util.stream.Stream;
28 import jxl.write.DateTime;
29 import jxl.write.Label;
30 import jxl.write.WritableSheet;
31 import jxl.write.WritableWorkbook;
32 import jxl.write.WriteException;
33 import org.turro.elephant.db.WhereClause;
34 import org.turro.jpa.Dao;
35 import org.turro.log.WebLoggers;
36 import org.turro.sql.SqlClause;
37 import org.turro.time.Dates;
45 private static final int MAX_ROWS = 60000;
47 private final Dao dao;
48 private final SqlClause sc;
50 private final Collection<String> columns;
52 private int countCols = 0, countRows = 0;
53 private BiFunction<Integer, Object, Object> replace =
null;
54 private WritableWorkbook ww;
55 private WritableSheet ws;
59 this.columns = columns;
66 this.columns = columns;
72 this.replace = replace;
77 ww = Workbook.createWorkbook(file);
81 }
catch (WriteException | IOException ex) {
86 private void writeSheet() throws WriteException {
88 for(String s : columns) {
89 ws.addCell(
new Label(countCols++, countRows, s));
92 try(Stream stream = dao.stream(Object[].class, sc)) {
95 }
else if(wc !=
null) {
96 try(Stream stream = dao.stream(Object[].class, wc)) {
102 private void writeRows(
final Stream stream) {
103 stream.forEach(objs -> {
107 for(Object obj : (Object[]) objs) {
109 if(replace !=
null) {
110 obj = replace.apply(countCols, obj);
112 if(obj instanceof Number) {
113 ws.addCell(
new jxl.write.Number(countCols++, actualRow(),
114 ((Number)obj).doubleValue()));
115 }
else if(obj instanceof Date) {
116 ws.addCell(
new DateTime(countCols++, actualRow(), Dates.fromGTM((Date) obj).get()));
117 }
else if(obj instanceof Boolean) {
118 ws.addCell(
new Label(countCols++, actualRow(), ((Boolean) obj).toString()));
120 ws.addCell(
new Label(countCols++, actualRow(), (String) obj));
122 }
catch (WriteException ex) {
123 WebLoggers.severe(
this).exception(ex).log();
129 private void checkSheet() {
130 if(actualRow() == 0) {
131 int sheetIndex = Math.abs(countRows / MAX_ROWS);
132 ws = ww.createSheet(
"Sheet %d".formatted(sheetIndex), sheetIndex);
136 private int actualRow() {
137 return countRows % MAX_ROWS;
ExportAsStream(Dao dao, Collection< String > columns, WhereClause wc)
void fieldReplacement(BiFunction< Integer, Object, Object > replace)
void generateExcel(File file)
ExportAsStream(Dao dao, Collection< String > columns, SqlClause sc)
static WebLoggers severe(Object entity)
WebLoggers exception(Throwable throwable)