简体   繁体   中英

Apache POI set cell Font and font color

I am reading an xlsx file using XSSF of Apache POI. When reading the font of a particular cell and applying in new cell that font is applied for the whole sheet not for that perticular cell. i want for Example:

        column1 column2      column3 
row1    ARIAL   TIMES ROMAN  ARIAL

row2    TIMES ROMAN ARIAL   ARIAL   

row3     ARIAL   ARIAL   ARIAL   

I want the same font in each cell that was in original file.

My code is:

public XSSFCellStyle setFontOnCell(XSSFCellStyle cellStyle, XSSFCell cell)
    {
        try {
            font = cell.getCellStyle().getFont();
            new_font.setFontName(font.getFontName());
            new_font.setBoldweight(font.getBoldweight());
            new_font.setFontHeight((short)font.getFontHeight());
            new_font.setFamily(font.getFamily());
            cellStyle.setFont(new_font);
            return cellStyle;
        } catch (Exception e) {
            //System.out.println(e.getMessage());
            return cellStyle;
        }
    }

Your new_font seems to be a global variable, and you keep changing it and then injecting it into cells. It should be local to setFontOneCell() so that each cell gets its own Font instance, instead of all of them actually using the same object,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM