"堆叠"使用F#和GemBox.Spreadsheet的单元格样式

时间:2015-08-31 20:27:05

标签: excel f# f#-3.0 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")'

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

样式可以&#34;堆叠&#34;使用GemBox.Spreadsheet还是我需要为每个要应用的粗体 / red和粗体单元格创建/应用样式?这里提供的数据是一个相当简化的数据集;在大多数情况下,我会有许多不同的风格,理想情况下,可以堆叠。

感谢您的意见和帮助。

1 个答案:

答案 0 :(得分:2)

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

//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

在:

Before:

现在:

New