KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > domain > ExcelFile


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.domain;
18
19 import info.jtrac.util.ItemUtils;
20 import java.io.InputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.poi.hssf.usermodel.HSSFCell;
27 import org.apache.poi.hssf.usermodel.HSSFRow;
28 import org.apache.poi.hssf.usermodel.HSSFSheet;
29 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
30 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
31
32 /**
33  * Class that encapsulates an Excel Sheet / Workbook
34  * and is used to process, cleanse and import contents of an
35  * uploaded excel file into JTrac
36  */

37 public class ExcelFile implements Serializable JavaDoc {
38     
39     protected final Log logger = LogFactory.getLog(getClass());
40     
41     public class Column {
42         
43         private String JavaDoc label;
44         private Field field;
45         
46         public Column(String JavaDoc label) {
47             this.label = label;
48         }
49
50         public Field getField() {
51             return field;
52         }
53
54         public String JavaDoc getLabel() {
55             return label;
56         }
57         
58     }
59     
60     public class Cell {
61         
62         private Object JavaDoc value;
63
64         public Cell(Object JavaDoc value) {
65             this.value = value;
66         }
67         
68         @Override JavaDoc
69         public String JavaDoc toString() {
70             if (value == null) {
71                 return "";
72             }
73             if (value instanceof String JavaDoc) {
74                 return ItemUtils.fixWhiteSpace((String JavaDoc) value);
75             }
76             return value.toString();
77         }
78         
79     }
80     
81     private List JavaDoc<Column> columns;
82     private List JavaDoc<List JavaDoc<Cell>> rows;
83
84     public List JavaDoc<List JavaDoc<Cell>> getRows() {
85         return rows;
86     }
87
88     public List JavaDoc<Column> getColumns() {
89         return columns;
90     }
91     
92     //==========================================================================
93
// form binding stuff
94

95     private int[] selCols;
96     private int[] selRows;
97     private int action;
98
99     public int getAction() {
100         return action;
101     }
102     
103     public void setAction(int action) {
104         this.action = action;
105     }
106      
107     public int[] getSelCols() {
108         return selCols;
109     }
110
111     public void setSelCols(int[] selCols) {
112         this.selCols = selCols;
113     }
114
115     public int[] getSelRows() {
116         return selRows;
117     }
118
119     public void setSelRows(int[] selRows) {
120         this.selRows = selRows;
121     }
122     
123     //==========================================================================
124
// edits
125

126     /* note that selected rows and columns would be set by spring MVC */
127     public void deleteSelectedRowsAndColumns() {
128         int cursor = 0;
129         if (selRows != null) {
130             for(int i : selRows) {
131                 rows.remove(i - cursor);
132                 cursor++;
133             }
134         }
135         cursor = 0;
136         if (selCols != null) {
137             for(int i : selCols) {
138                 columns.remove(i - cursor);
139                 for(List JavaDoc<Cell> cells : rows) {
140                     cells.remove(i - cursor);
141                 }
142                 cursor++;
143             }
144         }
145     }
146     
147     public void convertSelectedColumnsToDate() {
148         if (selCols == null) {
149             return;
150         }
151         // could not find a better way to convert excel number to date
152
HSSFWorkbook wb = new HSSFWorkbook();
153         HSSFSheet sheet = wb.createSheet();
154         HSSFRow row = sheet.createRow(0);
155         HSSFCell cell = row.createCell((short) 0);
156         for(int i : selCols) {
157             for(List JavaDoc<Cell> cells : rows) {
158                 Cell c = cells.get(i);
159                 if (c != null && c.value instanceof Double JavaDoc) {
160                     cell.setCellValue((Double JavaDoc) c.value);
161                     c.value = cell.getDateCellValue();
162                 }
163             }
164         }
165     }
166     
167     public void concatenateSelectedColumns() {
168         if (selCols == null) {
169             return;
170         }
171         List JavaDoc<Cell> list = new ArrayList JavaDoc<Cell>(rows.size());
172         for(List JavaDoc<Cell> cells : rows) {
173             list.add(new Cell(null));
174         }
175         int first = selCols[0];
176         for(int i : selCols) {
177             int rowIndex = 0;
178             for(List JavaDoc<Cell> cells : rows) {
179                 Cell c = cells.get(i);
180                 if (c != null) {
181                     String JavaDoc s = (String JavaDoc) list.get(rowIndex).value;
182                     if (s == null) {
183                         s = (String JavaDoc) c.value;
184                     } else {
185                         s += "\n\n" + c.value;
186                     }
187                     list.set(rowIndex, new Cell(s));
188                 }
189                 rowIndex++;
190             }
191         }
192         // update the first column
193
int rowIndex = 0;
194         for(List JavaDoc<Cell> cells : rows) {
195             cells.set(first, list.get(rowIndex));
196             rowIndex++;
197         }
198     }
199     
200     public void extractSummaryFromSelectedColumn() {
201         if (selCols == null) {
202             return;
203         }
204         int first = selCols[0];
205         for(List JavaDoc<Cell> cells : rows) {
206             Cell c = cells.get(first);
207             if (c != null && c.value != null) {
208                 String JavaDoc s = c.value.toString();
209                 if (s.length() > 80) {
210                     s = s.substring(0, 80);
211                 }
212                 cells.add(0, new Cell(s));
213             } else {
214                 cells.add(0, null);
215             }
216         }
217         columns.add(0, new Column("Summary"));
218     }
219     
220     //==========================================================================
221

222     public ExcelFile() {
223         // zero arg constructor
224
}
225     
226     public ExcelFile(InputStream JavaDoc is) {
227         POIFSFileSystem fs = null;
228         HSSFWorkbook wb = null;
229         try {
230             fs = new POIFSFileSystem(is);
231             wb = new HSSFWorkbook(fs);
232         } catch (Exception JavaDoc e) {
233             throw new RuntimeException JavaDoc(e);
234         }
235         HSSFSheet sheet = wb.getSheetAt(0);
236         HSSFRow r = null;
237         HSSFCell c = null;
238         int row = 0;
239         int col = 0;
240         columns = new ArrayList JavaDoc<Column>();
241         //========================== HEADER ====================================
242
r = sheet.getRow(row);
243         while(true) {
244             c = r.getCell((short) col);
245             if (c == null) {
246                 break;
247             }
248             String JavaDoc value = c.getStringCellValue();
249             if (value == null || value.trim().length() == 0) {
250                 break;
251             }
252             Column column = new Column(value.trim());
253             columns.add(column);
254             col++;
255         }
256         //============================ DATA ====================================
257
rows = new ArrayList JavaDoc<List JavaDoc<Cell>>();
258         while(true) {
259             row++;
260             r = sheet.getRow(row);
261             if (r == null) {
262                 break;
263             }
264             List JavaDoc rowData = new ArrayList JavaDoc(columns.size());
265             boolean isEmptyRow = true;
266             for(col = 0; col < columns.size(); col++) {
267                 c = r.getCell((short) col);
268                 Object JavaDoc value = null;
269                 switch(c.getCellType()) {
270                     case(HSSFCell.CELL_TYPE_STRING) : value = c.getStringCellValue(); break;
271                     case(HSSFCell.CELL_TYPE_NUMERIC) :
272                         // value = c.getDateCellValue();
273
value = c.getNumericCellValue();
274                         break;
275                     case(HSSFCell.CELL_TYPE_BLANK) : break;
276                 }
277                 if (value != null && value.toString().length() > 0) {
278                     isEmptyRow = false;
279                     rowData.add(new Cell(value));
280                 } else {
281                     rowData.add(null);
282                 }
283             }
284             if(isEmptyRow) {
285                 break;
286             }
287             rows.add(rowData);
288         }
289     }
290     
291 }
292
Popular Tags