001: /*
002: * The DbUnit Database Testing Framework
003: * Copyright (C)2002-2004, DbUnit.org
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.dbunit.dataset.excel;
020:
021: import org.slf4j.Logger;
022: import org.slf4j.LoggerFactory;
023:
024: import org.apache.poi.hssf.usermodel.HSSFCell;
025: import org.apache.poi.hssf.usermodel.HSSFRow;
026: import org.apache.poi.hssf.usermodel.HSSFSheet;
027: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
028: import org.dbunit.dataset.*;
029: import org.dbunit.dataset.datatype.DataType;
030:
031: import java.io.*;
032:
033: /**
034: * This dataset implementation can read and write MS Excel documents. Each
035: * sheet represents a table. The first row of a sheet defines the columns names
036: * and remaining rows contains the data.
037: *
038: * @author Manuel Laflamme
039: * @since Feb 21, 2003
040: * @version $Revision: 554 $
041: */
042: public class XlsDataSet extends AbstractDataSet {
043:
044: /**
045: * Logger for this class
046: */
047: private static final Logger logger = LoggerFactory
048: .getLogger(XlsDataSet.class);
049:
050: private final ITable[] _tables;
051:
052: /**
053: * Creates a new XlsDataSet object that loads the specified Excel document.
054: */
055: public XlsDataSet(File file) throws IOException, DataSetException {
056: this (new FileInputStream(file));
057: }
058:
059: /**
060: * Creates a new XlsDataSet object that loads the specified Excel document.
061: */
062: public XlsDataSet(InputStream in) throws IOException,
063: DataSetException {
064: HSSFWorkbook workbook = new HSSFWorkbook(in);
065: _tables = new ITable[workbook.getNumberOfSheets()];
066: for (int i = 0; i < _tables.length; i++) {
067: _tables[i] = new XlsTable(workbook.getSheetName(i),
068: workbook.getSheetAt(i));
069: }
070: }
071:
072: /**
073: * Write the specified dataset to the specified Excel document.
074: */
075: public static void write(IDataSet dataSet, OutputStream out)
076: throws IOException, DataSetException {
077: logger.debug("write(dataSet=" + dataSet + ", out=" + out
078: + ") - start");
079:
080: HSSFWorkbook workbook = new HSSFWorkbook();
081:
082: int index = 0;
083: ITableIterator iterator = dataSet.iterator();
084: while (iterator.next()) {
085: // create the table i.e. sheet
086: ITable table = iterator.getTable();
087: ITableMetaData metaData = table.getTableMetaData();
088: HSSFSheet sheet = workbook.createSheet(metaData
089: .getTableName());
090:
091: // write table metadata i.e. first row in sheet
092: workbook.setSheetName(index, metaData.getTableName(),
093: HSSFWorkbook.ENCODING_UTF_16);
094:
095: HSSFRow headerRow = sheet.createRow(0);
096: Column[] columns = metaData.getColumns();
097: for (int j = 0; j < columns.length; j++) {
098: Column column = columns[j];
099: HSSFCell cell = headerRow.createCell((short) j);
100: cell.setEncoding(HSSFCell.ENCODING_UTF_16);
101: cell.setCellValue(column.getColumnName());
102: }
103:
104: // write table data
105: for (int j = 0; j < table.getRowCount(); j++) {
106: HSSFRow row = sheet.createRow(j + 1);
107: for (int k = 0; k < columns.length; k++) {
108: Column column = columns[k];
109: Object value = table.getValue(j, column
110: .getColumnName());
111: if (value != null) {
112: HSSFCell cell = row.createCell((short) k);
113: cell.setEncoding(HSSFCell.ENCODING_UTF_16);
114: cell.setCellValue(DataType.asString(value));
115: }
116: }
117: }
118:
119: index++;
120: }
121:
122: // write xls document
123: workbook.write(out);
124: out.flush();
125: }
126:
127: ////////////////////////////////////////////////////////////////////////////
128: // AbstractDataSet class
129:
130: protected ITableIterator createIterator(boolean reversed)
131: throws DataSetException {
132: logger.debug("createIterator(reversed=" + reversed
133: + ") - start");
134:
135: return new DefaultTableIterator(_tables, reversed);
136: }
137: }
|