【问题标题】:How do I change the Text Color and the Fillcolor如何更改文本颜色和填充颜色
【发布时间】:2014-09-22 18:53:23
【问题描述】:

如何将标题字体颜色更改为白色和填充绿色?这些是我正在使用的类:

import static org.apache.poi.ss.usermodel.CellStyle.*
import static org.apache.poi.ss.usermodel.IndexedColors.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.CellStyle
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.Font

这是我认为必须插入的代码。

Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD)
CellStyle headerStyle = wb.createCellStyle()
headerStyle.setFont(headerFont)

cellMOPID.setCellStyle(headerStyle)
cellType.setCellStyle(headerStyle)
cellStatus.setCellStyle(headerStyle)
cellState.setCellStyle(headerStyle)
cellStartDate.setCellStyle(headerStyle)
cellEndDate.setCellStyle(headerStyle)
cellDesc.setCellStyle(headerStyle)

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:
         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
    

    【讨论】:

      【解决方案2】:

      如果要将颜色设置为简单的单元格样式...可以编写类似的代码。

       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);
      

      这将用绿色填充单元格,字体将是粗体白色

      【讨论】:

      • XSSFCellStyle 位于哪个 jar 文件中?这看起来像我需要的!
      • 我使用的是 apache poi 3.9。 XSSFCellStyle 位于 org.apache.poi.xssf.usermodel.XSSFCellStyle
      • 我在文档顶部列出了 import org.apache.poi.xssf.usermodel.XSSFCellStyle 但我收到“无法解析类 org.apache.poi.xssf.usermodel.XSSFCellStyle”消息.这是否意味着我没有它?
      • 问题可能是因为您使用的是 HSSF 和 xls 文件。我正在使用 XSSF 和 xlsx 文件。如果它没有改变您的要求,请转到 xlsx 文件并使用 XSSF。如果您需要 xls 文件,则保留在 HSSF 上。或许stackoverflow.com/a/10924483/624003 会帮你搞定。
      • 无需从 HSSF 迁移到 XSSF 而不会出现任何特定的内存问题。U 也可以使用 hssf 为字体应用颜色。
      【解决方案3】:

      对于 xls 文件,我检查了以下内容,并且工作正常。

      我需要导入以下内容:

      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;
      

      这段代码如下:

          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();
      

      此代码的输出是索引二处工作表第一行的第一个单元格,将显示文本“标题文本”,单元格颜色为绿色,文本颜色为白色粗体字。

      我正在使用 apache POI 3.9。

      【讨论】:

        猜你喜欢
        • 2011-01-29
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        • 2018-11-12
        • 2017-12-19
        • 1970-01-01
        • 2021-05-23
        • 2018-12-25
        相关资源
        最近更新 更多