KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > index > filetypes > XincoIndexMicrosoftExcel


1 /**
2 *Copyright 2005 blueCubs.com
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 * This project supports the blueCubs vision of giving back
18 * to the community in exchange for free software!
19 * More information on: http://www.bluecubs.org
20 *************************************************************
21 *
22 * Name: XincoIndexMicrosoftExcel
23 *
24 * Description: indexing Microsoft Excel files
25 *
26 * Original Author: Alexander Manes
27 * Date: 2005/02/05
28 *
29 * Modifications:
30 *
31 * Who? When? What?
32 * - - -
33 *
34 *************************************************************
35 */

36
37 package com.bluecubs.xinco.index.filetypes;
38
39 import java.io.File JavaDoc;
40 import java.io.FileInputStream JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.io.Reader JavaDoc;
43 import org.apache.poi.hssf.usermodel.HSSFCell;
44 import org.apache.poi.hssf.usermodel.HSSFRow;
45 import org.apache.poi.hssf.usermodel.HSSFSheet;
46 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
47 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
48
49 public class XincoIndexMicrosoftExcel implements XincoIndexFileType {
50
51     public XincoIndexMicrosoftExcel() {
52         super();
53     }
54
55     public Reader JavaDoc getFileContentReader(File JavaDoc f) {
56         return null;
57     }
58
59     public String JavaDoc getFileContentString(File JavaDoc f) {
60         int i,j,j2,k;
61         short k2;
62         HSSFWorkbook wb = null;
63         HSSFSheet sheet = null;
64         HSSFRow row = null;
65         HSSFCell cell = null;
66         InputStream JavaDoc is = null;
67         String JavaDoc cell_string = "";
68         try {
69             is = new FileInputStream JavaDoc(f);
70             POIFSFileSystem fs = new POIFSFileSystem(is);
71             wb = new HSSFWorkbook(fs);
72             for (i=0;i<wb.getNumberOfSheets();i++) {
73                 sheet = wb.getSheetAt(i);
74                 j2 = 0;
75                 for (j=0;j<sheet.getPhysicalNumberOfRows();j++) {
76                     while ((row = sheet.getRow(j2)) == null) {
77                         j2++;
78                     }
79                     j2++;
80                     k2 = 0;
81                     for (k=0;k<row.getPhysicalNumberOfCells();k++) {
82                         while ((cell = row.getCell(k2)) == null) {
83                             k2++;
84                         }
85                         k2++;
86                         switch (cell.getCellType()) {
87                             case HSSFCell.CELL_TYPE_FORMULA :
88                                 break;
89                             case HSSFCell.CELL_TYPE_NUMERIC :
90                                 cell_string = cell_string + cell.getNumericCellValue() + "\t";
91                                 break;
92                             case HSSFCell.CELL_TYPE_STRING :
93                                 cell_string = cell_string + cell.getStringCellValue() + "\t";
94                                 break;
95                             default :
96                         }
97                         
98                     }
99                     cell_string = cell_string + "\n";
100                 }
101                 cell_string = cell_string + "\n\n\n";
102             }
103             is.close();
104         } catch (Exception JavaDoc fe) {
105             cell_string = null;
106             if (is != null) {
107                 try {
108                     is.close();
109                 } catch (Exception JavaDoc ise) {}
110             }
111         }
112         return cell_string;
113     }
114
115 }
116
Popular Tags