Multiple Styles to Excel Cell POI

前端 未结 2 1494
迷失自我
迷失自我 2020-12-19 02:20

I want to apply colour to cell as well as Format Cell value(e.g. Date,Amount).But when I am applying two Cell Style only the last style is gets applied on cell.



        
相关标签:
2条回答
  • 2020-12-19 03:00

    Multiple cell styles cannot be applied to a single Cell. The last cell style applied will overwrite any pre-existing cell style on the Cell. Setting multiple CellStyles won't combined the set attributes of each style.

    The solution is to create another CellStyle that has the desired attributes of both of the other CellStyles. You can use the cloneStyleFrom method to start with the attributes of one CellStyle.

    CellStyle combined = workbook.createCellStyle();
    combined.cloneStyleFrom(colourCellStyle);
    combined.setDataFormat(dateCellStyle.getDataFormat());
    // You can copy other attributes to "combined" here if desired.
    
    cell9.setCellStyle(combined);
    

    This technique can be generalized to clone any existing cell style and copy individual attributes from a second existing cell style. As always, reuse any existing CellStyles, but if a different combination of attributes is required, then you must create and use a new CellStyle.

    0 讨论(0)
  • 2020-12-19 03:07

    You can create a map of styles and then you can use different styles throughout the java program.

    For example

    Map<String, CellStyle> cellStyles = new HashMap<String, CellStyle>();
    DataFormat dataFormat = workbook.createDataFormat();
    
    XSSFCellStyle cellStyle;
    XSSFFont font;
    
    cellStyle = workbook.createCellStyle();
    
    cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    cellStyle.setAlignment(cellStyle.ALIGN_CENTER_SELECTION);   
    font = workbook.createFont();
    font.setFontHeightInPoints((short)16);
    font.setFontName("Calibri");                                            
    cellStyle.setFont(font);
    cellStyles.put("header_cell_style", cellStyle);
    
    cellStyle = workbook.createCellStyle(); 
    cellStyle.setAlignment(cellStyle.ALIGN_CENTER_SELECTION);
    font = workbook.createFont();
    font.setFontHeightInPoints((short)12);
    font.setFontName("Calibri");                   
    cellStyle.setFont(font);
    cellStyles.put("normal_cell_style", cellStyle);
    
    cellStyle = workbook.createCellStyle();
    cellStyle.setAlignment(cellStyle.ALIGN_CENTER_SELECTION);       
    cellStyle.setDataFormat(dataFormat.getFormat("dd-mmm-yyyy"));
    font = workbook.createFont();
    font.setFontHeightInPoints((short)12);
    font.setFontName("Calibri");                   
    cellStyle.setFont(font);
    cellStyles.put("date_cell_style", cellStyle);
    
    cellStyle = workbook.createCellStyle();
    cellStyle.setAlignment(cellStyle.ALIGN_CENTER_SELECTION);       
    cellStyle.setDataFormat(dataFormat.getFormat("dd-mmm-yyyy"));
    font = workbook.createFont();
    font.setFontHeightInPoints((short)16);
    font.setFontName("Calibri");                   
    cellStyle.setFont(font);
    cellStyles.put("header_date_cell_style", cellStyle);
    
    return cellStyles;       
    

    and then use this map like

    Map<String, CellStyle> multipleCellStyles = createMultipleExcelCellStyles(workbook);
    
    headerCellD1.setCellStyle(multipleCellStyles.get("header_cell_style"));
    
    cellB.setCellStyle(multipleCellStyles.get("normal_cell_style"));
    
    cellC.setCellStyle(multipleCellStyles.get("date_cell_style"));
    
    0 讨论(0)
提交回复
热议问题