Example usage for org.apache.poi.hssf.usermodel HSSFName setSheetIndex

List of usage examples for org.apache.poi.hssf.usermodel HSSFName setSheetIndex

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFName setSheetIndex.

Prototype

public void setSheetIndex(int index) 

Source Link

Document

Specifies if the defined name is a local name, and if so, which sheet it is on.

Usage

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

License:Open Source License

protected void createTextCell(final JRPrintText textElement, final JRExporterGridCell gridCell,
        final int colIndex, final int rowIndex, final JRStyledText styledText, final StyleInfo baseStyle,
        final short forecolor) throws JRException {
    String formula = getFormula(textElement);
    String textStr = styledText.getText();

    if (formula != null) {
        try {/*from  w  ww.  j  a  v  a  2s.c  om*/
            TextValue value = getTextValue(textElement, textStr);

            if (value instanceof NumberTextValue) {
                String convertedPattern = getConvertedPattern(textElement,
                        ((NumberTextValue) value).getPattern());
                if (convertedPattern != null) {
                    baseStyle.setDataFormat(dataFormat.getFormat(convertedPattern));
                }
            } else if (value instanceof DateTextValue) {
                String convertedPattern = getConvertedPattern(textElement,
                        ((DateTextValue) value).getPattern());
                if (convertedPattern != null) {
                    baseStyle.setDataFormat(dataFormat.getFormat(convertedPattern));
                }
            }

            HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
            cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
            cell.setCellFormula(formula);
            endCreateCell(cellStyle);
            return;
        } catch (Exception e)//FIXMENOW what exceptions could we get here?
        {
            if (log.isWarnEnabled()) {
                log.warn(e.getMessage());
            }
        }
    }

    XlsReportConfiguration configuration = getCurrentItemConfiguration();

    if (configuration.isDetectCellType()) {
        TextValue value = getTextValue(textElement, textStr);
        value.handle(new TextValueHandler() {
            public void handle(StringTextValue textValue) {
                HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
                if (JRCommonText.MARKUP_NONE.equals(textElement.getMarkup())) {
                    setStringCellValue(textValue.getText());
                } else {
                    setRichTextStringCellValue(styledText, forecolor, textElement, getTextLocale(textElement));
                }
                endCreateCell(cellStyle);
            }

            public void handle(NumberTextValue textValue) {
                String convertedPattern = getConvertedPattern(textElement, textValue.getPattern());
                if (convertedPattern != null) {
                    baseStyle.setDataFormat(dataFormat.getFormat(convertedPattern));
                }

                HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
                if (textValue.getValue() == null) {
                    cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                } else {
                    cell.setCellValue(textValue.getValue().doubleValue());
                }
                endCreateCell(cellStyle);
            }

            public void handle(DateTextValue textValue) {
                baseStyle.setDataFormat(
                        dataFormat.getFormat(getConvertedPattern(textElement, textValue.getPattern())//FIXMEFORMAT why no null test like in numeric above?
                ));
                HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
                Date date = textValue.getValue();
                if (date == null) {
                    cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                } else {
                    date = translateDateValue(textElement, date);
                    cell.setCellValue(date);
                }
                endCreateCell(cellStyle);
            }

            public void handle(BooleanTextValue textValue) {
                HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
                if (textValue.getValue() == null) {
                    cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                } else {
                    cell.setCellValue(textValue.getValue().booleanValue());
                }
                endCreateCell(cellStyle);
            }

        });
    } else {
        HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
        if (JRCommonText.MARKUP_NONE.equals(textElement.getMarkup())) {
            setStringCellValue(textStr);
        } else {
            setRichTextStringCellValue(styledText, forecolor, textElement, getTextLocale(textElement));
        }
        endCreateCell(cellStyle);
    }

    if (!configuration.isIgnoreAnchors()) {
        String anchorName = textElement.getAnchorName();
        if (anchorName != null) {
            HSSFName aName = workbook.createName();
            aName.setNameName(JRStringUtil.getJavaIdentifier(anchorName));
            aName.setSheetIndex(workbook.getSheetIndex(sheet));
            CellReference cRef = new CellReference(rowIndex, colIndex, true, true);
            aName.setRefersToFormula(cRef.formatAsString());
            anchorNames.put(anchorName, aName);
        }
    }

    setHyperlinkCell(textElement);
}

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

License:Open Source License

protected void exportText(final JRPrintText textElement) throws JRException {
    String currentColumnName = textElement.getPropertiesMap()
            .getProperty(JRXlsAbstractMetadataExporter.PROPERTY_COLUMN_NAME);
    if (currentColumnName != null && currentColumnName.length() > 0) {
        String currentColumnData = textElement.getPropertiesMap()
                .getProperty(JRXlsAbstractMetadataExporter.PROPERTY_DATA);
        boolean repeatValue = getPropertiesUtil().getBooleanProperty(textElement,
                JRXlsAbstractMetadataExporter.PROPERTY_REPEAT_VALUE, false);

        setColumnName(currentColumnName);
        adjustColumnWidth(currentColumnName, textElement.getWidth(),
                ((JRXlsExporterNature) nature).getColumnAutoFit(textElement));
        adjustRowHeight(textElement.getHeight(),
                isWrapText(textElement) || ((JRXlsExporterNature) nature).getRowAutoFit(textElement));

        final short forecolor = getWorkbookColor(textElement.getForecolor()).getIndex();

        TextAlignHolder textAlignHolder = getTextAlignHolder(textElement);
        short horizontalAlignment = getHorizontalAlignment(textAlignHolder);
        short verticalAlignment = getVerticalAlignment(textAlignHolder);
        short rotation = getRotation(textAlignHolder);

        XlsReportConfiguration configuration = getCurrentItemConfiguration();

        short mode = backgroundMode;
        short backcolor = whiteIndex;
        if (!configuration.isIgnoreCellBackground() && textElement.getBackcolor() != null) {
            mode = HSSFCellStyle.SOLID_FOREGROUND;
            backcolor = getWorkbookColor(textElement.getBackcolor()).getIndex();
        }// ww w .j a v  a 2 s . co  m

        final StyleInfo baseStyle = new StyleInfo(mode, backcolor, horizontalAlignment, verticalAlignment,
                rotation, getLoadedFont(textElement, forecolor, null, getTextLocale(textElement)),
                new BoxStyle(textElement),
                isWrapText(textElement) || ((JRXlsExporterNature) nature).getColumnAutoFit(textElement),
                isCellLocked(textElement), isCellHidden(textElement));

        final JRStyledText styledText;
        final String textStr;
        final String formula;
        final CellSettings cellSettings = new CellSettings();
        if (currentColumnData != null) {
            styledText = new JRStyledText();
            styledText.append(currentColumnData);
            textStr = currentColumnData;
            formula = null;
        } else {
            styledText = getStyledText(textElement);
            if (styledText != null) {
                textStr = styledText.getText();
            } else {
                textStr = null;
            }
            formula = getFormula(textElement);
        }

        if (formula != null) {
            TextValue value = getTextValue(textElement, textStr);

            if (value instanceof NumberTextValue) {
                String convertedPattern = getConvertedPattern(textElement,
                        ((NumberTextValue) value).getPattern());
                if (convertedPattern != null) {
                    baseStyle.setDataFormat(dataFormat.getFormat(convertedPattern));
                }
            } else if (value instanceof DateTextValue) {
                String convertedPattern = getConvertedPattern(textElement,
                        ((DateTextValue) value).getPattern());
                if (convertedPattern != null) {
                    baseStyle.setDataFormat(dataFormat.getFormat(convertedPattern));
                }
            }

            cellSettings.importValues(HSSFCell.CELL_TYPE_FORMULA, getLoadedCellStyle(baseStyle), null, formula);

        } else if (getCurrentItemConfiguration().isDetectCellType()) {
            TextValue value = getTextValue(textElement, textStr);
            value.handle(new TextValueHandler() {
                public void handle(StringTextValue textValue) {
                    if (JRCommonText.MARKUP_NONE.equals(textElement.getMarkup())) {
                        cellSettings.importValues(HSSFCell.CELL_TYPE_STRING, getLoadedCellStyle(baseStyle),
                                new HSSFRichTextString(textValue.getText()));
                    } else {
                        cellSettings.importValues(HSSFCell.CELL_TYPE_STRING, getLoadedCellStyle(baseStyle),
                                getRichTextString(styledText, forecolor, textElement,
                                        getTextLocale(textElement)));
                    }
                }

                public void handle(NumberTextValue textValue) {
                    String convertedPattern = getConvertedPattern(textElement, textValue.getPattern());
                    if (convertedPattern != null) {
                        baseStyle.setDataFormat(dataFormat.getFormat(convertedPattern));
                    }
                    cellSettings.importValues(HSSFCell.CELL_TYPE_NUMERIC, getLoadedCellStyle(baseStyle),
                            textValue.getValue());
                }

                public void handle(DateTextValue textValue) {
                    String convertedPattern = getConvertedPattern(textElement, textValue.getPattern());
                    if (convertedPattern != null) {
                        baseStyle.setDataFormat(dataFormat.getFormat(convertedPattern));
                    }
                    Date value = textValue.getValue() == null ? null
                            : translateDateValue(textElement, textValue.getValue());
                    cellSettings.importValues(HSSFCell.CELL_TYPE_NUMERIC, getLoadedCellStyle(baseStyle), value);
                }

                public void handle(BooleanTextValue textValue) {
                    cellSettings.importValues(HSSFCell.CELL_TYPE_BOOLEAN, getLoadedCellStyle(baseStyle),
                            textValue.getValue());
                }

            });
        } else {
            if (JRCommonText.MARKUP_NONE.equals(textElement.getMarkup())) {
                cellSettings.importValues(HSSFCell.CELL_TYPE_STRING, getLoadedCellStyle(baseStyle),
                        new HSSFRichTextString(textStr));
            } else {
                cellSettings.importValues(HSSFCell.CELL_TYPE_STRING, getLoadedCellStyle(baseStyle),
                        getRichTextString(styledText, forecolor, textElement, getTextLocale(textElement)));
            }
        }

        if (!configuration.isIgnoreAnchors()) {
            String anchorName = textElement.getAnchorName();
            if (anchorName != null) {
                HSSFName aName = workbook.createName();
                aName.setNameName(JRStringUtil.getJavaIdentifier(anchorName));
                aName.setSheetIndex(workbook.getSheetIndex(sheet));
                CellReference cRef = new CellReference(rowIndex, columnNamesMap.get(currentColumnName), true,
                        true);
                aName.setRefersToFormula(cRef.formatAsString());
                anchorNames.put(anchorName, aName);
            }
        }

        setHyperlinkCell(textElement, cellSettings);
        addTextElement(cellSettings, textStr, repeatValue, currentColumnName);
    }
}