Example usage for org.apache.poi.ss SpreadsheetVersion getMaxColumns

List of usage examples for org.apache.poi.ss SpreadsheetVersion getMaxColumns

Introduction

In this page you can find the example usage for org.apache.poi.ss SpreadsheetVersion getMaxColumns.

Prototype

public int getMaxColumns() 

Source Link

Usage

From source file:com.asakusafw.testdriver.excel.ExcelSheetSinkFactory.java

License:Apache License

@Override
public <T> DataModelSink createSink(DataModelDefinition<T> definition, TestContext context) throws IOException {
    if (definition == null) {
        throw new IllegalArgumentException("definition must not be null"); //$NON-NLS-1$
    }/*  ww  w. ja  v  a2s .  com*/
    if (context == null) {
        throw new IllegalArgumentException("context must not be null"); //$NON-NLS-1$
    }

    SpreadsheetVersion version = Util.getSpreadsheetVersionFor(output.getPath());
    if (definition.getProperties().size() > version.getMaxColumns()) {
        LOG.warn(MessageFormat.format(Messages.getString("ExcelSheetSinkFactory.warnExceedColumnCount"), //$NON-NLS-1$
                definition.getModelClass().getName(), version.getMaxColumns(), output));
    }
    File parent = output.getParentFile();
    if (parent != null && parent.isDirectory() == false && parent.mkdirs() == false) {
        throw new IOException(MessageFormat.format(
                Messages.getString("ExcelSheetSinkFactory.errorFailedToCreateOutputDirectory"), //$NON-NLS-1$
                output));
    }
    final Workbook workbook = Util.createEmptyWorkbookFor(output.getPath());
    Sheet sheet = workbook.createSheet("results"); //$NON-NLS-1$
    return new ExcelSheetSink(definition, sheet, version.getMaxColumns()) {
        private boolean closed = false;

        @Override
        public void close() throws IOException {
            if (closed) {
                return;
            }
            closed = true;
            LOG.info(MessageFormat.format(Messages.getString("ExcelSheetSinkFactory.infoStartOutput"), //$NON-NLS-1$
                    output));
            try (OutputStream stream = new FileOutputStream(output);) {
                workbook.write(stream);
            }
        }
    };
}