KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > demo > XML


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.UnsupportedEncodingException JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import jxl.Workbook;
30 import jxl.Sheet;
31 import jxl.Cell;
32 import jxl.CellType;
33 import jxl.format.CellFormat;
34 import jxl.format.Format;
35 import jxl.format.Border;
36 import jxl.format.BorderLineStyle;
37 import jxl.format.Colour;
38 import jxl.format.Pattern;
39 import jxl.format.Font;
40
41 /**
42  * Simple demo class which uses the api to present the contents
43  * of an excel 97 spreadsheet as an XML document, using a workbook
44  * and output stream of your choice
45  */

46 public class XML
47 {
48   /**
49    * The output stream to write to
50    */

51   private OutputStream JavaDoc out;
52
53   /**
54    * The encoding to write
55    */

56   private String JavaDoc encoding;
57
58   /**
59    * The workbook we are reading from
60    */

61   private Workbook workbook;
62   
63   /**
64    * Constructor
65    *
66    * @param w The workbook to interrogate
67    * @param out The output stream to which the XML values are written
68    * @param enc The encoding used by the output stream. Null or
69    * unrecognized values cause the encoding to default to UTF8
70    * @param f Indicates whether the generated XML document should contain
71    * the cell format information
72    * @exception java.io.IOException
73    */

74   public XML(Workbook w, OutputStream JavaDoc out, String JavaDoc enc, boolean f)
75     throws IOException JavaDoc
76   {
77     encoding = enc;
78     workbook = w;
79     this.out = out;
80
81     if (encoding == null || !encoding.equals("UnicodeBig"))
82     {
83       encoding = "UTF8";
84     }
85
86     if (f)
87     {
88       writeFormattedXML();
89     }
90     else
91     {
92       writeXML();
93     }
94
95   }
96
97   /**
98    * Writes out the workbook data as XML, without formatting information
99    */

100   private void writeXML() throws IOException JavaDoc
101   {
102     try
103     {
104       OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(out, encoding);
105       BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(osw);
106       
107       bw.write("<?xml version=\"1.0\" ?>");
108       bw.newLine();
109       bw.write("<!DOCTYPE workbook SYSTEM \"workbook.dtd\">");
110       bw.newLine();
111       bw.newLine();
112       bw.write("<workbook>");
113       bw.newLine();
114       for (int sheet = 0; sheet < workbook.getNumberOfSheets(); sheet++)
115       {
116         Sheet s = workbook.getSheet(sheet);
117
118         bw.write(" <sheet>");
119         bw.newLine();
120         bw.write(" <name><![CDATA["+s.getName()+"]]></name>");
121         bw.newLine();
122       
123         Cell[] row = null;
124       
125         for (int i = 0 ; i < s.getRows() ; i++)
126         {
127           bw.write(" <row number=\"" + i + "\">");
128           bw.newLine();
129           row = s.getRow(i);
130
131           for (int j = 0 ; j < row.length; j++)
132           {
133             if (row[j].getType() != CellType.EMPTY)
134             {
135               bw.write(" <col number=\"" + j + "\">");
136               bw.write("<![CDATA["+row[j].getContents()+"]]>");
137               bw.write("</col>");
138               bw.newLine();
139             }
140           }
141           bw.write(" </row>");
142           bw.newLine();
143         }
144         bw.write(" </sheet>");
145         bw.newLine();
146       }
147       
148       bw.write("</workbook>");
149       bw.newLine();
150
151       bw.flush();
152       bw.close();
153     }
154     catch (UnsupportedEncodingException JavaDoc e)
155     {
156       System.err.println(e.toString());
157     }
158   }
159
160   /**
161    * Writes out the workbook data as XML, with formatting information
162    */

163   private void writeFormattedXML() throws IOException JavaDoc
164   {
165     try
166     {
167       OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(out, encoding);
168       BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(osw);
169       
170       bw.write("<?xml version=\"1.0\" ?>");
171       bw.newLine();
172       bw.write("<!DOCTYPE workbook SYSTEM \"formatworkbook.dtd\">");
173       bw.newLine();
174       bw.newLine();
175       bw.write("<workbook>");
176       bw.newLine();
177       for (int sheet = 0; sheet < workbook.getNumberOfSheets(); sheet++)
178       {
179         Sheet s = workbook.getSheet(sheet);
180
181         bw.write(" <sheet>");
182         bw.newLine();
183         bw.write(" <name><![CDATA["+s.getName()+"]]></name>");
184         bw.newLine();
185       
186         Cell[] row = null;
187         CellFormat format = null;
188         Font font = null;
189       
190         for (int i = 0 ; i < s.getRows() ; i++)
191         {
192           bw.write(" <row number=\"" + i + "\">");
193           bw.newLine();
194           row = s.getRow(i);
195
196           for (int j = 0 ; j < row.length; j++)
197           {
198             // Remember that empty cells can contain format information
199
if ((row[j].getType() != CellType.EMPTY) ||
200                 (row[j].getCellFormat() != null))
201             {
202               format = row[j].getCellFormat();
203               bw.write(" <col number=\"" + j + "\">");
204               bw.newLine();
205               bw.write(" <data>");
206               bw.write("<![CDATA["+row[j].getContents()+"]]>");
207               bw.write("</data>");
208               bw.newLine();
209
210               if (row[j].getCellFormat() != null)
211               {
212                 bw.write(" <format wrap=\"" + format.getWrap() + "\"");
213                 bw.newLine();
214                 bw.write(" align=\"" +
215                          format.getAlignment().getDescription() + "\"");
216                 bw.newLine();
217                 bw.write(" valign=\"" +
218                          format.getVerticalAlignment().getDescription() + "\"");
219                 bw.newLine();
220                 bw.write(" orientation=\"" +
221                          format.getOrientation().getDescription() + "\"");
222                 bw.write(">");
223                 bw.newLine();
224
225                 // The font information
226
font = format.getFont();
227                 bw.write(" <font name=\"" + font.getName() + "\"");
228                 bw.newLine();
229                 bw.write(" point_size=\"" +
230                          font.getPointSize() + "\"");
231                 bw.newLine();
232                 bw.write(" bold_weight=\"" +
233                          font.getBoldWeight() + "\"");
234                 bw.newLine();
235                 bw.write(" italic=\"" + font.isItalic() + "\"");
236                 bw.newLine();
237                 bw.write(" underline=\"" +
238                          font.getUnderlineStyle().getDescription() + "\"");
239                 bw.newLine();
240                 bw.write(" colour=\"" +
241                          font.getColour().getDescription() + "\"");
242                 bw.newLine();
243                 bw.write(" script=\"" +
244                          font.getScriptStyle().getDescription() + "\"");
245                 bw.write(" />");
246                 bw.newLine();
247
248
249                 // The cell background information
250
if (format.getBackgroundColour() != Colour.DEFAULT_BACKGROUND ||
251                     format.getPattern() != Pattern.NONE)
252                 {
253                   bw.write(" <background colour=\"" +
254                            format.getBackgroundColour().getDescription() + "\"");
255                   bw.newLine();
256                   bw.write(" pattern=\"" +
257                            format.getPattern().getDescription() + "\"");
258                   bw.write(" />");
259                   bw.newLine();
260                 }
261
262
263                 // The cell border, if it has one
264
if (format.getBorder(Border.TOP ) != BorderLineStyle.NONE ||
265                     format.getBorder(Border.BOTTOM) != BorderLineStyle.NONE ||
266                     format.getBorder(Border.LEFT) != BorderLineStyle.NONE ||
267                     format.getBorder(Border.RIGHT) != BorderLineStyle.NONE)
268                 {
269                   
270                   bw.write(" <border top=\"" +
271                            format.getBorder(Border.TOP).getDescription() + "\"");
272                   bw.newLine();
273                   bw.write(" bottom=\"" +
274                            format.getBorder(Border.BOTTOM).getDescription() +
275                            "\"");
276                   bw.newLine();
277                   bw.write(" left=\"" +
278                            format.getBorder(Border.LEFT).getDescription() + "\"");
279                   bw.newLine();
280                   bw.write(" right=\"" +
281                            format.getBorder(Border.RIGHT).getDescription() + "\"");
282                   bw.write(" />");
283                   bw.newLine();
284                 }
285
286                 // The cell number/date format
287
if (!format.getFormat().getFormatString().equals(""))
288                 {
289                   bw.write(" <format_string string=\"");
290                   bw.write(format.getFormat().getFormatString());
291                   bw.write("\" />");
292                   bw.newLine();
293                 }
294
295                 bw.write(" </format>");
296                 bw.newLine();
297               }
298
299               bw.write(" </col>");
300               bw.newLine();
301             }
302           }
303           bw.write(" </row>");
304           bw.newLine();
305         }
306         bw.write(" </sheet>");
307         bw.newLine();
308       }
309       
310       bw.write("</workbook>");
311       bw.newLine();
312
313       bw.flush();
314       bw.close();
315     }
316     catch (UnsupportedEncodingException JavaDoc e)
317     {
318       System.err.println(e.toString());
319     }
320   }
321
322 }
323
324
325
326
327
328
329
Popular Tags