Example usage for org.apache.poi.ss.usermodel CellStyle getAlignment

List of usage examples for org.apache.poi.ss.usermodel CellStyle getAlignment

Introduction

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

Prototype

HorizontalAlignment getAlignment();

Source Link

Document

get the type of horizontal alignment for the cell

Usage

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

License:Apache License

/**
 * [Flow #-3]  ? ?  :  ?? ?(? ?, Border? ?, ? ?,  )? 
 *///from ww  w  .  jav a2 s.  c o  m
@Test
public void testWriteExcelFileAttribute() throws Exception {

    try {
        LOGGER.debug("testWriteExcelFileAttribute start....");

        short rowheight = 40 * 10;
        int columnwidth = 30;

        StringBuffer sb = new StringBuffer();
        sb.append(fileLocation).append("/").append("testWriteExcelFileAttribute.xlsx");

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

            LOGGER.debug("Delete file....{}", sb.toString());
        }

        XSSFWorkbook wb = new XSSFWorkbook();

        XSSFSheet sheet1 = wb.createSheet("new sheet");
        wb.createSheet("second sheet");

        // ? ?
        sheet1.setDefaultRowHeight(rowheight);
        sheet1.setDefaultColumnWidth(columnwidth);

        Font f2 = wb.createFont();
        XSSFCellStyle cs = wb.createCellStyle();

        cs.setFont(f2);
        cs.setWrapText(true);

        // 
        cs.setAlignment(CellStyle.ALIGN_RIGHT);
        cs.setFillPattern(CellStyle.DIAMONDS); //  ?

        XSSFRow r1 = sheet1.createRow(0);
        r1.createCell(0);

        // ? ?
        cs.setFillForegroundColor(IndexedColors.BLUE.getIndex()); //  
        cs.setFillBackgroundColor(IndexedColors.RED.getIndex()); // 

        sheet1.setDefaultColumnStyle((short) 0, cs);

        Workbook tmp = excelService.createWorkbook(wb, sb.toString());

        Sheet sheetTmp1 = tmp.getSheetAt(0);

        assertEquals(rowheight, sheetTmp1.getDefaultRowHeight());
        assertEquals(columnwidth, sheetTmp1.getDefaultColumnWidth());

        CellStyle cs1 = tmp.getCellStyleAt((short) (tmp.getNumCellStyles() - 1));

        LOGGER.debug("getAlignment : {}", cs1.getAlignment());
        assertEquals(XSSFCellStyle.ALIGN_RIGHT, cs1.getAlignment());

        LOGGER.debug("getFillPattern : {}", cs1.getFillPattern());
        assertEquals(XSSFCellStyle.DIAMONDS, cs1.getFillPattern());

        LOGGER.debug("getFillForegroundColor : {}", cs1.getFillForegroundColor());
        LOGGER.debug("getFillBackgroundColor : {}", cs1.getFillBackgroundColor());

        LOGGER.debug(
                "XSSFWorkbook.getFillBackgroundColor(), XSSFColor().getIndexed() ? ? 0 ? ?");

        assertEquals(IndexedColors.BLUE.getIndex(), cs1.getFillForegroundColor());
        assertEquals(IndexedColors.RED.getIndex(), cs1.getFillBackgroundColor());

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

From source file:net.ceos.project.poi.annotated.core.CellStyleHandler.java

License:Apache License

/**
 * Clone a cell style passed as parameter.
 * //from   w w  w .  j  av  a2s. com
 * @param wb
 *            the {@link Workbook} in use
 * @param csBase
 *            the {@link CellStyle} base
 * @return the new cell style
 */
private static CellStyle cloneCellStyle(final Workbook wb, final CellStyle csBase) {
    CellStyle cs = cellStyleFactory.newInstance(wb);
    cs.setAlignment(csBase.getAlignment());
    cs.setVerticalAlignment(csBase.getVerticalAlignment());
    cs.setBorderTop(csBase.getBorderTop());
    cs.setBorderBottom(csBase.getBorderBottom());
    cs.setBorderLeft(csBase.getBorderLeft());
    cs.setBorderRight(csBase.getBorderRight());
    cs.setFillForegroundColor(csBase.getFillForegroundColor());
    cs.setFillPattern(csBase.getFillPattern());
    cs.setWrapText(csBase.getWrapText());

    cs.setFont(wb.getFontAt(csBase.getFontIndex()));
    return cs;
}

From source file:org.apache.metamodel.excel.ExcelUtils.java

License:Apache License

public static Style getCellStyle(Workbook workbook, Cell cell) {
    if (cell == null) {
        return Style.NO_STYLE;
    }/* ww  w. ja  v  a 2 s  .  co  m*/
    final CellStyle cellStyle = cell.getCellStyle();

    final short fontIndex = cellStyle.getFontIndex();
    final Font font = workbook.getFontAt(fontIndex);
    final StyleBuilder styleBuilder = new StyleBuilder();

    // Font bold, italic, underline
    if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) {
        styleBuilder.bold();
    }
    if (font.getItalic()) {
        styleBuilder.italic();
    }
    if (font.getUnderline() != FontUnderline.NONE.getByteValue()) {
        styleBuilder.underline();
    }

    // Font size
    final Font stdFont = workbook.getFontAt((short) 0);
    final short fontSize = font.getFontHeightInPoints();
    if (stdFont.getFontHeightInPoints() != fontSize) {
        styleBuilder.fontSize(fontSize, SizeUnit.PT);
    }

    // Font color
    final short colorIndex = font.getColor();
    if (font instanceof HSSFFont) {
        if (colorIndex != HSSFFont.COLOR_NORMAL) {
            final HSSFWorkbook wb = (HSSFWorkbook) workbook;
            HSSFColor color = wb.getCustomPalette().getColor(colorIndex);
            if (color != null) {
                short[] triplet = color.getTriplet();
                styleBuilder.foreground(triplet);
            }
        }
    } else if (font instanceof XSSFFont) {
        XSSFFont xssfFont = (XSSFFont) font;

        XSSFColor color = xssfFont.getXSSFColor();
        if (color != null) {
            String argbHex = color.getARGBHex();
            if (argbHex != null) {
                styleBuilder.foreground(argbHex.substring(2));
            }
        }
    } else {
        throw new IllegalStateException(
                "Unexpected font type: " + (font == null ? "null" : font.getClass()) + ")");
    }

    // Background color
    if (cellStyle.getFillPattern() == 1) {
        Color color = cellStyle.getFillForegroundColorColor();
        if (color instanceof HSSFColor) {
            short[] triplet = ((HSSFColor) color).getTriplet();
            if (triplet != null) {
                styleBuilder.background(triplet);
            }
        } else if (color instanceof XSSFColor) {
            String argb = ((XSSFColor) color).getARGBHex();
            if (argb != null) {
                styleBuilder.background(argb.substring(2));
            }
        } else {
            throw new IllegalStateException(
                    "Unexpected color type: " + (color == null ? "null" : color.getClass()) + ")");
        }
    }

    // alignment
    switch (cellStyle.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        styleBuilder.leftAligned();
        break;
    case CellStyle.ALIGN_RIGHT:
        styleBuilder.rightAligned();
        break;
    case CellStyle.ALIGN_CENTER:
        styleBuilder.centerAligned();
        break;
    case CellStyle.ALIGN_JUSTIFY:
        styleBuilder.justifyAligned();
        break;
    }

    return styleBuilder.create();
}

From source file:org.drugepi.table.CellStyleLookup.java

License:Mozilla Public License

public static String styleToString(CellStyle style) {
    StringBuffer sb = new StringBuffer();
    sb.append("getDataFormatString=" + style.getDataFormatString() + "\n");
    sb.append("getFontIndex=" + style.getFontIndex() + "\n");
    sb.append("getHidden=" + style.getHidden() + "\n");
    sb.append("getAlignment=" + style.getAlignment() + "\n");
    sb.append("getWrapText=" + style.getWrapText() + "\n");
    sb.append("getVerticalAlignment=" + style.getVerticalAlignment() + "\n");
    sb.append("getRotation=" + style.getRotation() + "\n");
    sb.append("getIndention=" + style.getIndention() + "\n");
    sb.append("getBorderLeft=" + style.getBorderLeft() + "\n");
    sb.append("getBorderRight=" + style.getBorderRight() + "\n");
    sb.append("getBorderTop=" + style.getBorderTop() + "\n");
    sb.append("getBorderBottom=" + style.getBorderBottom() + "\n");
    sb.append("getLeftBorderColor=" + style.getLeftBorderColor() + "\n");
    sb.append("getRightBorderColor=" + style.getRightBorderColor() + "\n");
    sb.append("getTopBorderColor=" + style.getTopBorderColor() + "\n");
    sb.append("getBottomBorderColor=" + style.getBottomBorderColor() + "\n");
    sb.append("getFillPattern=" + style.getFillPattern() + "\n");
    sb.append("getFillBackgroundColor=" + style.getFillBackgroundColor() + "\n");
    sb.append("getFillForegroundColor=" + style.getFillForegroundColor() + "\n");

    return sb.toString();
}

From source file:org.joeffice.spreadsheet.cell.CellRenderer.java

License:Apache License

public void decorateLabel(Cell cell, JLabel defaultRenderer) {

    // Text/*from  w  ww .ja  va  2  s.c o  m*/
    // String text = getFormattedText(cell);
    // XXX small bug with decimal not using the correct comma's
    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA && formulaEvaluator == null) {
        formulaEvaluator = cell.getRow().getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
    }
    String text = DATA_FORMATTER.formatCellValue(cell, formulaEvaluator);
    setText(text);

    decorateComponent(cell, this, defaultRenderer);

    // Alignment
    CellStyle style = cell.getCellStyle();
    short alignment = style.getAlignment();
    if (alignment == CellStyle.ALIGN_CENTER || cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        setHorizontalAlignment(SwingConstants.CENTER);
    } else if (alignment == CellStyle.ALIGN_RIGHT || cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        setHorizontalAlignment(SwingConstants.RIGHT);
    } else {
        setHorizontalAlignment(defaultRenderer.getHorizontalAlignment());
    }
    short verticalAlignment = style.getAlignment(); // Either LibreOffice 4 or POI has problem with vertical alignment
    if (verticalAlignment == CellStyle.VERTICAL_TOP) {
        setVerticalAlignment(SwingConstants.TOP);
    } else if (verticalAlignment == CellStyle.VERTICAL_CENTER) {
        setVerticalAlignment(SwingConstants.CENTER);
    } else if (verticalAlignment == CellStyle.VERTICAL_BOTTOM) {
        setVerticalAlignment(SwingConstants.BOTTOM);
    } else {
        setVerticalAlignment(defaultRenderer.getVerticalAlignment());
    }
}

From source file:org.netxilia.impexp.impl.PoiUtils.java

License:Open Source License

public static Styles poiStyle2Netxilia(CellStyle poiStyle, Font font, HSSFPalette palette,
        NetxiliaStyleResolver styleResolver) {
    List<Style> entries = new ArrayList<Style>();

    if (!poiStyle.getWrapText()) {
        entries.add(DefaultStyle.nowrap.getStyle());
    }//w  w  w . j  a  va 2s  . c  om
    // font
    if (font.getItalic()) {
        entries.add(DefaultStyle.italic.getStyle());
    }
    if (font.getStrikeout()) {
        entries.add(DefaultStyle.strikeout.getStyle());
    }
    if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) {
        entries.add(DefaultStyle.bold.getStyle());
    }
    if (font.getUnderline() != Font.U_NONE) {
        entries.add(DefaultStyle.underline.getStyle());
    }
    // borders
    if (poiStyle.getBorderBottom() != CellStyle.BORDER_NONE) {
        entries.add(DefaultStyle.borderBottom.getStyle());
    }
    if (poiStyle.getBorderLeft() != CellStyle.BORDER_NONE) {
        entries.add(DefaultStyle.borderLeft.getStyle());
    }
    if (poiStyle.getBorderTop() != CellStyle.BORDER_NONE) {
        entries.add(DefaultStyle.borderTop.getStyle());
    }
    if (poiStyle.getBorderRight() != CellStyle.BORDER_NONE) {
        entries.add(DefaultStyle.borderRight.getStyle());
    }
    // align
    switch (poiStyle.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        entries.add(DefaultStyle.alignLeft.getStyle());
        break;
    case CellStyle.ALIGN_RIGHT:
        entries.add(DefaultStyle.alignRight.getStyle());
        break;
    case CellStyle.ALIGN_CENTER:
        entries.add(DefaultStyle.alignCenter.getStyle());
        break;
    case CellStyle.ALIGN_JUSTIFY:
        entries.add(DefaultStyle.alignJustify.getStyle());
        break;
    }
    if (font != null && font.getColor() != 0) {
        HSSFColor poiForeground = palette.getColor(font.getColor());
        if (poiForeground != null && poiForeground != HSSFColor.AUTOMATIC.getInstance()) {
            Style foregroundDef = styleResolver.approximateForeground(poiForeground.getTriplet()[0],
                    poiForeground.getTriplet()[1], poiForeground.getTriplet()[2]);
            if (foregroundDef != null) {
                entries.add(foregroundDef);
            }
        }
    }

    if (poiStyle.getFillForegroundColor() != 0) {
        HSSFColor poiBackground = palette.getColor(poiStyle.getFillForegroundColor());
        if (poiBackground != null && poiBackground != HSSFColor.AUTOMATIC.getInstance()) {
            Style backgroundDef = styleResolver.approximateBackground(poiBackground.getTriplet()[0],
                    poiBackground.getTriplet()[1], poiBackground.getTriplet()[2]);
            if (backgroundDef != null) {
                entries.add(backgroundDef);
            }
        }
    }
    return entries.size() > 0 ? Styles.styles(entries) : null;
}

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

private void fillCellStyle(CStyle cellStyle, CellStyle excelCellStyle) {
    Font excelFont = workbook.getFontAt(excelCellStyle.getFontIndex());
    // TODO    CFont newFont(excelFont)
    //CFont font = new CFont();
    //cellStyle.setFont( font );
    CFont font = cellStyle.getFont();//from ww w .j a v  a2 s. c  om

    fillFont(font, excelFont);

    cellStyle.setHidden(excelCellStyle.getHidden());
    cellStyle.setLocked(excelCellStyle.getLocked());
    cellStyle.setWrapped(excelCellStyle.getWrapText());

    cellStyle.setIndention(excelCellStyle.getIndention());
    cellStyle.setRotation(excelCellStyle.getRotation());

    cellStyle.setHorzAlignment(this.getHorzAlignment(excelCellStyle.getAlignment()));
    cellStyle.setVertAlignment(this.getVertAlignment(excelCellStyle.getVerticalAlignment()));

    CBorder leftBorder = cellStyle.getLeftBorder();
    CBorder rightBorder = cellStyle.getRightBorder();
    CBorder topBorder = cellStyle.getTopBorder();
    CBorder bottomBorder = cellStyle.getBottomBorder();

    BorderType lbType = this.convertBorderType(excelCellStyle.getBorderLeft());
    BorderType rbType = this.convertBorderType(excelCellStyle.getBorderRight());
    BorderType tbType = this.convertBorderType(excelCellStyle.getBorderTop());
    BorderType bbType = this.convertBorderType(excelCellStyle.getBorderBottom());

    leftBorder.setType(lbType);
    rightBorder.setType(rbType);
    topBorder.setType(tbType);
    bottomBorder.setType(bbType);

    //   "Fill Background Color" ???,    ??,
    //   ? ??? .  ?  .
    //      "Fill Foreground Color"
    XSSFColor bgColor = (XSSFColor) excelCellStyle.getFillBackgroundColorColor();

    // ? Index   64,  ? ,         ,
    // ?  ?   null ? 
    if (null != bgColor && 64 != bgColor.getIndexed()) {
        String bgColorHexRGB = bgColor.getARGBHex().substring(2);
        cellStyle.setBgColor(new CColor(bgColorHexRGB));
    }

    //   "Fill Background Color"      ??,
    //   ? ??? .      
    XSSFColor fgColor = (XSSFColor) excelCellStyle.getFillForegroundColorColor();

    if (null != fgColor && 64 != fgColor.getIndexed()) {
        String fgColorHexRGB = fgColor.getARGBHex().substring(2);
        cellStyle.setFgColor(new CColor(fgColorHexRGB));
    }

    // TODO   
}

From source file:ru.spb.nicetu.tableviewer.server.XlsToHtml.java

License:Apache License

private void styleContents(CellStyle style, Formatter out, boolean isBuiltIn, boolean isNumeric) {
    if (isNumeric && style.getAlignment() == 0)
        styleOut("text-align", "right", out, isBuiltIn);
    else/*from  w ww  .  ja va2s  .  c  om*/
        styleOut("text-align", style.getAlignment(), ALIGN, out, isBuiltIn);
    styleOut("vertical-align", style.getVerticalAlignment(), VERTICAL_ALIGN, out, isBuiltIn);
    fontStyle(style, out, isBuiltIn);
    borderStyles(style, out, isBuiltIn);
    helper.colorStyles(style, out, isBuiltIn);
}