KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > demo > CSV


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
33 /**
34  * Simple demo class which uses the api to present the contents
35  * of an excel 97 spreadsheet as comma separated values, using a workbook
36  * and output stream of your choice
37  */

38 public class CSV
39 {
40   /**
41    * Constructor
42    *
43    * @param w The workbook to interrogate
44    * @param out The output stream to which the CSV values are written
45    * @param encoding The encoding used by the output stream. Null or
46    * unrecognized values cause the encoding to default to UTF8
47    * @param hide Suppresses hidden cells
48    * @exception java.io.IOException
49    */

50   public CSV(Workbook w, OutputStream JavaDoc out, String JavaDoc encoding, boolean hide)
51     throws IOException JavaDoc
52   {
53     if (encoding == null || !encoding.equals("UnicodeBig"))
54     {
55       encoding = "UTF8";
56     }
57
58     try
59     {
60       OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(out, encoding);
61       BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(osw);
62       
63       for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++)
64       {
65         Sheet s = w.getSheet(sheet);
66
67         if (!(hide && s.getSettings().isHidden()))
68         {
69           bw.write(s.getName());
70           bw.newLine();
71           
72           Cell[] row = null;
73           
74           for (int i = 0 ; i < s.getRows() ; i++)
75           {
76             row = s.getRow(i);
77             
78             if (row.length > 0)
79             {
80               if (!(hide && row[0].isHidden()))
81               {
82                 bw.write(row[0].getContents());
83                 // Java 1.4 code to handle embedded commas
84
// bw.write("\"" + row[0].getContents().replaceAll("\"","\"\"") + "\"");
85
}
86               
87               for (int j = 1; j < row.length; j++)
88               {
89                 bw.write(',');
90                 if (!(hide && row[j].isHidden()))
91                 {
92                   bw.write(row[j].getContents());
93                   // Java 1.4 code to handle embedded quotes
94
// bw.write("\"" + row[j].getContents().replaceAll("\"","\"\"") + "\"");
95
}
96               }
97             }
98             bw.newLine();
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
114
Popular Tags