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:com.plugin.excel.util.ExcelUtil.java

License:Apache License

/**
 * @param oldCell/* ww  w .j  av  a 2 s  .c  om*/
 * @param newCell
 * @param styleMap
 */
public static void copyCell(Cell oldCell, Cell newCell, Map<Integer, CellStyle> styleMap) {
    if (styleMap != null) {
        if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
            newCell.setCellStyle(oldCell.getCellStyle());
        } else {
            int stHashCode = oldCell.getCellStyle().hashCode();
            CellStyle newCellStyle = styleMap.get(stHashCode);
            if (newCellStyle == null) {
                newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                styleMap.put(stHashCode, newCellStyle);
            }
            newCell.setCellStyle(newCellStyle);
        }
    }
    switch (oldCell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_BLANK:
        newCell.setCellType(Cell.CELL_TYPE_BLANK);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(oldCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        newCell.setCellErrorValue(oldCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        newCell.setCellFormula(oldCell.getCellFormula());
        break;
    default:
        break;
    }

}

From source file:com.qihang.winter.poi.excel.export.template.ExcelExportOfTemplateUtil.java

License:Apache License

/**
 * ??/*from   ww  w.j  a  v a  2 s .co m*/
 * @param cell
 * @param name
 * @return
 */
private List<com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams> getAllDataColumns(Cell cell,
        String name) {
    List<com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams> columns = new ArrayList<com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams>();
    cell.setCellValue("");
    if (name.contains(PoiElUtil.END_STR)) {
        columns.add(new com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams(
                name.replace(PoiElUtil.END_STR, PoiElUtil.EMPTY).trim(), cell.getCellStyle(),
                cell.getRow().getHeight()));
        return columns;
    }
    columns.add(new com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams(name.trim(),
            cell.getCellStyle(), cell.getRow().getHeight()));
    int index = cell.getColumnIndex();
    Cell tempCell;
    while (true) {
        tempCell = cell.getRow().getCell(++index);
        if (tempCell == null) {
            break;
        }
        String cellStringString;
        try {//?,??,?
            cellStringString = tempCell.getStringCellValue();
            if (StringUtils.isBlank(cellStringString)) {
                break;
            }
        } catch (Exception e) {
            throw new ExcelExportException("for each ,?");
        }
        //?cell 
        tempCell.setCellValue("");
        if (cellStringString.contains(PoiElUtil.END_STR)) {
            columns.add(new com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams(
                    cellStringString.trim().replace(PoiElUtil.END_STR, ""), tempCell.getCellStyle(),
                    tempCell.getRow().getHeight()));
            break;
        } else {
            if (cellStringString.trim().contains(teplateParams.getTempParams())) {
                columns.add(new com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams(
                        cellStringString.trim(), tempCell.getCellStyle(), tempCell.getRow().getHeight()));
            } else {
                //?
                break;
            }
        }

    }
    return columns;
}

From source file:com.qihang.winter.poi.util.PoiSheetUtility.java

License:Apache License

private static void cloneCell(Cell cNew, Cell cOld) {
    cNew.setCellComment(cOld.getCellComment());
    cNew.setCellStyle(cOld.getCellStyle());

    switch (cNew.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN: {
        cNew.setCellValue(cOld.getBooleanCellValue());
        break;//  w  w w.ja v a  2 s.c o  m
    }
    case Cell.CELL_TYPE_NUMERIC: {
        cNew.setCellValue(cOld.getNumericCellValue());
        break;
    }
    case Cell.CELL_TYPE_STRING: {
        cNew.setCellValue(cOld.getStringCellValue());
        break;
    }
    case Cell.CELL_TYPE_ERROR: {
        cNew.setCellValue(cOld.getErrorCellValue());
        break;
    }
    case Cell.CELL_TYPE_FORMULA: {
        cNew.setCellFormula(cOld.getCellFormula());
        break;
    }
    }

}

From source file:com.radaee.excel.ToHtml.java

License:Apache License

public void printStyles() {
    ensureOut();/*  ww  w.java 2 s. com*/

    // First, copy the base css
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("excelStyle.css")));
        //   in = new BufferedReader(new InputStreamReader())
        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:com.radaee.excel.ToHtml.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads();/*from w  w  w.  ja  v a2s .  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++) {
            String content = "&nbsp;";
            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 = getText(cell);

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

From source file:com.tikal.tallerWeb.servicio.reporte.cliente.AbstractSeccionXLS.java

License:Apache License

public void paintBorder(XSSFWorkbook wb, Sheet sheet, short borderType, BordeSeccion borde) {
    for (int i = borde.getUpperRow(); i <= borde.getLowerRow(); i++) {
        Row row = sheet.getRow(i);/*from w  ww. j  ava2  s . c om*/
        for (int j = borde.getLeftColumn(); j <= borde.getRightColumn(); j++) {
            if (i == borde.getUpperRow() || i == borde.getLowerRow() || j == borde.getLeftColumn()
                    || j == borde.getRightColumn()) {
                Cell cell = row.getCell(j);
                XSSFCellStyle actual = (XSSFCellStyle) cell.getCellStyle();
                XSSFCellStyle nuevo = wb.createCellStyle();
                nuevo.cloneStyleFrom(actual);
                if (i == borde.getUpperRow()) {
                    nuevo.setBorderTop(borderType);
                    nuevo.setTopBorderColor(IndexedColors.BLACK.getIndex());
                }
                if (i == borde.getLowerRow()) {
                    nuevo.setBorderBottom(borderType);
                    nuevo.setBottomBorderColor(IndexedColors.BLACK.getIndex());
                }
                if (j == borde.getLeftColumn()) {
                    nuevo.setBorderLeft(borderType);
                    nuevo.setLeftBorderColor(IndexedColors.BLACK.getIndex());
                }
                if (j == borde.getRightColumn()) {
                    nuevo.setBorderRight(borderType);
                    nuevo.setRightBorderColor(IndexedColors.BLACK.getIndex());
                }
                cell.setCellStyle(nuevo);
            }
        }
    }
}

From source file:com.tutorial.excelreadwrite.excelFunctions.java

public void convertColor(int r, int g, int b, int numColors) {
        //Get the userDefinedColor and set the style
        userDefinedColor = new XSSFColor(new java.awt.Color(r, g, b));
        XSSFCellStyle userDefinedCS = workbook.createCellStyle();
        userDefinedCS.setFillForegroundColor(userDefinedColor);
        userDefinedCS.setFillPattern(CellStyle.SOLID_FOREGROUND);

        //Create an arrayList and add foreground colors that will be converted and then remove them
        List<XSSFColor> listOfColors = new ArrayList();
        for (int i = 0; i < numColors; ++i) {
            try {
                //First row of excel document will be reserved for obtaining the colors of the foreground used
                listOfColors.add(sheet.getRow(0).getCell(i).getCellStyle().getFillForegroundXSSFColor());
                sheet.getRow(0).getCell(i).setCellStyle(null);
            } catch (NullPointerException ex) {
                throw new NullPointerException("Either incorrect # colors entered OR colors NOT SET.");
            }//  ww  w .  j a  v a  2 s  .co m
        }

        //Set-up rowIterator and get Row
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            //Set-up cellIterator and get Cell
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                //Null-Check for Cell
                if (cell != null) {
                    //Get the Cell Style, Null-Check for Cell Style
                    XSSFCellStyle currCellStyle = (XSSFCellStyle) cell.getCellStyle();
                    if (currCellStyle != null) {
                        //Get the fillForeground color
                        XSSFColor fgColor = currCellStyle.getFillForegroundXSSFColor();
                        //cycle through ArrayList and compare if any of the colors listed matches
                        for (XSSFColor col : listOfColors) {
                            if (col.equals(fgColor)) {
                                cell.setCellStyle(userDefinedCS);
                            }
                        }
                    }
                }

            }
        }
    }

From source file:com.tutorial.excelreadwrite.excelFunctions.java

    public void markHorizontal(int spacesApart){
        //Set-up rowIterator and get Row
        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()){
            Row row = rowIterator.next();
            //from   ww w  .j  a v a2  s .co m
            //Set-up cellIterator and get Cell
            Iterator<Cell> cellIterator = row.cellIterator();
            while(cellIterator.hasNext()){
                Cell cell = cellIterator.next();
                
                //Obtains the Cell Style
                XSSFCellStyle cellStyle = (XSSFCellStyle)cell.getCellStyle();
                //Checks to see if the Cell Style is null; if null, go to next cell
                if(cellStyle != null){
                    //Checks to see what color is the cell's color
                    XSSFColor cellColor = cellStyle.getFillForegroundXSSFColor();
                    //Checks to see if color is null; if not compare to accept only editted or userDefined cells
                    if(cellColor != null){
                        //Checks if current cell is userDefined or editted
                        //If it is not, then go to the next cell
                        if(cellColor.equals(mark.getFillForegroundXSSFColor()) || cellColor.equals(userDefinedColor)){

                            //Set boolean isCellMarked to false before proceeding
                            isCellMarked = false;

                            //Define Cell to be (spacesApart+1) away
                            //So if x = current cell then the cell that is 5 spacesApart =
                            // [x][][][][][][x]
                            Cell cellMark = sheet.getRow(cell.getRowIndex()).getCell(cell.getColumnIndex() + spacesApart + 1);

                            //Checks to see if cell is null; if present, get its Cell Style
                            if(cellMark != null){
                                XSSFCellStyle cellMarkStyle = (XSSFCellStyle)cellMark.getCellStyle();

                                //Checks to see if the style is null; if present, get its color
                                if(cellMarkStyle != null){
                                    XSSFColor cellMarkColor = cellMarkStyle.getFillForegroundXSSFColor();

                                    //Checks to see if the color is null; if present, compare colors
                                    if(cellMarkColor != null){
                                        if(cellMarkColor.equals(userDefinedColor)){
                                            isCellMarked = true;
                                        }
                                    }
                                }
                            }

                            /*
                            ** CHECK#1: 'isCellMarked'
                            ** If isCellMarked is marked true, start iterating through the
                            ** cells in between and check if null or not userDefinedStyle
                            */
                            if(isCellMarked == true){
                                for(int i = 1; i <= spacesApart; ++i){
                                    Cell isNull = sheet.getRow(cell.getRowIndex()).getCell(cell.getColumnIndex()+i);

                                    //Checks to see if the cell is null; if color is present, set isCellMarked to false
                                    if(isNull != null){
                                        XSSFCellStyle cellCheckIfNullCellStyle = (XSSFCellStyle)isNull.getCellStyle();
                                        if(cellCheckIfNullCellStyle != null){
                                            XSSFColor cellCheckIfNullColor = cellCheckIfNullCellStyle.getFillForegroundXSSFColor();
                                            if(cellCheckIfNullColor != null){
                                                if(cellCheckIfNullColor.equals(userDefinedColor)){
                                                    isCellMarked = false;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            /*
                            ** CHECK#2: 'isCellMarked2'
                            ** If isCellMarked remains as true, set the two cell's style
                            */
                            if(isCellMarked == true){
                                cell.setCellStyle(mark);
                                cellMark.setCellStyle(mark);
                            }
                        }
                }
            }
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.action.SpreadsheetAction.java

License:Open Source License

/**
 * Returns the locking status of the given cell.
 * //from ww w.j a v a 2 s .  c  om
 * @param cell
 *            Target cell
 * @return true if the given cell is locked
 */
protected boolean isCellLocked(Cell cell) {
    return cell.getSheet().getProtect() && cell.getCellStyle().getLocked();
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionShifter.java

License:Open Source License

/**
 * "Shifts" cell value. Shifting here is an Excel term and means the
 * situation where the user has selected one or more cells, and grabs the
 * bottom right hand square of the selected area to extend or curtail the
 * selection and fill the new area with values determined from the existing
 * values.//  w  w w.jav a  2  s  .  co m
 * 
 * @param shiftedCell
 *            Source cell
 * @param newCell
 *            Resulting new cell
 * @param removeShifted
 *            true to remove the source cell at the end
 * @param sequenceIncrement
 *            increment added to shifted cell value
 */
protected void shiftCellValue(Cell shiftedCell, Cell newCell, boolean removeShifted, Double sequenceIncrement) {
    // clear the new cell first because it might have errors which prevent
    // it from being set to a new type
    if (newCell.getCellType() != Cell.CELL_TYPE_BLANK || shiftedCell.getCellType() == Cell.CELL_TYPE_BLANK) {
        newCell.setCellType(Cell.CELL_TYPE_BLANK);
    }
    newCell.setCellType(shiftedCell.getCellType());
    newCell.setCellStyle(shiftedCell.getCellStyle());
    spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
    switch (shiftedCell.getCellType()) {
    case Cell.CELL_TYPE_FORMULA:
        shiftFormula(shiftedCell, newCell);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(shiftedCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        newCell.setCellValue(shiftedCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        shiftNumeric(shiftedCell, newCell, sequenceIncrement);
        break;
    case Cell.CELL_TYPE_STRING:
        shiftString(shiftedCell, newCell, sequenceIncrement);
        break;
    case Cell.CELL_TYPE_BLANK:
        // cell is cleared when type is set
    default:
        break;
    }
    spreadsheet.getCellValueManager().cellUpdated(newCell);
    if (removeShifted) {
        shiftedCell.setCellValue((String) null);
        spreadsheet.getCellValueManager().cellDeleted(shiftedCell);
    }
}