如何在 Spreadsheetgear 中设置文本和背景颜色?
How to set text and background color in 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"
但是还没有任何东西可以工作。
示例:
您的代码混淆了将格式直接应用于单元格本身与单元格使用的单元格样式。请参阅我的回答 here 了解更多详情。
如果您只想影响具有特定格式的特定单元格,则需要使用 IRange。Interior / IRange.Font.Color / 等。示例:
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 = "@"
如果您希望工作簿中的所有单元格都具有特定格式,那么修改这些单元格使用的样式(默认情况下是 "Normal" 样式)将是一个很好的方法.您可以访问当前 IStyle used by a particular cell via IRange.Style. You can access the entire collection of available IStyles via the IWorkbook.Styles collection。示例:
' 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 = "@"
我想在 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"
但是还没有任何东西可以工作。
示例:
您的代码混淆了将格式直接应用于单元格本身与单元格使用的单元格样式。请参阅我的回答 here 了解更多详情。
如果您只想影响具有特定格式的特定单元格,则需要使用 IRange。Interior / IRange.Font.Color / 等。示例:
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 = "@"
如果您希望工作簿中的所有单元格都具有特定格式,那么修改这些单元格使用的样式(默认情况下是 "Normal" 样式)将是一个很好的方法.您可以访问当前 IStyle used by a particular cell via IRange.Style. You can access the entire collection of available IStyles via the IWorkbook.Styles collection。示例:
' 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 = "@"