Example usage for org.apache.poi.hssf.usermodel HSSFCellStyle getAlignment

List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle getAlignment

Introduction

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

Prototype

@Override
    public HorizontalAlignment getAlignment() 

Source Link

Usage

From source file:com.wangzhu.poi.ExcelToHtmlConverter.java

License:Apache License

protected boolean processCell(HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx,
        float normalHeightPt) {
    final HSSFCellStyle cellStyle = cell.getCellStyle();

    String value;//ww w  .j  a  va  2 s .  c  o m
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        // XXX: enrich
        value = cell.getRichStringCellValue().getString();
        break;
    case Cell.CELL_TYPE_FORMULA:
        switch (cell.getCachedFormulaResultType()) {
        case Cell.CELL_TYPE_STRING:
            HSSFRichTextString str = cell.getRichStringCellValue();
            if ((str != null) && (str.length() > 0)) {
                value = (str.toString());
            } else {
                value = ExcelToHtmlUtils.EMPTY;
            }
            break;
        case Cell.CELL_TYPE_NUMERIC:
            HSSFCellStyle style = cellStyle;
            if (style == null) {
                value = String.valueOf(cell.getNumericCellValue());
            } else {
                value = (this._formatter.formatRawCellContents(cell.getNumericCellValue(),
                        style.getDataFormat(), style.getDataFormatString()));
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            value = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            value = ErrorEval.getText(cell.getErrorCellValue());
            break;
        default:
            ExcelToHtmlConverter.logger.log(POILogger.WARN,
                    "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultType() + ")");
            value = ExcelToHtmlUtils.EMPTY;
            break;
        }
        break;
    case Cell.CELL_TYPE_BLANK:
        value = ExcelToHtmlUtils.EMPTY;
        break;
    case Cell.CELL_TYPE_NUMERIC:
        value = this._formatter.formatCellValue(cell);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        value = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        value = ErrorEval.getText(cell.getErrorCellValue());
        break;
    default:
        ExcelToHtmlConverter.logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellType() + ")");
        return true;
    }

    final boolean noText = ExcelToHtmlUtils.isEmpty(value);
    final boolean wrapInDivs = !noText && this.isUseDivsToSpan() && !cellStyle.getWrapText();

    final short cellStyleIndex = cellStyle.getIndex();
    if (cellStyleIndex != 0) {
        HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook();
        String mainCssClass = this.getStyleClassName(workbook, cellStyle);
        if (wrapInDivs) {
            tableCellElement.setAttribute("class", mainCssClass + " " + this.cssClassContainerCell);
        } else {
            tableCellElement.setAttribute("class", mainCssClass);
        }

        if (noText) {
            /*
             * if cell style is defined (like borders, etc.) but cell text
             * is empty, add " " to output, so browser won't collapse
             * and ignore cell
             */
            value = "\u00A0";
        }
    }

    if (this.isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) {
        StringBuffer builder = new StringBuffer();
        for (int c = 0; c < value.length(); c++) {
            if (value.charAt(c) != ' ') {
                break;
            }
            builder.append('\u00a0');
        }

        if (value.length() != builder.length()) {
            builder.append(value.substring(builder.length()));
        }

        value = builder.toString();
    }

    Text text = this.htmlDocumentFacade.createText(value);

    if (wrapInDivs) {
        Element outerDiv = this.htmlDocumentFacade.createBlock();
        outerDiv.setAttribute("class", this.cssClassContainerDiv);

        Element innerDiv = this.htmlDocumentFacade.createBlock();
        StringBuffer innerDivStyle = new StringBuffer();
        innerDivStyle.append("position:absolute;min-width:");
        innerDivStyle.append(normalWidthPx);
        innerDivStyle.append("px;");
        if (maxSpannedWidthPx != Integer.MAX_VALUE) {
            innerDivStyle.append("max-width:");
            innerDivStyle.append(maxSpannedWidthPx);
            innerDivStyle.append("px;");
        }
        innerDivStyle.append("overflow:hidden;max-height:");
        innerDivStyle.append(normalHeightPt);
        innerDivStyle.append("pt;white-space:nowrap;");
        ExcelToHtmlUtils.appendAlign(innerDivStyle, cellStyle.getAlignment());
        this.htmlDocumentFacade.addStyleClass(outerDiv, this.cssClassPrefixDiv, innerDivStyle.toString());

        innerDiv.appendChild(text);
        outerDiv.appendChild(innerDiv);
        tableCellElement.appendChild(outerDiv);
    } else {
        tableCellElement.appendChild(text);
    }

    return ExcelToHtmlUtils.isEmpty(value) && (cellStyleIndex == 0);
}

From source file:egovframework.rte.fdl.excel.EgovExcelSXSSFServiceTest.java

License:Apache License

/**
 * [Flow #-6]  ?  :  ? ?(?, ? )? /*from   w  w w .j  a  v  a  2s.  c o  m*/
 */
@Test
public void testModifyCellAttribute() throws Exception {

    try {
        log.debug("testModifyCellAttribute start....");

        StringBuffer sb = new StringBuffer();
        sb.append(fileLocation).append("/").append("testModifyCellAttribute.xls");

        if (EgovFileUtil.isExistsFile(sb.toString())) {
            EgovFileUtil.delete(new File(sb.toString()));

            log.debug("Delete file...." + sb.toString());
        }

        HSSFWorkbook wbTmp = new HSSFWorkbook();
        wbTmp.createSheet();

        //  ? ?
        excelService.createWorkbook(wbTmp, sb.toString());

        //  ? 
        HSSFWorkbook wb = excelService.loadWorkbook(sb.toString());
        log.debug("testModifyCellAttribute after loadWorkbook....");

        HSSFSheet sheet = wb.createSheet("cell test sheet2");
        //           sheet.setColumnWidth((short) 3, (short) 200);   // column Width

        HSSFCellStyle cs = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setFontHeight((short) 16);
        font.setBoldweight((short) 3);
        font.setFontName("fixedsys");

        cs.setFont(font);
        cs.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // cell 
        cs.setWrapText(true);

        for (int i = 0; i < 100; i++) {
            HSSFRow row = sheet.createRow(i);
            //              row.setHeight((short)300); // row? height 

            for (int j = 0; j < 5; j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellValue(new HSSFRichTextString("row " + i + ", cell " + j));
                cell.setCellStyle(cs);
            }
        }

        //  ? 
        FileOutputStream out = new FileOutputStream(sb.toString());
        wb.write(out);
        out.close();

        //////////////////////////////////////////////////////////////////////////
        // ?
        HSSFWorkbook wbT = excelService.loadWorkbook(sb.toString());
        HSSFSheet sheetT = wbT.getSheet("cell test sheet2");
        log.debug("getNumCellStyles : " + wbT.getNumCellStyles());

        HSSFCellStyle cs1 = wbT.getCellStyleAt((short) (wbT.getNumCellStyles() - 1));

        HSSFFont fontT = cs1.getFont(wbT);
        log.debug("font getFontHeight : " + fontT.getFontHeight());
        log.debug("font getBoldweight : " + fontT.getBoldweight());
        log.debug("font getFontName : " + fontT.getFontName());
        log.debug("getAlignment : " + cs1.getAlignment());
        log.debug("getWrapText : " + cs1.getWrapText());

        for (int i = 0; i < 100; i++) {
            HSSFRow row1 = sheetT.getRow(i);
            for (int j = 0; j < 5; j++) {
                HSSFCell cell1 = row1.getCell(j);
                log.debug("row " + i + ", cell " + j + " : " + cell1.getRichStringCellValue());
                assertEquals(16, fontT.getFontHeight());
                assertEquals(3, fontT.getBoldweight());
                assertEquals(HSSFCellStyle.ALIGN_RIGHT, cs1.getAlignment());
                assertTrue(cs1.getWrapText());
            }
        }

    } catch (Exception e) {
        log.error(e.toString());
        throw new Exception(e);
    } finally {
        log.debug("testModifyCellAttribute end....");
    }
}

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

/**
 * (Each Excel sheet cell becomes an HTML table cell) Generates an HTML
 * table cell which has the same font styles, alignments, colours and
 * borders as the Excel cell./* ww w . j  av  a2  s .  co m*/
 *
 * @param cell
 *            The Excel cell.
 */

private void td(final HSSFCell cell) {
    int colspan = 1;
    if (colIndex == mergeStart) {
        // First cell in the merging region - set colspan.
        colspan = mergeEnd - mergeStart + 1;
    } else if (colIndex == mergeEnd) {
        // Last cell in the merging region - no more skipped cells.
        mergeStart = -1;
        mergeEnd = -1;
        return;
    } else if (mergeStart != -1 && mergeEnd != -1 && colIndex > mergeStart && colIndex < mergeEnd) {
        // Within the merging region - skip the cell.
        return;
    }
    //KLO 05022018
    //out.append("<td ");

    out.append("<td height=\"15\" ");
    if (colspan > 1) {
        out.append("colspan='").append(colspan).append("' ");
    }
    if (cell == null) {
        out.append("/>\n");
        return;
    }

    out.append("style='");
    final HSSFCellStyle style = cell.getCellStyle();
    // Text alignment
    switch (style.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        out.append("text-align: left; ");
        break;
    case CellStyle.ALIGN_RIGHT:
        out.append("text-align: right; ");
        break;
    case CellStyle.ALIGN_CENTER:
        out.append("text-align: center; ");
        break;
    default:
        break;
    }

    // Font style, size and weight
    final HSSFFont font = style.getFont(book);
    if (font == null)
        return;
    if (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) {
        out.append("font-weight: bold; ");
    }
    if (font.getItalic()) {
        out.append("font-style: italic; ");
    }
    if (font.getUnderline() != HSSFFont.U_NONE) {
        out.append("text-decoration: underline; ");
    }
    out.append("font-size: ").append(Math.floor(font.getFontHeightInPoints() * 0.8)).append("pt; ");
    // Cell background and font colours
    final short[] backRGB = style.getFillForegroundColorColor().getTriplet();
    final HSSFColor foreColor = palette.getColor(font.getColor());
    if (foreColor != null) {
        final short[] foreRGB = foreColor.getTriplet();
        if (foreRGB[0] != 0 || foreRGB[1] != 0 || foreRGB[2] != 0) {
            out.append("color: rgb(").append(foreRGB[0]).append(',').append(foreRGB[1]).append(',')
                    .append(foreRGB[2]).append(");");
        }
    }
    if (backRGB[0] != 0 || backRGB[1] != 0 || backRGB[2] != 0) {
        out.append("background-color: rgb(").append(backRGB[0]).append(',').append(backRGB[1]).append(',')
                .append(backRGB[2]).append(");");
    }
    // Border
    if (style.getBorderTop() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-top-style: solid; ");
    }
    if (style.getBorderRight() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-right-style: solid; ");
    }
    if (style.getBorderBottom() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-bottom-style: solid; ");
    }
    if (style.getBorderLeft() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-left-style: solid; ");
    }
    out.append("'>");
    String val = "";
    try {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            val = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            // POI does not distinguish between integer and double, thus:
            final double original = cell.getNumericCellValue(), rounded = Math.round(original);
            if (Math.abs(rounded - original) < 0.00000000000000001) {
                val = String.valueOf((int) rounded);
            } else {
                val = String.valueOf(original);
            }
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            final CellValue cv = evaluator.evaluate(cell);
            if (cv == null)
                return;
            switch (cv.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                out.append(cv.getBooleanValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                out.append(cv.getNumberValue());
                break;
            case Cell.CELL_TYPE_STRING:
                out.append(cv.getStringValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            case Cell.CELL_TYPE_ERROR:
                break;
            default:
                break;
            }
            break;
        default:
            // Neither string or number? Could be a date.
            try {
                val = sdf.format(cell.getDateCellValue());
            } catch (final Exception e1) {
            }
        }
    } catch (final Exception e) {
        val = e.getMessage();
    }
    if ("null".equals(val)) {
        val = "";
    }
    if (pix.containsKey(rowIndex)) {
        if (pix.get(rowIndex).containsKey(colIndex)) {
            for (final HSSFPictureData pic : pix.get(rowIndex).get(colIndex)) {
                out.append("<img alt='Image in Excel sheet' src='data:");
                out.append(pic.getMimeType());
                out.append(";base64,");
                try {
                    out.append(new String(Base64.encodeBase64(pic.getData()), "US-ASCII"));
                } catch (final UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                out.append("'/>");
            }
        }
    }

    if (isCode(val) && this.URL != null) {
        val = getHyperLink(val);
    }

    out.append(val);
    out.append("</td>\n");
}

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

private void td(final HSSFCell cell, StringBuffer buf) {
    int colspan = 1;
    if (colIndex == mergeStart) {
        // First cell in the merging region - set colspan.
        colspan = mergeEnd - mergeStart + 1;
    } else if (colIndex == mergeEnd) {
        // Last cell in the merging region - no more skipped cells.
        mergeStart = -1;//from   w w w .j  a  v a2s  .co  m
        mergeEnd = -1;
        return;
    } else if (mergeStart != -1 && mergeEnd != -1 && colIndex > mergeStart && colIndex < mergeEnd) {
        // Within the merging region - skip the cell.
        return;
    }

    //KLO 05022018
    //buf.append("<td ");

    buf.append("<td height=\"15\" ");

    if (colspan > 1) {
        buf.append("colspan='").append(colspan).append("' ");
    }
    if (cell == null) {
        buf.append("/>");
        return;
    }
    buf.append("style='");
    final HSSFCellStyle style = cell.getCellStyle();
    // Text alignment
    switch (style.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        buf.append("text-align: left; ");
        break;
    case CellStyle.ALIGN_RIGHT:
        buf.append("text-align: right; ");
        break;
    case CellStyle.ALIGN_CENTER:
        buf.append("text-align: center; ");
        break;
    default:
        break;
    }
    // Font style, size and weight
    final HSSFFont font = style.getFont(book);
    if (font == null)
        return;
    if (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) {
        buf.append("font-weight: bold; ");
    }
    if (font.getItalic()) {
        buf.append("font-style: italic; ");
    }
    if (font.getUnderline() != HSSFFont.U_NONE) {
        buf.append("text-decoration: underline; ");
    }
    buf.append("font-size: ").append(Math.floor(font.getFontHeightInPoints() * 0.8)).append("pt; ");

    // Cell background and font colours
    final short[] backRGB = style.getFillForegroundColorColor().getTriplet();
    final HSSFColor foreColor = palette.getColor(font.getColor());
    if (foreColor != null) {
        final short[] foreRGB = foreColor.getTriplet();
        if (foreRGB[0] != 0 || foreRGB[1] != 0 || foreRGB[2] != 0) {
            buf.append("color: rgb(").append(foreRGB[0]).append(',').append(foreRGB[1]).append(',')
                    .append(foreRGB[2]).append(");");

        }
    }
    if (backRGB[0] != 0 || backRGB[1] != 0 || backRGB[2] != 0) {
        buf.append("background-color: rgb(").append(backRGB[0]).append(',').append(backRGB[1]).append(',')
                .append(backRGB[2]).append(");");

    }
    // Border
    if (style.getBorderTop() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-top-style: solid; ");
    }
    if (style.getBorderRight() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-right-style: solid; ");
    }
    if (style.getBorderBottom() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-bottom-style: solid; ");
    }
    if (style.getBorderLeft() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-left-style: solid; ");
    }
    buf.append("'>");
    String val = "";
    try {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            val = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            // POI does not distinguish between integer and double, thus:
            final double original = cell.getNumericCellValue(), rounded = Math.round(original);
            if (Math.abs(rounded - original) < 0.00000000000000001) {
                val = String.valueOf((int) rounded);
            } else {
                val = String.valueOf(original);
            }
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            final CellValue cv = evaluator.evaluate(cell);
            if (cv == null)
                return;
            switch (cv.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                buf.append(cv.getBooleanValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                buf.append(cv.getNumberValue());
                break;
            case Cell.CELL_TYPE_STRING:
                buf.append(cv.getStringValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            case Cell.CELL_TYPE_ERROR:
                break;
            default:
                break;
            }
            break;
        default:
            // Neither string or number? Could be a date.
            try {
                val = sdf.format(cell.getDateCellValue());
            } catch (final Exception e1) {
            }
        }
    } catch (final Exception e) {
        val = e.getMessage();
    }
    if ("null".equals(val)) {
        val = "";
    }
    if (pix.containsKey(rowIndex)) {
        if (pix.get(rowIndex).containsKey(colIndex)) {
            for (final HSSFPictureData pic : pix.get(rowIndex).get(colIndex)) {
                buf.append("<img alt='Image in Excel sheet' src='data:");
                buf.append(pic.getMimeType());
                buf.append(";base64,");

                try {
                    buf.append(new String(Base64.encodeBase64(pic.getData()), "US-ASCII"));

                } catch (final UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                buf.append("'/>");
            }
        }
    }

    if (isCode(val) && this.URL != null) {
        val = getHyperLink(val);
    }
    buf.append(val);
    buf.append("</td>");

}

From source file:org.apache.cocoon.generation.HSSFGenerator.java

License:Apache License

/**
 * Writes out the workbook data as XML, with formatting information
 *//*  w ww  . j a v a2s  . co m*/
private void writeStyles(HSSFWorkbook workbook, HSSFSheet sheet) throws SAXException {
    start("Styles");
    HSSFRow row = null;
    HSSFCell cell = null;
    Iterator cells = null;
    Iterator rows = sheet.rowIterator();
    while (rows.hasNext()) {
        row = (HSSFRow) rows.next();
        cells = row.cellIterator();
        while (cells.hasNext()) {
            cell = (HSSFCell) cells.next();
            attribute("startRow", Integer.toString(row.getRowNum()));
            attribute("endRow", Integer.toString(row.getRowNum()));
            attribute("startCol", Short.toString(cell.getCellNum()));
            attribute("endCol", Short.toString(cell.getCellNum()));
            start("StyleRegion");
            HSSFCellStyle style = cell.getCellStyle();
            attribute("HAlign", Integer.toString(style.getAlignment()));
            attribute("VAlign", Integer.toString(style.getVerticalAlignment()));
            attribute("WrapText", ((style.getWrapText()) ? "1" : "0"));
            attribute("Orient", Integer.toString(style.getRotation()));
            attribute("Indent", Integer.toString(style.getIndention()));
            attribute("Locked", ((style.getLocked()) ? "1" : "0"));
            attribute("Hidden", ((style.getHidden()) ? "1" : "0"));
            attribute("Fore",
                    workbook.getCustomPalette().getColor(style.getFillForegroundColor()).getHexString());
            attribute("Back",
                    workbook.getCustomPalette().getColor(style.getFillBackgroundColor()).getHexString());
            attribute("PatternColor", Integer.toString(style.getFillPattern())); // TODO
            attribute("Format", "General"); // TODO
            start("Style");
            HSSFFont font = workbook.getFontAt(style.getFontIndex());
            attribute("Unit", Short.toString(font.getFontHeightInPoints()));
            attribute("Bold", Short.toString(font.getBoldweight()));
            attribute("Italic", ((font.getItalic()) ? "1" : "0"));
            attribute("Unterline", Integer.toString(font.getUnderline()));
            attribute("StrikeThrough", ((font.getStrikeout()) ? "1" : "0"));
            start("Font");
            data(font.getFontName());
            end("Font");
            end("Style");
            end("StyleRegion");
        }
    }
    end("Styles");
}

From source file:org.jxstar.report.util.XlsToHtml.java

/**
 * ???//from   www.  j  a  v  a 2s .  c o m
 * @param style -- xls?
 * @return
 */
private String getAlignStyle(HSSFCellStyle style) {
    //???
    short align = style.getAlignment();
    String strAlign = "text-align:left;";
    if (align == CellStyle.ALIGN_CENTER) {
        strAlign = "text-align:center;";
    } else if (align == CellStyle.ALIGN_RIGHT) {
        strAlign = "text-align:right;";
    }

    //???
    short valign = style.getVerticalAlignment();
    String strVAlign = "vertical-align:bottom;";
    if (valign == CellStyle.VERTICAL_CENTER) {
        strVAlign = "vertical-align:middle;";
    } else if (valign == CellStyle.VERTICAL_TOP) {
        strVAlign = "vertical-align:top;";
    }

    return strAlign + strVAlign;
}

From source file:org.orbeon.oxf.util.XLSUtils.java

License:Open Source License

public static void copyCell(HSSFWorkbook workbook, HSSFCell destination, HSSFCell source) {

    // Copy cell content
    destination.setCellType(source.getCellType());
    switch (source.getCellType()) {
    case HSSFCell.CELL_TYPE_BOOLEAN:
        destination.setCellValue(source.getBooleanCellValue());
        break;/*  w  w  w  . j  av  a  2 s  .  co m*/
    case HSSFCell.CELL_TYPE_FORMULA:
    case HSSFCell.CELL_TYPE_STRING:
        destination.setCellValue(source.getStringCellValue());
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        destination.setCellValue(source.getNumericCellValue());
        break;
    }

    // Copy cell style
    HSSFCellStyle sourceCellStyle = source.getCellStyle();
    HSSFCellStyle destinationCellStyle = workbook.createCellStyle();
    destinationCellStyle.setAlignment(sourceCellStyle.getAlignment());
    destinationCellStyle.setBorderBottom(sourceCellStyle.getBorderBottom());
    destinationCellStyle.setBorderLeft(sourceCellStyle.getBorderLeft());
    destinationCellStyle.setBorderRight(sourceCellStyle.getBorderRight());
    destinationCellStyle.setBorderTop(sourceCellStyle.getBorderTop());
    destinationCellStyle.setBottomBorderColor(sourceCellStyle.getBottomBorderColor());
    destinationCellStyle.setDataFormat(sourceCellStyle.getDataFormat());
    destinationCellStyle.setFillBackgroundColor(sourceCellStyle.getFillForegroundColor());
    destinationCellStyle.setFillForegroundColor(sourceCellStyle.getFillForegroundColor());
    destinationCellStyle.setFillPattern(sourceCellStyle.getFillPattern());
    destinationCellStyle.setFont(workbook.getFontAt(sourceCellStyle.getFontIndex()));
    destinationCellStyle.setHidden(sourceCellStyle.getHidden());
    destinationCellStyle.setIndention(sourceCellStyle.getIndention());
    destinationCellStyle.setLeftBorderColor(sourceCellStyle.getLeftBorderColor());
    destinationCellStyle.setLocked(sourceCellStyle.getLocked());
    destinationCellStyle.setRightBorderColor(sourceCellStyle.getRightBorderColor());
    destinationCellStyle.setRotation(sourceCellStyle.getRotation());
    destinationCellStyle.setTopBorderColor(sourceCellStyle.getTopBorderColor());
    destinationCellStyle.setVerticalAlignment(sourceCellStyle.getVerticalAlignment());
    destinationCellStyle.setWrapText(sourceCellStyle.getWrapText());
    destination.setCellStyle(destinationCellStyle);
}

From source file:poi.hssf.view.SVTableCellEditor.java

License:Apache License

/**
 *  Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
 *
 * @return             The tableCellEditorComponent value
 */// w  ww. java2 s  .c o m
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
        int column) {
    System.out.println("GetTableCellEditorComponent");
    HSSFCell cell = (HSSFCell) value;
    if (cell != null) {
        HSSFCellStyle style = cell.getCellStyle();
        HSSFFont f = wb.getFontAt(style.getFontIndex());
        boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
        boolean isitalics = f.getItalic();

        int fontstyle = Font.PLAIN;

        if (isbold)
            fontstyle = Font.BOLD;
        if (isitalics)
            fontstyle = fontstyle | Font.ITALIC;

        int fontheight = f.getFontHeightInPoints();
        if (fontheight == 9)
            fontheight = 10; //fix for stupid ol Windows

        Font font = new Font(f.getFontName(), fontstyle, fontheight);
        editor.setFont(font);

        if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
            editor.setBackground(getAWTColor(style.getFillForegroundColor(), white));
        } else
            editor.setBackground(white);

        editor.setForeground(getAWTColor(f.getColor(), black));

        //Set the value that is rendered for the cell
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_BLANK:
            editor.setText("");
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            if (cell.getBooleanCellValue()) {
                editor.setText("true");
            } else {
                editor.setText("false");
            }
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            editor.setText(Double.toString(cell.getNumericCellValue()));
            break;
        case HSSFCell.CELL_TYPE_STRING:
            editor.setText(cell.getRichStringCellValue().getString());
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
        default:
            editor.setText("?");
        }
        switch (style.getAlignment()) {
        case HSSFCellStyle.ALIGN_LEFT:
        case HSSFCellStyle.ALIGN_JUSTIFY:
        case HSSFCellStyle.ALIGN_FILL:
            editor.setHorizontalAlignment(SwingConstants.LEFT);
            break;
        case HSSFCellStyle.ALIGN_CENTER:
        case HSSFCellStyle.ALIGN_CENTER_SELECTION:
            editor.setHorizontalAlignment(SwingConstants.CENTER);
            break;
        case HSSFCellStyle.ALIGN_GENERAL:
        case HSSFCellStyle.ALIGN_RIGHT:
            editor.setHorizontalAlignment(SwingConstants.RIGHT);
            break;
        default:
            editor.setHorizontalAlignment(SwingConstants.LEFT);
            break;
        }

    }
    return editor;
}

From source file:poi.hssf.view.SVTableCellRenderer.java

License:Apache License

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    boolean isBorderSet = false;

    //If the JTables default cell renderer has been setup correctly the
    //value will be the HSSFCell that we are trying to render
    HSSFCell c = (HSSFCell) value;// w w w.ja v  a 2 s  .  c  om

    if (c != null) {
        HSSFCellStyle s = c.getCellStyle();
        HSSFFont f = wb.getFontAt(s.getFontIndex());
        setFont(SVTableUtils.makeFont(f));

        if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
            setBackground(SVTableUtils.getAWTColor(s.getFillForegroundColor(), SVTableUtils.white));
        } else
            setBackground(SVTableUtils.white);

        setForeground(SVTableUtils.getAWTColor(f.getColor(), SVTableUtils.black));

        cellBorder.setBorder(SVTableUtils.getAWTColor(s.getTopBorderColor(), SVTableUtils.black),
                SVTableUtils.getAWTColor(s.getRightBorderColor(), SVTableUtils.black),
                SVTableUtils.getAWTColor(s.getBottomBorderColor(), SVTableUtils.black),
                SVTableUtils.getAWTColor(s.getLeftBorderColor(), SVTableUtils.black), s.getBorderTop(),
                s.getBorderRight(), s.getBorderBottom(), s.getBorderLeft(), hasFocus);
        setBorder(cellBorder);
        isBorderSet = true;

        //Set the value that is rendered for the cell
        switch (c.getCellType()) {
        case HSSFCell.CELL_TYPE_BLANK:
            setValue("");
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            if (c.getBooleanCellValue()) {
                setValue("true");
            } else {
                setValue("false");
            }
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            short format = s.getDataFormat();
            double numericValue = c.getNumericCellValue();
            if (cellFormatter.useRedColor(format, numericValue))
                setForeground(Color.red);
            else
                setForeground(null);
            setValue(cellFormatter.format(format, c.getNumericCellValue()));
            break;
        case HSSFCell.CELL_TYPE_STRING:
            setValue(c.getRichStringCellValue().getString());
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
        default:
            setValue("?");
        }
        //Set the text alignment of the cell
        switch (s.getAlignment()) {
        case HSSFCellStyle.ALIGN_LEFT:
        case HSSFCellStyle.ALIGN_JUSTIFY:
        case HSSFCellStyle.ALIGN_FILL:
            setHorizontalAlignment(SwingConstants.LEFT);
            break;
        case HSSFCellStyle.ALIGN_CENTER:
        case HSSFCellStyle.ALIGN_CENTER_SELECTION:
            setHorizontalAlignment(SwingConstants.CENTER);
            break;
        case HSSFCellStyle.ALIGN_GENERAL:
        case HSSFCellStyle.ALIGN_RIGHT:
            setHorizontalAlignment(SwingConstants.RIGHT);
            break;
        default:
            setHorizontalAlignment(SwingConstants.LEFT);
            break;
        }
    } else {
        setValue("");
        setBackground(SVTableUtils.white);
    }

    if (hasFocus) {
        if (!isBorderSet) {
            //This is the border to paint when there is no border
            //and the cell has focus
            cellBorder.setBorder(SVTableUtils.black, SVTableUtils.black, SVTableUtils.black, SVTableUtils.black,
                    HSSFCellStyle.BORDER_NONE, HSSFCellStyle.BORDER_NONE, HSSFCellStyle.BORDER_NONE,
                    HSSFCellStyle.BORDER_NONE, isSelected);
            setBorder(cellBorder);
        }
        if (table.isCellEditable(row, column)) {
            setForeground(UIManager.getColor("Table.focusCellForeground"));
            setBackground(UIManager.getColor("Table.focusCellBackground"));
        }
    } else if (!isBorderSet) {
        setBorder(noFocusBorder);
    }

    // ---- begin optimization to avoid painting background ----
    Color back = getBackground();
    boolean colorMatch = (back != null) && (back.equals(table.getBackground())) && table.isOpaque();
    setOpaque(!colorMatch);
    // ---- end optimization to aviod painting background ----
    return this;
}

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

License:BSD License

public int getAlignment() {
    HSSFCellStyle cellStyle = theCell.getCellStyle();
    if (cellStyle == null) {
        return SwingConstants.LEFT;
    }// w w  w .  jav  a 2 s.c  om
    short hssfAlignment = cellStyle.getAlignment();
    if (hssfAlignment == HSSFCellStyle.ALIGN_LEFT) {
        return SwingConstants.LEFT;
    } else if (hssfAlignment == HSSFCellStyle.ALIGN_CENTER) {
        return SwingConstants.CENTER;
    } else if (hssfAlignment == HSSFCellStyle.ALIGN_RIGHT) {
        return SwingConstants.RIGHT;
    } else {
        return SwingConstants.LEFT;
    }
}