KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > dev > EFHSSF


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
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
18 package org.apache.poi.hssf.dev;
19
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
26 import org.apache.poi.hssf.record.*;
27 import org.apache.poi.hssf.eventmodel.*;
28 import org.apache.poi.hssf.eventusermodel.*;
29 import org.apache.poi.hssf.usermodel.*;
30
31 import org.apache.poi.hssf.eventusermodel.HSSFRequest;
32 import org.apache.poi.hssf.eventusermodel.HSSFListener;
33 import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
34
35 /**
36  * Event Factory version of HSSF test class.
37  * @author andy
38  */

39
40 public class EFHSSF
41 {
42     String JavaDoc infile;
43     String JavaDoc outfile;
44     HSSFWorkbook workbook = null;
45     HSSFSheet cursheet = null;
46
47     /** Creates a new instance of EFHSSF */
48
49     public EFHSSF()
50     {
51     }
52
53     public void setInputFile(String JavaDoc infile)
54     {
55         this.infile = infile;
56     }
57
58     public void setOutputFile(String JavaDoc outfile)
59     {
60         this.outfile = outfile;
61     }
62
63     public void run()
64         throws IOException JavaDoc
65     {
66         FileInputStream JavaDoc fin = new FileInputStream JavaDoc(infile);
67         POIFSFileSystem poifs = new POIFSFileSystem(fin);
68         InputStream JavaDoc din = poifs.createDocumentInputStream("Workbook");
69         HSSFRequest req = new HSSFRequest();
70
71         req.addListenerForAllRecords(new EFHSSFListener(this));
72         HSSFEventFactory factory = new HSSFEventFactory();
73
74         factory.processEvents(req, din);
75         fin.close();
76         din.close();
77         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(outfile);
78
79         workbook.write(fout);
80         fout.close();
81         System.out.println("done.");
82     }
83
84     public void recordHandler(Record record)
85     {
86         HSSFRow row = null;
87         HSSFCell cell = null;
88         int sheetnum = -1;
89
90         switch (record.getSid())
91         {
92
93             case BOFRecord.sid :
94                 BOFRecord bof = ( BOFRecord ) record;
95
96                 if (bof.getType() == bof.TYPE_WORKBOOK)
97                 {
98                     workbook = new HSSFWorkbook();
99                 }
100                 else if (bof.getType() == bof.TYPE_WORKSHEET)
101                 {
102                     sheetnum++;
103                     cursheet = workbook.getSheetAt(sheetnum);
104                 }
105                 break;
106
107             case BoundSheetRecord.sid :
108                 BoundSheetRecord bsr = ( BoundSheetRecord ) record;
109
110                 workbook.createSheet(bsr.getSheetname());
111                 break;
112
113             case RowRecord.sid :
114                 RowRecord rowrec = ( RowRecord ) record;
115
116                 cursheet.createRow(rowrec.getRowNumber());
117                 break;
118
119             case NumberRecord.sid :
120                 NumberRecord numrec = ( NumberRecord ) record;
121
122                 row = cursheet.getRow(numrec.getRow());
123                 cell = row.createCell(numrec.getColumn(),
124                                       HSSFCell.CELL_TYPE_NUMERIC);
125                 cell.setCellValue(numrec.getValue());
126                 break;
127
128             case SSTRecord.sid :
129                 SSTRecord sstrec = ( SSTRecord ) record;
130
131                 for (int k = 0; k < sstrec.getNumUniqueStrings(); k++)
132                 {
133                     workbook.addSSTString(sstrec.getString(k));
134                 }
135                 break;
136
137             case LabelSSTRecord.sid :
138                 LabelSSTRecord lrec = ( LabelSSTRecord ) record;
139
140                 row = cursheet.getRow(lrec.getRow());
141                 cell = row.createCell(lrec.getColumn(),
142                                       HSSFCell.CELL_TYPE_STRING);
143                 cell.setCellValue(workbook.getSSTString(lrec.getSSTIndex()));
144                 break;
145         }
146     }
147
148     public static void main(String JavaDoc [] args)
149     {
150         if ((args.length < 2) || !args[ 0 ].equals("--help"))
151         {
152             try
153             {
154                 EFHSSF viewer = new EFHSSF();
155
156                 viewer.setInputFile(args[ 0 ]);
157                 viewer.setOutputFile(args[ 1 ]);
158                 viewer.run();
159             }
160             catch (IOException JavaDoc e)
161             {
162                 e.printStackTrace();
163             }
164         }
165         else
166         {
167             System.out.println("EFHSSF");
168             System.out.println(
169                 "General testbed for HSSFEventFactory based testing and "
170                 + "Code examples");
171             System.out.println("Usage: java org.apache.poi.hssf.dev.EFHSSF "
172                                + "file1 file2");
173             System.out.println(
174                 " --will rewrite the file reading with the event api");
175             System.out.println("and writing with the standard API");
176         }
177     }
178 }
179
180 class EFHSSFListener
181     implements HSSFListener
182 {
183     EFHSSF efhssf;
184
185     public EFHSSFListener(EFHSSF efhssf)
186     {
187         this.efhssf = efhssf;
188     }
189
190     public void processRecord(Record record)
191     {
192         efhssf.recordHandler(record);
193     }
194 }
195
Popular Tags