BrightSide Workbench Full Report + Source Code
ExportQuery.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.jpa.export;
19 
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;
28 import jxl.Workbook;
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;
35 
40 public class ExportQuery {
41 
42  private final Collection<String> columns;
43  private final Collection<Object[]> results;
44 
45  public ExportQuery(Collection<String> columns, Collection<Object[]> results) {
46  this.columns = columns;
47  this.results = results;
48  }
49 
50  public InputStream getExcel(String sheetName) {
51  try {
52  ByteArrayOutputStream baos = new ByteArrayOutputStream();
53  WritableWorkbook ww = Workbook.createWorkbook(baos);
54  WritableSheet ws = ww.createSheet(sheetName, 0);
55  writeSheet(ws);
56  ww.write();
57  ww.close();
58  baos.close();
59  return new ByteArrayInputStream(baos.toByteArray());
60  } catch (WriteException | IOException ex) {
61  Logger.getLogger(ExportQuery.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
62  }
63  return null;
64  }
65 
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));
70  }
71  for(Object[] objs : results) {
72  countCols = 0;
73  countRows++;
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));
80  } else {
81  ws.addCell(new Label(countCols++, countRows, (String) obj));
82  }
83  }
84  }
85  }
86 
87 }
InputStream getExcel(String sheetName)
ExportQuery(Collection< String > columns, Collection< Object[]> results)