View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
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  package net.sf.exorcist.xls;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.Iterator;
21  
22  import org.apache.poi.hssf.usermodel.HSSFCell;
23  import org.apache.poi.hssf.usermodel.HSSFRow;
24  import org.apache.poi.hssf.usermodel.HSSFSheet;
25  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
26  import org.apache.xerces.util.XMLChar;
27  import org.xml.sax.ContentHandler;
28  import org.xml.sax.SAXException;
29  import org.xml.sax.helpers.AttributesImpl;
30  
31  /***
32   * 
33   */
34  public class ExcelToXmlTransformer {
35  
36      private static final String PREFIX = "xls";
37  
38      private static final String NSURI = "http://exorcist.sf.net/ns/2005/xls";
39  
40      private InputStream stream;
41  
42      private ContentHandler handler;
43  
44      public void setInputStream(InputStream stream) {
45          this.stream = stream;
46      }
47  
48      public void setContentHandler(ContentHandler handler) {
49          this.handler = handler;
50      }
51  
52      public void transform() throws IOException, SAXException {
53          HSSFWorkbook workbook = new HSSFWorkbook(stream);
54          
55          handler.startDocument();
56          handler.startPrefixMapping(null, NSURI);
57          transformWorkbook(workbook);
58          handler.endPrefixMapping(null);
59          handler.endDocument();
60      }
61  
62      private void transformWorkbook(HSSFWorkbook workbook) throws SAXException {
63          handler.startElement(NSURI, "workbook", "workbook", null);
64          for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
65              HSSFSheet sheet = workbook.getSheetAt(i);
66              if (sheet.getPhysicalNumberOfRows() > 0) {
67                  transformSheet(workbook.getSheetName(i), sheet);
68              }
69          }
70          handler.endElement(NSURI, "workbook", "workbook");
71      }
72  
73      private void transformSheet(String name, HSSFSheet sheet) throws SAXException {
74          Iterator rows = sheet.rowIterator();
75          if (rows.hasNext()) {
76              HSSFRow first = (HSSFRow) rows.next();
77              while (rows.hasNext()) {
78                  transformRow(name, first, (HSSFRow) rows.next());
79              }
80          }
81      }
82  
83      private void transformRow(String name, HSSFRow first, HSSFRow row) throws SAXException {
84          handler.startElement(null, name, name, null);
85          for (short i = first.getFirstCellNum(); i < first.getLastCellNum(); i++) {
86              transformCell(first.getCell(i).getStringCellValue(), row.getCell(i));
87          }
88          handler.endElement(null, name, name);
89      }
90  
91      private void transformCell(String name, HSSFCell cell) throws SAXException {
92          handler.startElement(null, name, name, null);
93  
94          if (cell != null) {
95              char[] chars = null;
96              switch (cell.getCellType()) {
97              case HSSFCell.CELL_TYPE_STRING:
98                  chars = cell.getStringCellValue().toCharArray();
99                  for (int i = 0; i < chars.length; i++) {
100                     if (XMLChar.isInvalid(chars[i])) {
101                         chars[i] = ' ';
102                     }
103                 }
104                 break;
105             case HSSFCell.CELL_TYPE_NUMERIC:
106                 String value = String.valueOf(cell.getNumericCellValue());
107                 if (value.endsWith(".0")) {
108                     value = value.substring(0, value.length() - 2);
109                 }
110                 chars = value.toCharArray();
111                 break;
112             case HSSFCell.CELL_TYPE_BOOLEAN:
113                 chars = String.valueOf(cell.getBooleanCellValue()).toCharArray();
114                 break;
115             default:
116                 chars = new char[] {};
117             break;
118             }
119             handler.characters(chars, 0, chars.length);
120         }
121 
122         handler.endElement(null, name, name);
123     }
124 
125 }