View Javadoc

1   /***
2    * 
3    */
4   package ar.com.epere4.java2excel.excelWritter;
5   
6   import java.io.BufferedOutputStream;
7   import java.io.File;
8   import java.io.FileOutputStream;
9   import java.io.IOException;
10  import java.io.OutputStream;
11  import java.util.Calendar;
12  import java.util.Collection;
13  import java.util.Date;
14  import java.util.Iterator;
15  
16  import ognl.OgnlException;
17  
18  import org.apache.poi.hssf.usermodel.HSSFCell;
19  import org.apache.poi.hssf.usermodel.HSSFRow;
20  import org.apache.poi.hssf.usermodel.HSSFSheet;
21  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
22  
23  import ar.com.epere4.java2excel.parser.Java2ExcelDtd;
24  import ar.com.epere4.java2excel.parser.PropertyDtd;
25  
26  /***
27   * This class allows you to print a some
28   * {@link java.util.Collection Collections} to an excel file using some
29   * configuration {@link ar.com.epere4.java2excel.parser.Java2ExcelDtd}.
30   * 
31   * @author epere4
32   * 
33   */
34  public class ExcelWritter {
35      /***
36       * This identifies the excel builtin format 0xf, "d-mmm-yy", as defined in
37       * {@link org.apache.poi.hssf.usermodel.HSSFDataFormat}.
38       * 
39       * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
40       */
41      private static final short DATE_FORMAT = 0xf;
42  
43      /***
44       * This represents the excel workbook.
45       */
46      private final HSSFWorkbook workbook = new HSSFWorkbook();
47  
48      /***
49       * Default constructor.
50       * 
51       * @since 18/02/2006
52       */
53      public ExcelWritter() {
54  
55      }
56  
57      /***
58       * Adds a collection to be printed in the excel workbook represented by this
59       * object. For each collection added, a new excel sheet is created.
60       * 
61       * @param data
62       *            the collection to be printed in the excel file.
63       * @param metaData
64       *            the configuration info.
65       * @since 18/02/2006
66       */
67      public void addReport(final Collection data, final Java2ExcelDtd metaData) {
68          addReport(data, metaData, null);
69      }
70      /***
71       * Adds a collection to be printed in the excel workbook represented by this
72       * object. For each collection added, a new excel sheet is created with the
73       * specified sheetName.
74       * 
75       * @param data
76       *            the collection to be printed in the excel file.
77       * @param metaData
78       *            the configuration info.
79       * @param sheetName
80       *            The name for the seet being added to the excel. If null, then
81       *            a default name will be used. Sheet name cannot be blank,
82       *            greater than 31 chars, or contain any of ///*?[].
83       * @since 05/03/2006
84       */
85      public void addReport(final Collection data, final Java2ExcelDtd metaData, 
86                  final String sheetName) {
87          if (data.size() > 0) {
88              metaData.getBody().completeProperties(
89                      data.iterator().next().getClass());
90          }
91          HSSFSheet sheet;
92          if (sheetName == null) {
93              sheet = workbook.createSheet();
94          } else {
95              sheet = workbook.createSheet(sheetName);
96          }
97  
98          HSSFRow titlesRow = sheet.createRow(0);
99          addMetaData(titlesRow, metaData);
100 
101         int rowNum = 1;
102         for (Iterator iter = data.iterator(); iter.hasNext(); rowNum++) {
103             Object element = iter.next();
104             addData(sheet.createRow(rowNum), metaData, element);
105         }
106     }
107 
108     /***
109      * This method adds the column titles to the row supplied.
110      * 
111      * @param row
112      *            the row where the titles will be added.
113      * @param metaData
114      *            the configuration info, that contains the column names.
115      * @since 18/02/2006
116      */
117     private void addMetaData(final HSSFRow row, final Java2ExcelDtd metaData) {
118         short colNum = 0;
119         for (Iterator iter = metaData.getBody().propertiesIterator(); iter
120                 .hasNext(); colNum++) {
121             PropertyDtd property = (PropertyDtd) iter.next();
122             HSSFCell cell = row.createCell(colNum);
123 
124             cell.setCellValue(property.getTitle());
125         }
126     }
127 
128     /***
129      * Adds the element to the row, using the metaData configuration.
130      * 
131      * @param row
132      *            the row where to add the element.
133      * @param metaData
134      *            the configuration info.
135      * @param element
136      *            the element to add to the row.
137      * @since 18/02/2006
138      */
139     private void addData(final HSSFRow row, final Java2ExcelDtd metaData,
140             final Object element) {
141         short colNum = 0;
142         for (Iterator iter = metaData.getBody().propertiesIterator(); iter
143                 .hasNext(); colNum++) {
144             PropertyDtd property = (PropertyDtd) iter.next();
145             HSSFCell cell = row.createCell(colNum);
146 
147             Object elementValue;
148             try {
149                 elementValue = property.evaluateExpression(element);
150             } catch (OgnlException e) {
151                 if (property.isProtectNullPointer()) {
152                     elementValue = null;
153                 } else {
154                     throw new RuntimeException(e);
155                 }
156             }
157             if (elementValue instanceof Number) {
158                 cell.setCellValue(((Number) elementValue).doubleValue());
159 
160             } else if (elementValue instanceof Date) {
161                 cell.setCellStyle(workbook.createCellStyle());
162                 cell.getCellStyle().setDataFormat(DATE_FORMAT);
163                 cell.setCellValue((Date) elementValue);
164 
165             } else if (elementValue instanceof Calendar) {
166                 cell.setCellStyle(workbook.createCellStyle());
167                 cell.getCellStyle().setDataFormat(DATE_FORMAT);
168                 cell.setCellValue((Calendar) elementValue);
169 
170             } else if (elementValue instanceof Boolean) {
171                 cell.setCellValue(((Boolean) elementValue).booleanValue());
172 
173             } else {
174                 cell.setCellValue(elementValue == null ? null : elementValue
175                         .toString());
176             }
177         }
178     }
179 
180     /***
181      * Saves the generated excel to the specified outputStream.
182      * 
183      * @param outputStream
184      *            the stream where you want to save the Excel.
185      * @throws IOException
186      *             if any Input Output problem shows up.
187      */
188     public void saveTo(final OutputStream outputStream) throws IOException {
189         workbook.write(outputStream);
190     }
191 
192     /***
193      * Saves the generated excel to the specified outputFile.
194      * 
195      * @param outputFile
196      *            the file where you want to save the Excel.
197      * @throws IOException
198      *             if any Input Output problem shows up.
199      */
200     public void saveTo(final File outputFile) throws IOException {
201         OutputStream outputStream = null;
202         try {
203             outputStream = new BufferedOutputStream(new FileOutputStream(
204                     outputFile));
205             saveTo(outputStream);
206         } finally {
207             if (outputStream != null) {
208                 outputStream.close();
209             }
210         }
211     }
212 
213 }