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

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

Introduction

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

Prototype

public void setNameName(String nameName) 

Source Link

Document

Sets the name of the named range

The following is a list of syntax rules that you need to be aware of when you create and edit names.

  • Valid characters The first character of a name must be a letter, an underscore character (_), or a backslash (\).

    Usage

    From source file:com.wetts.base.utils.poi.excel.ExcelDao.java

    License:Open Source License

    /**
     * ??//from  w w  w .j a v  a2s  . c  o m
     *
     * @param wb
     * @param name
     * @param expression
     * @return
     */
    public static HSSFName createName(HSSFWorkbook wb, String name, String expression) {
        HSSFName refer = wb.getName(name);
        //?????
        if (refer == null) {
            refer = wb.createName();
            refer.setNameName(name);
        }
        //???
        refer.setRefersToFormula(expression);
        return refer;
    }
    

    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  w w . j  a  v  a2 s  .  c o  m
                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.  ja  va 2  s .  c  om
    
            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);
        }
    }
    

    From source file:org.opentestsystem.delivery.testreg.rest.view.ExcelView.java

    License:Open Source License

    @SuppressWarnings("unchecked")
    @Override//from www. j  av  a  2  s . c  om
    protected void buildExcelDocument(final Map<String, Object> model, final HSSFWorkbook workbook,
            final HttpServletRequest request, final HttpServletResponse response) {
        // get data model which is passed by the Spring container
        final List<TestRegistrationBase> entityList = (List<TestRegistrationBase>) model.get(DATA_LIST);
        final String formatType = ((String) model.get(FORMAT_TYPE)).toUpperCase();
        String exportType = null;
        if (formatType.equalsIgnoreCase(FormatType.STUDENT.name())) {
            exportType = ((String) model.get(EXPORT_TYPE)).toUpperCase();
            HSSFName name = workbook.createName();
            // display filename to return to the client
            name.setNameName(exportType);
        }
    
        final Sheet sheet = workbook.createSheet(formatType);
        sheet.setDefaultColumnWidth(30);
    
        String headerColumns[] = new String[100];
        if (formatType.equalsIgnoreCase(FormatType.STUDENT.name())) {
            // Based on export type for FormatType( STUDENT ) assigning headers for export file 
            if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.studentsPlusAccommodations.name())) {
                List<String> headerStudentList = templateDownloadMap.get(FormatType.STUDENT);
                String[] headerCodesTemp = headersMap.get(FormatType.DESIGNATEDSUPPORTSANDACCOMMODATIONS.name());
                List<String> headerCodes = Arrays.asList(headerCodesTemp);
                List<String> headerAccommodationList = headerCodes.subList(2, headerCodes.size());
                String lastElement = headerStudentList.get(headerStudentList.size() - 1);
                List<String> combinedHeaders = ListUtils
                        .union(headerStudentList.subList(0, headerStudentList.size() - 1), headerAccommodationList);
                combinedHeaders.add(lastElement);
                headerColumns = combinedHeaders.toArray(new String[combinedHeaders.size()]);
            } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.studentsOnly.name())) {
                List<String> headerStudentList = templateDownloadMap.get(FormatType.STUDENT);
                headerColumns = headerStudentList.toArray(new String[headerStudentList.size()]);
            } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.accommodationsOnly.name())) {
                headerColumns = headersMap.get(FormatType.DESIGNATEDSUPPORTSANDACCOMMODATIONS.name());
            } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.explicitEligibility.name())) {
                List<String> headerExplicitEligibilityList = templateDownloadMap
                        .get(FormatType.EXPLICITELIGIBILITY);
                headerColumns = headerExplicitEligibilityList
                        .toArray(new String[headerExplicitEligibilityList.size()]);
            }
        } else {
            headerColumns = this.headersMap.get(formatType);
        }
        final Row header = sheet.createRow(0);
        for (int i = 0; i < headerColumns.length; i++) {
            final Cell cell = header.createCell(i);
            cell.setCellValue(headerColumns[i]);
        }
        // DataRow Begin
        if (!CollectionUtils.isEmpty(entityList)) {
            int rowCount = 1;
            for (final TestRegistrationBase entity : entityList) {
                // if format is user then we need to customize export to include role associations as separate rows
                switch (FormatType.valueOf(formatType)) {
                case USER:
                    rowCount = exportUser(sheet, rowCount, entity);
                    break;
                case STUDENTGROUP:
                    rowCount = exportStudentGroup(sheet, rowCount, entity);
                    break;
                case STUDENT:
                    rowCount = exportStudent(sheet, rowCount, (Student) entity, exportType);
                    break;
                default:
                    final Row rowData = sheet.createRow(rowCount);
                    final String[] columnData = this.testRegistrationViewTransformer.transform(entity);
                    addData(rowData, 0, columnData.length, columnData);
                }
                rowCount++;
            }
        }
    }
    

    From source file:uk.ac.manchester.cs.owl.semspreadsheets.model.hssf.impl.WorkbookHSSFImpl.java

    License:BSD License

    public void addName(String name, Range rng) {
        if (workbook.getName(name) != null) {
            workbook.removeName(name);/*  w  w w. j  a v a 2  s  .c  om*/
        }
        HSSFName hssfName = workbook.createName();
        hssfName.setNameName(name);
        hssfName.setRefersToFormula(rng.toFixedAddress());
    }