投稿‎ > ‎

RangeDataSet.java

posted Mar 15, 2016, 9:36 PM by Zhang Wenxu
package jp.btsol.dbunit.dataset.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbok;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.dbunit.dataset.AbstractDataSet;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.DefaultTableIterator;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.ITableIterator;
import org.dbunit.dataset.OrderedTableNameMap;
import org dbunit.dataset.excel.XlsDataSetWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RangeDataSet extends AbstractDataSet{
    private static final Logger logger = LoggerFactory.getLogger(RangeDataSet.class);
    private final OrderedTableNameMap _tables;

    public RangeDataSet(File file) throws IOException, DataSetException{
        This(new FileInputStream(file));
    }
   public RangeDataSet(InputStream in) throws IOException, DataSetException{
        _tables = supper.createTableNameMap();
        Workbook workbook;
        try{
             Workbook = WorkbookFactory.create(in);
         } catch(InvalidFormatException e){
             throw new IOException(e);
        }

        int nameCount = workbook.getNumberOfNames();
        for(int i = 0; i < nameCount; i++){
             ITable table = new RangeTable(workbook.getNameAt(i).getNameName(), workbook.getSheetAt(workbook.getNameAt(i).getSheetIndex()), workbook.getNameAt(i));
            _tables.add(table.getTableMetadata().getTableName(), table);
       }
    }

    public static void write(IDataSet dataSet, OutputStream out){
        throws IOException, DataSetException{
        new XlsDataSetWriter().write(dataSet, out);
    }
    protected ITableIterator createIterator(boolean reversed){
        throws DataSetException{
        ITable[] tables = (ITable[])_tables.orderdValues().toArray(new ITable[0]);
        return new DefaultTableIterator(tables, reversed);
    }
}

Comments