KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > demo > WriteAccess


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.BufferedWriter JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import jxl.WorkbookSettings;
27 import jxl.biff.Type;
28 import jxl.biff.StringHelper;
29 import jxl.read.biff.File;
30 import jxl.read.biff.BiffRecordReader;
31 import jxl.read.biff.BiffException;
32 import jxl.read.biff.Record;
33
34 /**
35  * Displays whatever generated the excel file (ie. the WriteAccess record)
36  */

37 class WriteAccess
38 {
39   private BiffRecordReader reader;
40
41   public WriteAccess(java.io.File JavaDoc file)
42     throws IOException JavaDoc, BiffException
43   {
44     WorkbookSettings ws = new WorkbookSettings();
45     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
46     File f = new File(fis, ws);
47     reader = new BiffRecordReader(f);
48
49     display(ws);
50     fis.close();
51   }
52
53   /**
54    * Dumps out the contents of the excel file
55    */

56   private void display(WorkbookSettings ws) throws IOException JavaDoc
57   {
58     Record r = null;
59     boolean found = false;
60     while (reader.hasNext() && !found)
61     {
62       r = reader.next();
63       if (r.getType() == Type.WRITEACCESS)
64       {
65         found = true;
66       }
67     }
68
69     if (!found)
70     {
71       System.err.println("Warning: could not find write access record");
72       return;
73     }
74
75     byte[] data = r.getData();
76
77     String JavaDoc s = null;
78
79     s = StringHelper.getString(data, data.length, 0, ws);
80     
81     System.out.println(s);
82   }
83 }
84
Popular Tags