使用F#和GemBox.Spreadsheet“堆叠”单元格样式

我使用GemBox.Spreadsheet将数据从F#写入Excel。 从F#获取数据到Excel是相当简单的。 将单个单元格样式添加到特定或单元格区域也相当简单。 然而,我试图“堆叠”样式(即添加多个样式到同一个单元格),但是我很难确定是否1)这是可能的, 2)如果可能的话,怎么做?

下面是一些简单的F#代码:

 open System open System.Data open System.Xml open System.Linq open System.Text open System.Drawing open GemBox.Spreadsheet SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY") //initialize a new ExcelFile let excelFile = new ExcelFile() //Create a new worksheet in the excel object let ws = excelFile.Worksheets.Add("data") //Create a new DataTable to fill with data let dt = new DataTable("dataTable") dt.MinimumCapacity <- 1 let dcStatus = new DataColumn("Status") let dcNumber = new DataColumn("Number") //Add the columns to the DataTable, dt dt.Columns.Add(dcStatus) dt.Columns.Add(dcNumber) dt.Rows.Add("Status", "Number") dt.Rows.Add("Not Started - behind schedule", 3) dt.Rows.Add("In Progress - behind schedule", 5) dt.Rows.Add("Withdrawn", 3) dt.Rows.Add("Total", 11) //Define red style let redStyle = new CellStyle() redStyle.Font.Color <- SpreadsheetColor.FromName(ColorName.Red) //Define bold style let boldStyle = new CellStyle() boldStyle.Font.Weight <- ExcelFont.BoldWeight //Apply redStyle to row 1:3 and cols 0:1 ws.Cells.GetSubrangeAbsolute(1,0, 3, 1).Style <- redStyle //Apply boldStyle to row 1:3, col 0 ws.Cells.GetSubrangeAbsolute(1, 0, 4, 0).Style <- boldStyle //Insert datatable with GemBoxSpreadsheet options; GemBox starts with cell A1 as [0,0] //StartRow == 0, StartColumn == 0 ws.InsertDataTable(dt, InsertDataTableOptions(0, 0)) //Set Col with of first column to autofit ws.Columns.[0].AutoFit() //Write excelFile to filePath excelFile.Save("[YOUR_LOCAL_FILEPATH/]filename.xlsx")' 

当我将一种样式应用于一系列单元格时,将另一种样式应用于该范围,第一种样式将被覆盖。

样式可以用GemBox.Spreadsheet“叠加”,还是需要为每个我想应用的粗体 /红色和粗体单元格创build/应用样式? 这里提供的数据是一个相当简化的数据集。 在大多数情况下,我会有很多不同的风格,理想情况下,可以堆叠。

感谢您的意见和帮助。

所以你定义了一个新的风格。 只设置必要的部分:

 //Apply redStyle to row 1:3 and cols 0:1 ws.Cells.GetSubrangeAbsolute(1, 0, 3, 1).Style <- redStyle //Apply boldStyle to row 1:3, col 0 ws.Cells.GetSubrangeAbsolute(1, 0, 4, 0).Style.Font.Weight <- ExcelFont.BoldWeight 

之前:

之前:

现在:

新