【问题标题】:How can I set the RGB Color in font Using xssfworkbook npoi如何使用 xssfworkbook npoi 设置字体中的 RGB 颜色
【发布时间】:2016-11-08 11:01:36
【问题描述】:

如何使用 npoi 使​​用 xssfworkbook 类在 cell backgroudn 中设置 RGB 颜色?

byte[] rgb = new byte[3] { 192, 50, 90 };
XSSFCellStyle HeaderCellStyle1 = (XSSFCellStyle)wb.CreateCellStyle();
HeaderCellStyle1.SetFillForegroundColor(new XSSFColor(new Color(255, 255, 255)));

我不想使用这种模式:

titlestyle.BottomBorderColor = IndexedColors.Grey25Percent.Index;

【问题讨论】:

    标签: c# colors background rgb npoi


    【解决方案1】:
        solution of your problem is here
    
        here simply define new xssfcolor and assign it to xssfcellstyle     
    
    
     var color = new XSSFColor(new byte[] { 0,255, 0 });
     var rowstyle =(XSSFCellStyle)wb.CreateCellStyle();
     rowstyle.SetFillForegroundColor(color)
    

    【讨论】:

    • 我必须再添加一行才能使其在 .Net Core 3.1 中正常工作:rowStyle.FillPattern = FillPattern.SolidForeground;
    【解决方案2】:

    您必须首先确保将字体转换为XSSFFont,IFont 不提供对字体的 RGB 颜色属性的访问。

    然后您可以使用XSSColor 设置颜色,它可以从字节数组或 System.Drawing.Color 对象构造。

    示例代码,cmets 中不同种类的构造函数:

    var wb = new XSSFWorkbook();
    var sheet = wb.CreateSheet("Sheet 1");
    
    // Create a colored font
    var font = (XSSFFont) wb.CreateFont();
    // var color = new XSSFColor(ColorTranslator.FromHtml("#C88C14"));
    // var color = new XSSFColor(new Color(255, 255, 255));
    var color = new XSSFColor(new byte[] {200, 140, 20});
    font.SetColor(color);
    
    // Create a dedicated cell style using that font 
    var style = wb.CreateCellStyle();
    style.SetFont(font);
    
    // Create some cell values
    var row = sheet.CreateRow(0);
    row.CreateCell(0).SetCellValue("Standard text");
    var cell = row.CreateCell(1);
    cell.SetCellValue("Colored text");
    
    // Apply the cellstyle we created
    cell.CellStyle = style;
    

    【讨论】:

    • Paul,这不会改变字体颜色吗? PParmar 的原始问题询问如何更改单元格背景/前景色。
    • @AmosLong 如果您查看问题历史记录,您会发现专门针对字体颜色提出的-original- 问题:-)。这个问题在我回答后被修改了,使得这个答案有点过时了。我将其搁置是因为有些人似乎觉得它很有用。
    猜你喜欢
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多