18 package org.turro.jpa.export;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import jxl.write.DateTime;
30 import jxl.write.Label;
31 import jxl.write.WritableSheet;
32 import jxl.write.WritableWorkbook;
33 import jxl.write.WriteException;
34 import org.turro.elephant.context.ElephantContext;
42 private final Collection<String> columns;
43 private final Collection<Object[]> results;
45 public ExportQuery(Collection<String> columns, Collection<Object[]> results) {
46 this.columns = columns;
47 this.results = results;
50 public InputStream
getExcel(String sheetName) {
52 ByteArrayOutputStream baos =
new ByteArrayOutputStream();
53 WritableWorkbook ww = Workbook.createWorkbook(baos);
54 WritableSheet ws = ww.createSheet(sheetName, 0);
59 return new ByteArrayInputStream(baos.toByteArray());
60 }
catch (WriteException | IOException ex) {
66 private void writeSheet(WritableSheet ws)
throws WriteException {
67 int countCols = 0, countRows = 0;
68 for(String s : columns) {
69 ws.addCell(
new Label(countCols++, countRows, s));
71 for(Object[] objs : results) {
74 for(Object obj : objs) {
75 if(obj instanceof Number) {
76 ws.addCell(
new jxl.write.Number(countCols++, countRows,
77 ((Number)obj).doubleValue()));
78 }
else if(obj instanceof Date) {
79 ws.addCell(
new DateTime(countCols++, countRows, (Date) obj));
81 ws.addCell(
new Label(countCols++, countRows, (String) obj));
static String logMsg(String msg)
InputStream getExcel(String sheetName)
ExportQuery(Collection< String > columns, Collection< Object[]> results)