如何在Spreadsheetgear中设置文本和背景颜色?

时间:2019-04-19 14:36:17

标签: vb.net spreadsheetgear

在SpreadsheetGear 2012中,我要设置:

  • 将文本颜色更改为白色。
  • 单元格的背景颜色变为黑色。

我尝试了以下形式的变化:

Sheet_Obj.Cells(0,0).Style.NumberFormat = "@"
Sheet_Obj.Cells(0,0).Interior.Color = Spreadsheetgear.Color.FromArgb(&H0)
Sheet_Obj.Cells(0,0).Style.Font.Color = SpreadsheetGear.Color.FromArgb(&HFFFFFF)
Sheet_Obj.Cells(0,0).Value = "Terabytes"

但是没有任何东西可以工作。

示例:

Example

1 个答案:

答案 0 :(得分:1)

您的代码正在将格式直接应用于单元格本身与单元格使用的单元格样式混合在一起。有关此详细信息,请参见我的答案here

如果只想影响具有特定格式的特定单元格,则需要使用IRange。Interior / IRange。Font。颜色等。示例:

Dim workbook As SpreadsheetGear.IWorkbook = SpreadsheetGear.Factory.GetWorkbook()
Dim Sheet_Obj As SpreadsheetGear.IWorksheet = workbook.ActiveWorksheet

' This modifies the format of the cell directly.
Sheet_Obj.Cells(0, 0).Interior.Color = SpreadsheetGear.Colors.Black
Sheet_Obj.Cells(0, 0).Font.Color = SpreadsheetGear.Colors.White
Sheet_Obj.Cells(0, 0).NumberFormat = "@"

如果您希望工作簿中的所有单元格都具有特定格式,那么修改这些单元格使用的样式(默认情况下为“普通”样式)将是解决该问题的好方法。您可以通过IRange。IStyle访问特定单元格使用的当前Style。您可以通过IWorkbook。collection of available IStyles集合访问整个Styles。示例:

' This modifies whatever IStyle is currently used by this cell (probably the "Normal" 
' style).  All other cells which also use this still will be affected as well.
Sheet_Obj.Cells(0, 0).Style.Interior.Color = SpreadsheetGear.Colors.Black
Sheet_Obj.Cells(0, 0).Style.Font.Color = SpreadsheetGear.Colors.White
Sheet_Obj.Cells(0, 0).Style.NumberFormat = "@"

' Assuming you are modifying the "Normal" style, this code is equivalent to the above code.
Dim style As SpreadsheetGear.IStyle = workbook.Styles("Normal")
style.Interior.Color = SpreadsheetGear.Colors.Black
style.Font.Color = SpreadsheetGear.Colors.White
style.NumberFormat = "@"