Example usage for org.apache.poi.ss.usermodel Workbook getSheet

List of usage examples for org.apache.poi.ss.usermodel Workbook getSheet

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Workbook getSheet.

Prototype

Sheet getSheet(String name);

Source Link

Document

Get sheet with the given name

Usage

From source file:xqt.adapters.csv.MSExcelDataAdapterHelper.java

@Override
public LinkedHashMap<String, FieldInfo> getContinerSchema(SingleContainer container, Object... params) {
    if (isHeaderExternal(container)) {
        return super.getContinerSchema(container, params);
    } else {//from ww  w .  j a  v  a  2s  . c o m
        try {
            LinkedHashMap<String, FieldInfo> headers = new LinkedHashMap<>();
            String baseContainerPath = String.valueOf(params[0]);
            //String columnDelimiter =    String.valueOf(params[1]);
            String typeDelimiter = String.valueOf(params[2]);
            String unitDelimiter = String.valueOf(params[3]);

            String fileName = getCompleteSourceName(container, baseContainerPath);
            HeaderBuilder hb = new HeaderBuilder();
            //XSSFWorkbook workbook2 = new XSSFWorkbook(fileName);
            InputStream inp = new FileInputStream(fileName);
            Workbook workbook = WorkbookFactory.create(inp);
            Sheet sheet = null;
            String sheetName = container.getContainerName();
            if (sheetName != null && !sheetName.isEmpty()) {
                sheet = workbook.getSheet(sheetName);
            }
            if (sheet == null) { // sheetName is not valid or is an index
                sheet = workbook.getSheetAt(container.getContainerIndex());
            }
            FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
            if (sheet.rowIterator().hasNext()) {
                Row row = sheet.getRow(0);
                String[] cellvalues = RowBuilder.createRowArray(row, evaluator);
                int indexCount = 0;
                for (String cell : cellvalues) {
                    if (cell != null && !cell.isEmpty()) {
                        FieldInfo field = hb.convert(cell, typeDelimiter, unitDelimiter);
                        field.index = indexCount;
                        if (field.name != null && !field.name.isEmpty() && !headers.containsKey(field.name)) {
                            headers.put(field.name, field);
                            indexCount++;
                        }
                    }
                }
            }
            for (FieldInfo field : headers.values()) {
                field.conceptualDataType = getConceptualType(field.internalDataType);
            }
            return headers;
        } catch (IOException | InvalidFormatException ex) {
            LoggerHelper.logError(
                    MessageFormat.format("Schema generation error for adapter: \'CSV\'. {0}", ex.getMessage()));
            return new LinkedHashMap<>();
        }
    }
}