Example usage for org.apache.poi.ss.usermodel Sheet getSheetName

List of usage examples for org.apache.poi.ss.usermodel Sheet getSheetName

Introduction

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

Prototype

String getSheetName();

Source Link

Document

Returns the name of this sheet

Usage

From source file:org.talend.dataprep.schema.xls.XlsSchemaParser.java

License:Open Source License

/**
 * Return the columns metadata for the given sheet.
 *
 * @param sheet the sheet to look at./* w w w .  j a v a 2s  .c  o m*/
 * @param datasetId the dataset id.
 * @return the columns metadata for the given sheet.
 */
private List<ColumnMetadata> parsePerSheet(Sheet sheet, String datasetId, FormulaEvaluator formulaEvaluator) {

    LOGGER.debug(Markers.dataset(datasetId), "parsing sheet '{}'", sheet.getSheetName());

    // Map<ColId, Map<RowId, type>>
    SortedMap<Integer, SortedMap<Integer, String>> cellsTypeMatrix = collectSheetTypeMatrix(sheet,
            formulaEvaluator);
    int averageHeaderSize = guessHeaderSize(cellsTypeMatrix);

    // here we have information regarding types for each rows/col (yup a Matrix!! :-) )
    // so we can analyse and guess metadata (column type, header value)
    final List<ColumnMetadata> columnsMetadata = new ArrayList<>(cellsTypeMatrix.size());

    cellsTypeMatrix.forEach((colId, typePerRowMap) -> {

        Type type = guessColumnType(colId, typePerRowMap, averageHeaderSize);

        String headerText = null;
        if (averageHeaderSize == 1 && sheet.getRow(0) != null) {
            // so header value is the first row of the column
            Cell headerCell = sheet.getRow(0).getCell(colId);
            headerText = XlsUtils.getCellValueAsString(headerCell, formulaEvaluator);
        }

        // header text cannot be null so use a default one
        if (StringUtils.isEmpty(headerText)) {
            // +1 because it starts from 0
            headerText = message("import.local.generated_column_name", colId + 1);
        }

        // FIXME what do we do if header size is > 1 concat all lines?
        columnsMetadata.add(ColumnMetadata.Builder //
                .column() //
                .headerSize(averageHeaderSize) //
                .name(headerText) //
                .type(type) //
                .build());

    });

    return columnsMetadata;
}

From source file:org.teiid.translator.excel.ExcelMetadataProcessor.java

License:Open Source License

private void addTable(MetadataFactory mf, Sheet sheet, String xlsName) {
    int firstRowNumber = sheet.getFirstRowNum();
    Row headerRow = null;/*from  w  w w .j a va2s .  c om*/
    int firstCellNumber = -1;
    if (this.hasHeader) {
        headerRow = sheet.getRow(this.headerRowNumber);
        if (headerRow != null) {
            firstRowNumber = this.headerRowNumber;
            firstCellNumber = headerRow.getFirstCellNum();
            if (firstCellNumber == -1) {
                LogManager.logInfo(LogConstants.CTX_CONNECTOR,
                        ExcelPlugin.Util.gs(ExcelPlugin.Event.TEIID23006, xlsName));
                return;
            }
        }
    }

    if (headerRow == null) {
        while (firstCellNumber == -1) {
            headerRow = sheet.getRow(firstRowNumber++);
            // check if this is a empty sheet; the data must be present in first 10000 rows
            if (headerRow == null && firstRowNumber > 10000) {
                return;
            }
            if (headerRow == null) {
                continue;
            }
            firstCellNumber = headerRow.getFirstCellNum();
        }
    }

    // create a table for each sheet
    AtomicInteger columnCount = new AtomicInteger();
    Table table = mf.addTable(sheet.getSheetName());
    table.setNameInSource(sheet.getSheetName());
    table.setProperty(ExcelMetadataProcessor.FILE, xlsName);

    // add implicit row_id column based on row number from excel sheet 
    Column column = mf.addColumn(ROW_ID, TypeFacility.RUNTIME_NAMES.INTEGER, table);
    column.setSearchType(SearchType.All_Except_Like);
    column.setProperty(CELL_NUMBER, ROW_ID);
    mf.addPrimaryKey("PK0", Arrays.asList(ROW_ID), table); //$NON-NLS-1$
    column.setUpdatable(false);

    Row dataRow = null;
    int lastCellNumber = headerRow.getLastCellNum();

    if (this.hasDataRowNumber) {
        // adjust for zero index
        table.setProperty(ExcelMetadataProcessor.FIRST_DATA_ROW_NUMBER, String.valueOf(this.dataRowNumber + 1));
        dataRow = sheet.getRow(this.dataRowNumber);
    } else if (this.hasHeader) {
        // +1 zero based, +1 to skip header
        table.setProperty(ExcelMetadataProcessor.FIRST_DATA_ROW_NUMBER, String.valueOf(firstRowNumber + 2));
        dataRow = sheet.getRow(firstRowNumber + 1);
    } else {
        //+1 already occurred because of the increment above
        table.setProperty(ExcelMetadataProcessor.FIRST_DATA_ROW_NUMBER, String.valueOf(firstRowNumber));
        dataRow = sheet.getRow(firstRowNumber);
    }

    if (firstCellNumber != -1) {
        for (int j = firstCellNumber; j < lastCellNumber; j++) {
            Cell headerCell = headerRow.getCell(j);
            Cell dataCell = dataRow.getCell(j);
            // if the cell value is null; then advance the data row cursor to to find it 
            if (dataCell == null) {
                for (int rowNo = firstRowNumber + 1; rowNo < firstRowNumber + 10000; rowNo++) {
                    Row row = sheet.getRow(rowNo);
                    dataCell = row.getCell(j);
                    if (dataCell != null) {
                        break;
                    }
                }
            }
            column = mf.addColumn(cellName(headerCell, columnCount), cellType(headerCell, dataCell), table);
            column.setSearchType(SearchType.Unsearchable);
            column.setProperty(ExcelMetadataProcessor.CELL_NUMBER, String.valueOf(j + 1));
        }
    }
}

From source file:org.tiefaces.components.websheet.configuration.ConfigurationHandler.java

License:MIT License

/**
 * Gets the sheet configuration./*from  w  w  w . j a  v  a2s  .  c  om*/
 *
 * @param sheet
 *            the sheet
 * @param formName
 *            the form name
 * @param sheetRightCol
 *            the sheet right col
 * @return the sheet configuration
 */
private SheetConfiguration getSheetConfiguration(final Sheet sheet, final String formName,
        final int sheetRightCol) {

    SheetConfiguration sheetConfig = new SheetConfiguration();
    sheetConfig.setFormName(formName);
    sheetConfig.setSheetName(sheet.getSheetName());
    int leftCol = sheet.getLeftCol();
    int lastRow = sheet.getLastRowNum();
    int firstRow = sheet.getFirstRowNum();
    int rightCol = 0;
    int maxRow = 0;
    for (Row row : sheet) {
        if (row.getRowNum() > TieConstants.TIE_WEB_SHEET_MAX_ROWS) {
            break;
        }
        maxRow = row.getRowNum();
        int firstCellNum = row.getFirstCellNum();
        if (firstCellNum >= 0 && firstCellNum < leftCol) {
            leftCol = firstCellNum;
        }
        if ((row.getLastCellNum() - 1) > rightCol) {
            int verifiedcol = verifyLastCell(row, rightCol, sheetRightCol);
            if (verifiedcol > rightCol) {
                rightCol = verifiedcol;
            }
        }
    }
    if (maxRow < lastRow) {
        lastRow = maxRow;
    }
    // header range row set to 0 while column set to first column to
    // max
    // column (FF) e.g. $A$0 : $FF$0
    String tempStr = TieConstants.CELL_ADDR_PRE_FIX + WebSheetUtility.getExcelColumnName(leftCol)
            + TieConstants.CELL_ADDR_PRE_FIX + "0 : " + TieConstants.CELL_ADDR_PRE_FIX
            + WebSheetUtility.getExcelColumnName(rightCol) + TieConstants.CELL_ADDR_PRE_FIX + "0";
    sheetConfig.setFormHeaderRange(tempStr);
    sheetConfig.setHeaderCellRange(new CellRange(tempStr));
    // body range row set to first row to last row while column set
    // to
    // first column to max column (FF) e.g. $A$1 : $FF$1000
    tempStr = TieConstants.CELL_ADDR_PRE_FIX + WebSheetUtility.getExcelColumnName(leftCol)
            + TieConstants.CELL_ADDR_PRE_FIX + (firstRow + 1) + " : " + TieConstants.CELL_ADDR_PRE_FIX
            + WebSheetUtility.getExcelColumnName(rightCol) + TieConstants.CELL_ADDR_PRE_FIX + (lastRow + 1);
    sheetConfig.setFormBodyRange(tempStr);
    sheetConfig.setBodyCellRange(new CellRange(tempStr));
    sheetConfig.setFormBodyType(org.tiefaces.common.TieConstants.FORM_TYPE_FREE);
    sheetConfig.setCellFormAttributes(new HashMap<String, List<CellFormAttributes>>());

    // check it's a hidden sheet
    int sheetIndex = parent.getWb().getSheetIndex(sheet);
    if (parent.getWb().isSheetHidden(sheetIndex) || parent.getWb().isSheetVeryHidden(sheetIndex)) {
        sheetConfig.setHidden(true);
    }

    return sheetConfig;

}

From source file:org.tiefaces.components.websheet.configuration.ConfigurationHandler.java

License:MIT License

/**
 * build top level configuration map from command list. User can either put
 * tie:form command in the comments (which will transfer to sheetConfig), Or
 * just ignore it, then use whole sheet as one form.
 *
 * @param sheet/*from w w  w  .j  av a 2s  .  c  o  m*/
 *            sheet.
 * @param sheetConfigMap
 *            sheetConfigMap.
 * @param commandList
 *            command list.
 * @param formList
 *            form list.
 * @param sheetRightCol
 *            the sheet right col
 */
private void buildSheetConfigMapFromFormCommand(final Sheet sheet,
        final Map<String, SheetConfiguration> sheetConfigMap, final List<ConfigCommand> commandList,
        final List<String> formList, final int sheetRightCol) {
    boolean foundForm = false;
    int minRowNum = sheet.getLastRowNum();
    int maxRowNum = sheet.getFirstRowNum();
    for (Command command : commandList) {
        // check whether is form command
        if (command.getCommandTypeName().equalsIgnoreCase(TieConstants.COMMAND_FORM)) {
            foundForm = true;
            FormCommand fcommand = (FormCommand) command;
            sheetConfigMap.put(fcommand.getName(),
                    getSheetConfigurationFromConfigCommand(sheet, fcommand, sheetRightCol));
            formList.add(fcommand.getName());
            if (fcommand.getTopRow() < minRowNum) {
                minRowNum = fcommand.getTopRow();
            }
            if (fcommand.getLastRow() > maxRowNum) {
                maxRowNum = fcommand.getLastRow();
            }
        }
    }
    // if no form found, then use the whole sheet as form
    if (!foundForm) {
        WebSheetUtility.clearHiddenColumns(sheet);
        String formName = sheet.getSheetName();
        SheetConfiguration sheetConfig = getSheetConfiguration(sheet, formName, sheetRightCol);
        FormCommand fcommand = buildFormCommandFromSheetConfig(sheetConfig, sheet);
        commandList.add(fcommand);
        sheetConfig.setFormCommand(fcommand);
        sheetConfigMap.put(formName, sheetConfig);
        formList.add(formName);
        minRowNum = sheet.getFirstRowNum();
        maxRowNum = sheet.getLastRowNum();
    }

    // if skip config then return.
    if (parent.isSkipConfiguration()) {
        return;
    }
    SaveAttrsUtility.setSaveAttrsForSheet(sheet, minRowNum, maxRowNum,
            parent.getCellAttributesMap().getTemplateCommentMap().get(TieConstants.SAVE_COMMENT_KEY_IN_MAP));
}

From source file:org.tiefaces.components.websheet.configuration.ConfigurationHandler.java

License:MIT License

/**
 * Copy the each command area to seperated sheet. As it will be used for
 * iteration.//from w  ww .  j a va  2  s.co m
 * 
 * @param sheet
 *            sheet.
 */
private void copyTemplateForTieCommands(final Sheet sheet) {
    // if skip configuration. then return.
    if (parent.isSkipConfiguration()) {
        return;
    }
    Workbook wb = sheet.getWorkbook();
    String copyName = TieConstants.COPY_SHEET_PREFIX + sheet.getSheetName();
    if (wb.getSheet(copyName) == null) {
        Sheet newSheet = wb.cloneSheet(wb.getSheetIndex(sheet));
        int sheetIndex = wb.getSheetIndex(newSheet);
        wb.setSheetName(sheetIndex, copyName);
        wb.setSheetHidden(sheetIndex, Workbook.SHEET_STATE_VERY_HIDDEN);
    }
}

From source file:org.tiefaces.components.websheet.configuration.ConfigurationHandler.java

License:MIT License

/**
 * Create sheet configuration from form command.
 *
 * @param sheet//www . j a v  a2s.  com
 *            sheet.
 * @param fcommand
 *            form command.
 * @param sheetRightCol
 *            the sheet right col
 * @return sheet configuration.
 */
private SheetConfiguration getSheetConfigurationFromConfigCommand(final Sheet sheet, final FormCommand fcommand,
        final int sheetRightCol) {

    SheetConfiguration sheetConfig = new SheetConfiguration();
    sheetConfig.setFormName(fcommand.getName());
    sheetConfig.setSheetName(sheet.getSheetName());
    int leftCol = fcommand.getLeftCol();
    int lastRow = fcommand.getLastRow();
    int rightCol = 0;
    int maxRow = 0;
    for (Row row : sheet) {
        if (row.getRowNum() > TieConstants.TIE_WEB_SHEET_MAX_ROWS) {
            break;
        }
        maxRow = row.getRowNum();
        if ((row.getLastCellNum() - 1) > rightCol) {
            int verifiedcol = verifyLastCell(row, rightCol, sheetRightCol);
            if (verifiedcol > rightCol) {
                rightCol = verifiedcol;
            }
        }
    }
    if (maxRow < lastRow) {
        lastRow = maxRow;
    }
    // header range row set to 0 while column set to first column to
    // max
    // column (FF) e.g. $A$0 : $FF$0
    setHeaderOfSheetConfiguration(fcommand, sheetConfig, leftCol, rightCol);
    // body range row set to first row to last row while column set
    // to
    // first column to max column (FF) e.g. $A$1 : $FF$1000
    setBodyOfSheetConfiguration(fcommand, sheetConfig, leftCol, lastRow, rightCol);

    // footer range row set to 0 while column set to first column to
    // max
    // column (FF) e.g. $A$0 : $FF$0
    setFooterOfSheetConfiguration(fcommand, sheetConfig, leftCol, rightCol);

    String hidden = fcommand.getHidden();
    if ((hidden != null) && (Boolean.parseBoolean(hidden))) {
        sheetConfig.setHidden(true);
    }
    String fixedWidthStyle = fcommand.getFixedWidthStyle();
    if ((fixedWidthStyle != null) && (Boolean.parseBoolean(fixedWidthStyle))) {
        sheetConfig.setFixedWidthStyle(true);
    }
    sheetConfig.setFormCommand(fcommand);
    return sheetConfig;

}

From source file:org.tiefaces.components.websheet.service.ValidationHandler.java

License:MIT License

/**
 * Do validation.//from   w w w . j  a v a  2 s. c om
 *
 * @param value            the value
 * @param attr            the attr
 * @param rowIndex            the row index
 * @param colIndex the col index
 * @param sheet            the sheet
 * @return true, if successful
 */
private boolean doValidation(final Object value, final CellFormAttributes attr, final int rowIndex,
        final int colIndex, final Sheet sheet) {
    boolean pass;

    String attrValue = attr.getValue();
    attrValue = attrValue.replace("$value", value.toString() + "").replace("$rowIndex", rowIndex + "")
            .replace("$colIndex", colIndex + "").replace("$sheetName", sheet.getSheetName());
    attrValue = ConfigurationUtility.replaceExpressionWithCellValue(attrValue, rowIndex, sheet);
    if (attrValue.contains(TieConstants.EL_START)) {
        Object returnObj = FacesUtility.evaluateExpression(attrValue, Object.class);
        attrValue = returnObj.toString();
        pass = Boolean.parseBoolean(attrValue);
    } else {
        pass = parent.getCellHelper().evalBoolExpression(attrValue);
    }
    return pass;

}

From source file:org.tiefaces.components.websheet.utility.CommandUtility.java

License:MIT License

/**
 * Insert each template./*from w w w.j a v  a  2  s  . c  om*/
 *
 * @param sourceConfigRange
 *            the source config range
 * @param configBuildRef
 *            the config build ref
 * @param index
 *            the index
 * @param insertPosition
 *            the insert position
 * @param unitRowsMapping
 *            the unit rows mapping
 */
public static void insertEachTemplate(final ConfigRange sourceConfigRange, final ConfigBuildRef configBuildRef,
        final int index, final int insertPosition, final RowsMapping unitRowsMapping) {
    int srcStartRow = sourceConfigRange.getFirstRowAddr().getRow();
    int srcEndRow = sourceConfigRange.getLastRowPlusAddr().getRow() - 1;

    Sheet sheet = configBuildRef.getSheet();
    Workbook wb = sheet.getWorkbook();
    // excel sheet name has limit 31 chars
    String copyName = TieConstants.COPY_SHEET_PREFIX + sheet.getSheetName();
    if (copyName.length() > TieConstants.EXCEL_SHEET_NAME_LIMIT) {
        copyName = copyName.substring(0, TieConstants.EXCEL_SHEET_NAME_LIMIT);
    }
    Sheet srcSheet = wb.getSheet(copyName);
    if (index > 0) {
        CellUtility.copyRows(srcSheet, sheet, srcStartRow, srcEndRow, insertPosition, false, true);
    }

    for (int rowIndex = srcStartRow; rowIndex <= srcEndRow; rowIndex++) {
        if (configBuildRef.getWatchList().contains(rowIndex)
                && (ConfigurationUtility.isStaticRow(sourceConfigRange, rowIndex))) {
            unitRowsMapping.addRow(rowIndex, sheet.getRow(insertPosition + rowIndex - srcStartRow));
        }
    }
}

From source file:org.tiefaces.components.websheet.utility.WebSheetUtility.java

License:MIT License

/**
 * return full name for cell with sheet name and $ format e.g. Sheet1$A$1
 * /*from   w  w  w  .  j a v a2  s.c o m*/
 * @param sheet1
 *            sheet
 * @param cell
 *            cell
 * @return String full cell reference name
 */

public static String getFullCellRefName(final Sheet sheet1, final Cell cell) {
    if ((sheet1 != null) && (cell != null)) {
        return sheet1.getSheetName() + "!$" + getExcelColumnName(cell.getColumnIndex()) + "$"
                + (cell.getRowIndex() + 1);
    }
    return null;
}

From source file:org.ujmp.poi.AbstractMatrixExcelImporter.java

License:Open Source License

public DenseObjectMatrix2D importFromSheet(final Sheet sheet) throws InvalidFormatException, IOException {
    final int rowCount = sheet.getLastRowNum();
    int columnCount = 0;

    Iterator<Row> rowIterator = sheet.rowIterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();/*from   w w  w .  j  a v a  2s . c  om*/
        if (row.getLastCellNum() > columnCount) {
            columnCount = row.getLastCellNum();
        }
    }

    final DefaultDenseObjectMatrix2D matrix = new DefaultDenseObjectMatrix2D(rowCount, columnCount);
    matrix.setLabel(sheet.getSheetName());

    for (int r = 0; r < rowCount; r++) {
        Row row = sheet.getRow(r);
        if (row != null) {
            for (int c = 0; c < columnCount; c++) {
                Cell cell = row.getCell(c);
                if (cell != null) {
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_BLANK:
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        matrix.setAsBoolean(cell.getBooleanCellValue(), r, c);
                        break;
                    case Cell.CELL_TYPE_ERROR:
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        matrix.setAsString(cell.getCellFormula(), r, c);
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        matrix.setAsDouble(cell.getNumericCellValue(), r, c);
                        break;
                    case Cell.CELL_TYPE_STRING:
                        matrix.setAsString(cell.getStringCellValue(), r, c);
                        break;
                    default:
                        break;
                    }

                }
            }
        }
    }

    return matrix;
}