KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > demo > Features


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.demo;
21
22 import java.io.File JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.BufferedWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.UnsupportedEncodingException JavaDoc;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import jxl.Workbook;
33 import jxl.Sheet;
34 import jxl.Cell;
35 import jxl.CellFeatures;
36 import jxl.CellReferenceHelper;
37
38 /**
39  * Goes through each cell in the workbook, and if the cell has any features
40  * associated with, it prints out the cell contents and the features
41  */

42 public class Features
43 {
44   /**
45    * Constructor
46    *
47    * @param w The workbook to interrogate
48    * @param out The output stream to which the CSV values are written
49    * @param encoding The encoding used by the output stream. Null or
50    * unrecognized values cause the encoding to default to UTF8
51    * @exception java.io.IOException
52    */

53   public Features(Workbook w, OutputStream JavaDoc out, String JavaDoc encoding)
54     throws IOException JavaDoc
55   {
56     if (encoding == null || !encoding.equals("UnicodeBig"))
57     {
58       encoding = "UTF8";
59     }
60
61     try
62     {
63       OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(out, encoding);
64       BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(osw);
65
66       ArrayList JavaDoc parseErrors = new ArrayList JavaDoc();
67       
68       for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++)
69       {
70         Sheet s = w.getSheet(sheet);
71
72         bw.write(s.getName());
73         bw.newLine();
74       
75         Cell[] row = null;
76         Cell c = null;
77       
78         for (int i = 0 ; i < s.getRows() ; i++)
79         {
80           row = s.getRow(i);
81
82           for (int j = 0; j < row.length; j++)
83           {
84             c = row[j];
85             if (c.getCellFeatures() != null)
86             {
87               CellFeatures features = c.getCellFeatures();
88               StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
89               CellReferenceHelper.getCellReference
90                  (c.getColumn(), c.getRow(), sb);
91
92               bw.write("Cell " + sb.toString() +
93                        " contents: " + c.getContents());
94               bw.flush();
95               bw.write(" comment: " + features.getComment());
96               bw.flush();
97               bw.newLine();
98             }
99           }
100         }
101       }
102       bw.flush();
103       bw.close();
104     }
105     catch (UnsupportedEncodingException JavaDoc e)
106     {
107       System.err.println(e.toString());
108     }
109   }
110
111 }
112
113
Popular Tags