KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > excel > XlsDataSet


1 /*
2  * The DbUnit Database Testing Framework
3  * Copyright (C)2002-2004, DbUnit.org
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 package org.dbunit.dataset.excel;
20
21 import org.apache.poi.hssf.usermodel.HSSFCell;
22 import org.apache.poi.hssf.usermodel.HSSFRow;
23 import org.apache.poi.hssf.usermodel.HSSFSheet;
24 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25 import org.dbunit.dataset.*;
26 import org.dbunit.dataset.datatype.DataType;
27
28 import java.io.*;
29
30 /**
31  * This dataset implementation can read and write MS Excel documents. Each
32  * sheet represents a table. The first row of a sheet defines the columns names
33  * and remaining rows contains the data.
34  *
35  * @author Manuel Laflamme
36  * @since Feb 21, 2003
37  * @version $Revision: 1.6 $
38  */

39 public class XlsDataSet extends AbstractDataSet
40 {
41     private final ITable[] _tables;
42
43     /**
44      * Creates a new XlsDataSet object that loads the specified Excel document.
45      */

46     public XlsDataSet(File file) throws IOException, DataSetException
47     {
48         this(new FileInputStream(file));
49     }
50
51     /**
52      * Creates a new XlsDataSet object that loads the specified Excel document.
53      */

54     public XlsDataSet(InputStream in) throws IOException, DataSetException
55     {
56         HSSFWorkbook workbook = new HSSFWorkbook(in);
57         _tables = new ITable[workbook.getNumberOfSheets()];
58         for (int i = 0; i < _tables.length; i++)
59         {
60             _tables[i] = new XlsTable(workbook.getSheetName(i),
61                     workbook.getSheetAt(i));
62         }
63     }
64
65     /**
66      * Write the specified dataset to the specified Excel document.
67      */

68     public static void write(IDataSet dataSet, OutputStream out)
69             throws IOException, DataSetException
70     {
71         HSSFWorkbook workbook = new HSSFWorkbook();
72
73         int index = 0;
74         ITableIterator iterator = dataSet.iterator();
75         while(iterator.next())
76         {
77             // create the table i.e. sheet
78
ITable table = iterator.getTable();
79             ITableMetaData metaData = table.getTableMetaData();
80             HSSFSheet sheet = workbook.createSheet(metaData.getTableName());
81
82             // write table metadata i.e. first row in sheet
83
workbook.setSheetName(index, metaData.getTableName(), HSSFWorkbook.ENCODING_UTF_16);
84
85             HSSFRow headerRow = sheet.createRow(0);
86             Column[] columns = metaData.getColumns();
87             for (int j = 0; j < columns.length; j++)
88             {
89                 Column column = columns[j];
90                 HSSFCell cell = headerRow.createCell((short)j);
91                 cell.setEncoding(HSSFCell.ENCODING_UTF_16);
92                 cell.setCellValue(column.getColumnName());
93             }
94
95             // write table data
96
for (int j = 0; j < table.getRowCount(); j++)
97             {
98                 HSSFRow row = sheet.createRow(j + 1);
99                 for (int k = 0; k < columns.length; k++)
100                 {
101                     Column column = columns[k];
102                     Object JavaDoc value = table.getValue(j, column.getColumnName());
103                     if (value != null)
104                     {
105                         HSSFCell cell = row.createCell((short)k);
106                         cell.setEncoding(HSSFCell.ENCODING_UTF_16);
107                         cell.setCellValue(DataType.asString(value));
108                     }
109                 }
110             }
111
112             index++;
113         }
114
115         // write xls document
116
workbook.write(out);
117         out.flush();
118     }
119
120     ////////////////////////////////////////////////////////////////////////////
121
// AbstractDataSet class
122

123     protected ITableIterator createIterator(boolean reversed)
124             throws DataSetException
125     {
126         return new DefaultTableIterator(_tables, reversed);
127     }
128 }
129
Popular Tags