Example usage for org.apache.poi.ss.usermodel Cell getCellStyle

List of usage examples for org.apache.poi.ss.usermodel Cell getCellStyle

Introduction

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

Prototype

CellStyle getCellStyle();

Source Link

Document

Return the cell's style.

Usage

From source file:document.ResultExcelDocument.java

protected void addResultmodCell(List<String> resultmods, Row row, int cellIdx, int pos, Result result) {
    StringBuilder sb = new StringBuilder();
    if (resultmods != null) {
        int size = resultmods.size();
        for (int i = 0; i < size; ++i) {
            String mod = resultmods.get(i);
            sb.append(mod).append(i == size - 1 ? "" : "\n");
        }//  ww  w.  j av  a2s  . c  o m
    }
    Cell c = addCell(row, cellIdx, sb.toString());
    applyMedalistCellStyle(c, pos, false, result);
    c.getCellStyle().setWrapText(true);
}

From source file:edu.scripps.fl.pubchem.xml.extract.AssayExtractor.java

License:Apache License

private void putStringListInModel(ExcelTableModel model, List<String> strings, Integer count, String name) {
    for (int hh = 0; hh < strings.size(); hh++) {
        String ss = strings.get(hh);
        Integer row = hh + count;
        model.setValueAt(name, row, 0);/*from   w  ww  .  jav  a 2  s.c  om*/
        model.setValueAt(ss, row, 1);
        Cell cell = model.getCellAt(row, 1);
        CellStyle style = cell.getCellStyle();
        style.setWrapText(true);
        cell.setCellStyle(style);
    }
}

From source file:examples.toHTML.ToHtml.java

License:Apache License

public void printStyles() {
    ensureOut();//www.ja v a2s.  co  m

    // First, copy the base css
    BufferedReader in = null;
    try {

        FileInputStream f = new FileInputStream(path + File.separator + "resources" + File.separator + "html"
                + File.separator + "excelStyle.css");
        in = new BufferedReader(new InputStreamReader(f));

        /**
         in = new BufferedReader(new InputStreamReader(
         getClass().getResourceAsStream("excelStyle.css")));
         */

        String line;
        while ((line = in.readLine()) != null) {
            out.format("%s%n", line);
        }
    } catch (IOException e) {
        throw new IllegalStateException("Reading standard css", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                //noinspection ThrowFromFinallyBlock
                throw new IllegalStateException("Reading standard css", e);
            }
        }
    }

    // now add css for each used style
    Set<CellStyle> seen = new HashSet<CellStyle>();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        Iterator<Row> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            Row row = rows.next();
            for (Cell cell : row) {
                CellStyle style = cell.getCellStyle();
                if (!seen.contains(style)) {
                    printStyle(style);
                    seen.add(style);
                }
            }
        }
    }
}

From source file:examples.toHTML.ToHtml.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads();/*  www.  j a v a 2  s  .  c  o m*/

    out.format("<tbody>%n");
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();

        out.format("  <tr>%n");
        out.format("    <td class=\"%s\">%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
        for (int i = firstColumn; i < endColumn; i++) {
            // &nbsp;
            String content = " ";
            String attrs = "";
            CellStyle style = null;
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    style = cell.getCellStyle();
                    attrs = tagStyle(cell, style);
                    //Set the value that is rendered for the cell
                    //also applies the format
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;

                    content = replaceUmlaut(content);

                    if (content.equals(""))
                        // &nbsp;
                        content = " ";
                }
            }
            out.format("    <td class=\"%s %s\">%s</td>%n", styleName(style), attrs, content);
        }
        out.format("  </tr>%n");
    }
    out.format("</tbody>%n");
}

From source file:fr.amapj.service.engine.generator.excel.ExcelGeneratorTool.java

License:Open Source License

private void colorize(Row row, int nbDates, int nbProd) {
    for (int i = 0; i < nbDates / 2; i++) {
        int index = 3 + nbProd + i * 2 * nbProd;
        for (int j = 0; j < nbProd; j++) {
            Cell cell = row.getCell(j + index);
            if (cell != null) {
                CellStyle st = cell.getCellStyle();
                beOrange(st);//w  w w .j  a va  2s  .c o m
                cell.setCellStyle(st);

            }
        }

    }

}

From source file:fr.amapj.service.engine.generator.excel.samples.DiagonalBorder.java

License:Open Source License

private void displayCell(Cell cell) throws Exception {

    CellStyle style = cell.getCellStyle();

    Field f = HSSFCellStyle.class.getDeclaredField("_format");
    f.setAccessible(true);//  w  ww . j  a va  2 s . c o m

    ExtendedFormatRecord efr = (ExtendedFormatRecord) f.get(style);

    System.out.println("getAdtlDiag=" + efr.getAdtlDiag());
    System.out.println("getDiag=" + efr.getDiag());
    System.out.println("getAdtlDiagLineStyle=" + efr.getAdtlDiagLineStyle());
    System.out.println("isIndentNotParentBorder=" + efr.isIndentNotParentBorder());
}

From source file:generate.CopyRow.java

private static void copyCellStyle(Workbook workbook, Cell oldCell, Cell newCell) {
    //ClonedStyleFactory c = new ClonedStyleFactory();
    newCell.setCellStyle(oldCell.getCellStyle());
}

From source file:gov.nih.nci.evs.app.neopl.XLStoXLSX.java

License:Open Source License

/**
 * @param args/*from w ww. ja v a 2s  .c o m*/
 * @throws InvalidFormatException
 * @throws IOException
 */

public static void run(String inputfile, String outputfile) throws IOException {
    InputStream in = new BufferedInputStream(new FileInputStream(inputfile));
    try {
        Workbook wbIn = new HSSFWorkbook(in);
        File outFn = new File(outputfile);
        if (outFn.exists()) {
            outFn.delete();
        }

        Workbook wbOut = new XSSFWorkbook();
        int sheetCnt = wbIn.getNumberOfSheets();
        for (int i = 0; i < sheetCnt; i++) {
            Sheet sIn = wbIn.getSheetAt(0);
            Sheet sOut = wbOut.createSheet(sIn.getSheetName());
            Iterator<Row> rowIt = sIn.rowIterator();
            while (rowIt.hasNext()) {
                Row rowIn = rowIt.next();
                Row rowOut = sOut.createRow(rowIn.getRowNum());

                Iterator<Cell> cellIt = rowIn.cellIterator();
                while (cellIt.hasNext()) {
                    Cell cellIn = cellIt.next();
                    Cell cellOut = rowOut.createCell(cellIn.getColumnIndex(), cellIn.getCellType());

                    switch (cellIn.getCellType()) {
                    case Cell.CELL_TYPE_BLANK:
                        break;

                    case Cell.CELL_TYPE_BOOLEAN:
                        cellOut.setCellValue(cellIn.getBooleanCellValue());
                        break;

                    case Cell.CELL_TYPE_ERROR:
                        cellOut.setCellValue(cellIn.getErrorCellValue());
                        break;

                    case Cell.CELL_TYPE_FORMULA:
                        cellOut.setCellFormula(cellIn.getCellFormula());
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        cellOut.setCellValue(cellIn.getNumericCellValue());
                        break;

                    case Cell.CELL_TYPE_STRING:
                        cellOut.setCellValue(cellIn.getStringCellValue());
                        break;
                    }

                    {
                        CellStyle styleIn = cellIn.getCellStyle();
                        CellStyle styleOut = cellOut.getCellStyle();
                        styleOut.setDataFormat(styleIn.getDataFormat());
                    }
                    cellOut.setCellComment(cellIn.getCellComment());

                    // HSSFCellStyle cannot be cast to XSSFCellStyle
                    // cellOut.setCellStyle(cellIn.getCellStyle());
                }
            }
        }
        OutputStream out = new BufferedOutputStream(new FileOutputStream(outFn));
        try {
            wbOut.write(out);
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
}

From source file:guru.qas.martini.report.DefaultState.java

License:Apache License

public void updateLongestExecutions() {
    if (!longestExecutionCells.isEmpty()) {
        for (Cell cell : longestExecutionCells) {
            CellStyle original = cell.getCellStyle();
            Sheet sheet = cell.getSheet();
            Workbook workbook = sheet.getWorkbook();
            CellStyle newStyle = workbook.createCellStyle();
            newStyle.cloneStyleFrom(original);
            int originalFontIndex = original.getFontIndexAsInt();
            Font originalFont = workbook.getFontAt(originalFontIndex);

            Font font = workbook.createFont();
            font.setBold(true);//from  w  w w .j  ava 2s  .  c  o m
            font.setColor(IndexedColors.DARK_RED.getIndex());
            font.setFontHeight((short) Math.round(originalFont.getFontHeight() * 1.5));
            newStyle.setFont(font);
            cell.setCellStyle(newStyle);

            Row row = cell.getRow();
            short firstCellNum = row.getFirstCellNum();
            short lastCellNum = row.getLastCellNum();

            for (int i = firstCellNum; i < lastCellNum; i++) {
                Cell rowCell = row.getCell(i);
                original = rowCell.getCellStyle();
                CellStyle borderStyle = workbook.createCellStyle();
                borderStyle.cloneStyleFrom(original);
                borderStyle.setBorderTop(BorderStyle.MEDIUM);
                borderStyle.setBorderBottom(BorderStyle.MEDIUM);

                if (i == cell.getColumnIndex()) {
                    borderStyle.setBorderLeft(BorderStyle.MEDIUM);
                    borderStyle.setBorderRight(BorderStyle.MEDIUM);
                } else if (i == firstCellNum) {
                    borderStyle.setBorderLeft(BorderStyle.MEDIUM);
                } else if (i == lastCellNum - 1) {
                    borderStyle.setBorderRight(BorderStyle.MEDIUM);
                }
                rowCell.setCellStyle(borderStyle);
            }
        }
    }
}

From source file:guru.qas.martini.report.DefaultState.java

License:Apache License

protected void colorRow(short color, Row row) {
    short firstCellNum = row.getFirstCellNum();
    short lastCellNum = row.getLastCellNum();
    for (int i = firstCellNum; i <= lastCellNum; i++) {
        Cell cell = row.getCell(i);
        if (null != cell) {
            CellStyle cellStyle = cell.getCellStyle();
            Workbook workbook = cell.getSheet().getWorkbook();
            CellStyle clone = workbook.createCellStyle();

            clone.cloneStyleFrom(cellStyle);
            clone.setFillForegroundColor(color);
            clone.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            BorderStyle borderStyle = cellStyle.getBorderLeftEnum();
            clone.setBorderLeft(BorderStyle.NONE == borderStyle ? BorderStyle.THIN : borderStyle);
            short borderColor = cellStyle.getLeftBorderColor();
            clone.setLeftBorderColor(0 == borderColor ? IndexedColors.BLACK.getIndex() : borderColor);

            borderStyle = cellStyle.getBorderRightEnum();
            clone.setBorderRight(BorderStyle.NONE == borderStyle ? BorderStyle.THIN : borderStyle);
            borderColor = cellStyle.getRightBorderColor();
            clone.setRightBorderColor(0 == borderColor ? IndexedColors.BLACK.getIndex() : borderColor);

            borderStyle = cellStyle.getBorderTopEnum();
            clone.setBorderTop(BorderStyle.NONE == borderStyle ? BorderStyle.THIN : borderStyle);
            borderColor = cellStyle.getTopBorderColor();
            clone.setTopBorderColor(0 == borderColor ? IndexedColors.BLACK.getIndex() : borderColor);

            borderStyle = cellStyle.getBorderBottomEnum();
            clone.setBorderBottom(BorderStyle.NONE == borderStyle ? BorderStyle.THIN : borderStyle);
            borderColor = cellStyle.getBottomBorderColor();
            clone.setBottomBorderColor(borderColor);
            cell.setCellStyle(clone);/*from  w  ww .j  av a  2  s. c o m*/
        }
    }
}