KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > extractor > MSExcelExtractor


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/extractor/MSExcelExtractor.java,v 1.1.2.1 2004/09/29 15:01:26 unico Exp $
3  * $Revision: 1.1.2.1 $
4  * $Date: 2004/09/29 15:01:26 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.extractor;
25
26 /**
27  * Author: Ryan Rhodes
28  * Date: Jun 26, 2004
29  * Time: 1:53:31 AM
30  */

31
32 import java.io.*;
33 import java.util.Iterator JavaDoc;
34
35 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
36 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
37 import org.apache.poi.hssf.usermodel.HSSFSheet;
38 import org.apache.poi.hssf.usermodel.HSSFRow;
39 import org.apache.poi.hssf.usermodel.HSSFCell;
40
41 public class MSExcelExtractor extends AbstractContentExtractor
42 {
43     public MSExcelExtractor(String JavaDoc uri, String JavaDoc contentType, String JavaDoc namespace) {
44       super(uri, contentType, namespace);
45     }
46
47     public Reader extract(InputStream content) throws ExtractorException
48     {
49         try
50         {
51             CharArrayWriter writer = new CharArrayWriter();
52
53             POIFSFileSystem fs = new POIFSFileSystem(content);
54             HSSFWorkbook workbook = new HSSFWorkbook(fs);
55
56             for (int i = 0; i < workbook.getNumberOfSheets(); i++ )
57             {
58                 HSSFSheet sheet = workbook.getSheetAt(i);
59
60              Iterator JavaDoc rows = sheet.rowIterator();
61                 while( rows.hasNext() ) {
62                     HSSFRow row = (HSSFRow) rows.next();
63
64                     Iterator JavaDoc cells = row.cellIterator();
65                     while( cells.hasNext() ) {
66                         HSSFCell cell = (HSSFCell) cells.next();
67                         switch ( cell.getCellType() ) {
68                             case HSSFCell.CELL_TYPE_NUMERIC:
69                                 String JavaDoc num = Double.toString(cell.getNumericCellValue()).trim();
70                                 if(num.length() > 0)
71                                     writer.write(num + " ");
72                                 break;
73                             case HSSFCell.CELL_TYPE_STRING:
74                                 String JavaDoc text = cell.getStringCellValue().trim();
75                                 if(text.length() > 0)
76                                     writer.write(text + " ");
77                                 break;
78                         }
79                     }
80                 }
81             }
82
83             return new CharArrayReader(writer.toCharArray());
84         }
85         catch(Exception JavaDoc e )
86         {
87             throw new ExtractorException(e.getMessage());
88         }
89     }
90
91     public static void main(String JavaDoc[] args) throws Exception JavaDoc
92     {
93         FileInputStream in = new FileInputStream(args[0]);
94
95         MSExcelExtractor ex = new MSExcelExtractor(null, null, null);
96
97         Reader reader = ex.extract(in);
98
99         int c = 0;
100         do
101         {
102             c = reader.read();
103             System.out.print((char)c);
104         }
105         while(c != -1);
106     }
107 }
Popular Tags