How do I change the Text Color and the Fillcolor

后端 未结 3 565
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 08:44

How do I change the header font color to white and the fill green? These are the classes that I am using:

import static org.apache.poi.ss.usermodel.CellStyl         


        
相关标签:
3条回答
  • 2020-12-15 09:06

    For xls file I have checked the following and is working fine on my end.

    I need to import following:

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Font;
    import org.apache.poi.ss.usermodel.IndexedColors;
    

    and the piece of code is following:

        FileInputStream fin = new FileInputStream (XLSFileAddress);
        HSSFWorkbook wb = new HSSFWorkbook(fin);
        HSSFCell cell=wb.getSheetAt(2).getRow(0).getCell(0);
        cell.setCellValue("Header Text");
        Font headerFont = wb.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        CellStyle headerStyle = wb.createCellStyle();
        headerStyle.setFont(headerFont);
        headerStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(headerStyle);
        FileOutputStream fileOut = new FileOutputStream(XLSFileAddress);
        wb.write(fileOut);
        fileOut.close();
    

    The output of this code is the first cell of first Row of sheet at index two, will show the text "Header Text" with cell color as Green and Text Color is White with Bold Font.

    I am using apache POI 3.9.

    0 讨论(0)
  • 2020-12-15 09:16

    If you want to set the color as simple cellstyle... you can write code like.

     cell.setCellValue("Header Text");
     XSSFCellStyle headerStyle = wb.createCellStyle();
     Font headerFont = wb.createFont();
     headerStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
     headerFont.setColor(IndexedColors.WHITE.getIndex());
     headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
     headerStyle.setFont(headerFont);
     cell.setCellStyle(headerStyle);
    

    This will fill the cell with GREEN color and the font will be of Bold WHITE color

    0 讨论(0)
  • 2020-12-15 09:21
         HSSFCellStyle cellStyle = workBook.createCellStyle();        
         HSSFFont font = wb.createFont();
         font.setFontName(XSSFFont.DEFAULT_FONT_NAME);
         font.setFontHeightInPoints((short)10);
         font.setColor(IndexedColors.BLUE.getIndex());
         cellStyle.setFont(font);
        //the version i am using is poi-3.8
    
    0 讨论(0)
提交回复
热议问题