View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.dataset.excel;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  
29  import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
30  import org.apache.poi.ss.usermodel.Workbook;
31  import org.apache.poi.ss.usermodel.WorkbookFactory;
32  import org.dbunit.dataset.AbstractDataSet;
33  import org.dbunit.dataset.DataSetException;
34  import org.dbunit.dataset.DefaultTableIterator;
35  import org.dbunit.dataset.IDataSet;
36  import org.dbunit.dataset.ITable;
37  import org.dbunit.dataset.ITableIterator;
38  import org.dbunit.dataset.OrderedTableNameMap;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * This dataset implementation can read and write MS Excel documents. Each
44   * sheet represents a table. The first row of a sheet defines the columns names
45   * and remaining rows contains the data.
46   *
47   * @author Manuel Laflamme
48   * @since Feb 21, 2003
49   * @version $Revision$
50   */
51  public class XlsDataSet extends AbstractDataSet
52  {
53      
54      /**
55       * Logger for this class
56       */
57      private static final Logger logger = LoggerFactory.getLogger(XlsDataSet.class);
58  
59      private final OrderedTableNameMap _tables;
60  
61  
62      /**
63       * Creates a new XlsDataSet object that loads the specified Excel document.
64       */
65      public XlsDataSet(File file) throws IOException, DataSetException
66      {
67          this(new FileInputStream(file));
68      }
69  
70      /**
71       * Creates a new XlsDataSet object that loads the specified Excel document.
72       */
73      public XlsDataSet(InputStream in) throws IOException, DataSetException
74      {
75          _tables = super.createTableNameMap();
76  
77          Workbook workbook;
78          try {
79              workbook = WorkbookFactory.create(in);
80          } catch (InvalidFormatException e) {
81              throw new IOException(e);
82          }
83  		
84          int sheetCount = workbook.getNumberOfSheets();
85          for (int i = 0; i < sheetCount; i++)
86          {
87              ITable table = new XlsTable(workbook.getSheetName(i),
88                      workbook.getSheetAt(i));
89              _tables.add(table.getTableMetaData().getTableName(), table);            
90          }
91      }
92  
93      /**
94       * Write the specified dataset to the specified Excel document.
95       */
96      public static void write(IDataSet dataSet, OutputStream out)
97              throws IOException, DataSetException
98      {
99          logger.debug("write(dataSet={}, out={}) - start", dataSet, out);
100 
101         new XlsDataSetWriter().write(dataSet, out);
102     }
103 
104 
105     ////////////////////////////////////////////////////////////////////////////
106     // AbstractDataSet class
107 
108     protected ITableIterator createIterator(boolean reversed)
109             throws DataSetException
110     {
111     	if(logger.isDebugEnabled())
112     		logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
113 
114     	ITable[] tables = (ITable[]) _tables.orderedValues().toArray(new ITable[0]);
115         return new DefaultTableIterator(tables, reversed);
116     }
117 }