Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook getCellStyleAt

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook getCellStyleAt

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFWorkbook getCellStyleAt.

Prototype

@Override
public XSSFCellStyle getCellStyleAt(int idx) 

Source Link

Document

Get the cell style object at the given index

Usage

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

License:Apache License

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

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

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

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

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

        Workbook wbTmp = new XSSFWorkbook();
        wbTmp.createSheet();

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

        //  ? 
        XSSFWorkbook wb = excelService.loadWorkbook(sb.toString(), new XSSFWorkbook());
        LOGGER.debug("testModifyCellAttribute after loadWorkbook....");

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

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

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

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

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

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

        //////////////////////////////////////////////////////////////////////////
        // ?
        XSSFWorkbook wbT = excelService.loadWorkbook(sb.toString(), new XSSFWorkbook());
        Sheet sheetT = wbT.getSheet("cell test sheet2");
        LOGGER.debug("getNumCellStyles : {}", wbT.getNumCellStyles());

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

        XSSFFont fontT = cs1.getFont();
        LOGGER.debug("font getFontHeight : {}", fontT.getFontHeight());
        LOGGER.debug("font getBoldweight : {}", fontT.getBoldweight());
        LOGGER.debug("font getFontName : {}", fontT.getFontName());
        LOGGER.debug("getAlignment : {}", cs1.getAlignment());
        LOGGER.debug("getWrapText : {}", cs1.getWrapText());

        for (int i = 0; i < 100; i++) {
            Row row1 = sheetT.getRow(i);
            for (int j = 0; j < 5; j++) {
                Cell cell1 = row1.getCell(j);
                LOGGER.debug("row {}, cell {} : {}", i, j, cell1.getRichStringCellValue());
                assertEquals(320, fontT.getFontHeight());
                assertEquals(400, fontT.getBoldweight());
                LOGGER.debug("fontT.getBoldweight()? ? 400? ?");

                assertEquals(XSSFCellStyle.ALIGN_RIGHT, cs1.getAlignment());
                assertTrue(cs1.getWrapText());
            }
        }

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

From source file:opn.greenwebs.FXMLDocumentController.java

private void injectStock(XSSFWorkbook wbs, Object obj, int row, int col) {

    Row rowed = wbs.getSheet("Digital Version").getRow(row);
    Cell cell = rowed.getCell(col);/*from   w w w .j  a v a 2 s.co  m*/
    if (obj instanceof String) {
        cell.setCellValue((String) obj);
    } else if (obj instanceof Boolean) {
        cell.setCellValue((Boolean) obj);
    } else if (obj instanceof Date) {
        CellStyle cellStyle = wbs.getCellStyleAt(col);
        CreationHelper createHelper = wbs.getCreationHelper();
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("mm/dd/yyyy"));
        cell.setCellValue((Date) obj);
        cell.setCellStyle(cellStyle);
    } else if (obj instanceof Double) {
        cell.setCellValue((Double) obj);
    } else if (obj instanceof Integer) {
        cell.setCellValue((int) obj);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.HSSFCellStyleProducer.java

License:Open Source License

/**
 * The class does the dirty work of creating the HSSF-objects.
 *
 * @param workbook/*from   www.j  a v  a  2s  .  co m*/
 *          the workbook for which the styles should be created.
 */
public HSSFCellStyleProducer(final Workbook workbook, final boolean hardLimit,
        final ExcelColorProducer colorProducer, final ExcelColorProducer fontColorProducer) {
    this.fontColorProducer = fontColorProducer;
    if (workbook == null) {
        throw new NullPointerException();
    }
    if (colorProducer == null) {
        throw new NullPointerException();
    }
    this.colorProducer = colorProducer;
    this.styleCache = new HashMap<HSSFCellStyleKey, CellStyle>();
    this.workbook = workbook;
    this.fontFactory = new ExcelFontFactory(workbook, fontColorProducer);
    this.dataFormat = workbook.createDataFormat();
    this.hardLimit = hardLimit;

    if (workbook instanceof XSSFWorkbook) {
        final XSSFWorkbook xssfWorkbook = (XSSFWorkbook) workbook;
        final int predefinedStyles = workbook.getNumCellStyles();
        for (int i = 0; i < predefinedStyles; i++) {
            final XSSFCellStyle cellStyleAt = xssfWorkbook.getCellStyleAt(i);
            this.styleCache.put(new HSSFCellStyleKey(cellStyleAt), cellStyleAt);
        }
    } else {
        // Read in the styles ...
        final int predefinedStyles = workbook.getNumCellStyles();
        for (int i = 0; i < predefinedStyles; i++) {
            final CellStyle cellStyleAt = workbook.getCellStyleAt(i);
            this.styleCache.put(new HSSFCellStyleKey(cellStyleAt), cellStyleAt);
        }
    }
}