ClosedXML - 在 C# 中设置行和列的样式

ClosedXML - Style Rows and Columns in C#

我正在做一个 ASP.net Excel 项目,我正在尝试向特定单元格(M2、N2、O2、P2、Q2)添加一些不同的颜色(红色) ).

using (DataTable dt = new DataTable())
{
    sda.Fill(dt);
    using (XLWorkbook wb = new XLWorkbook())
    {
        wb.Worksheets.Add(dt, "Customers");

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;filename=Excel.xlsx");
        using (MemoryStream MyMemoryStream = new MemoryStream())
        {
            wb.SaveAs(MyMemoryStream);
            MyMemoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }
}

我想在 wb.Worksheets.Add(dt, "Customers"); 之后添加一些行,但我找不到方法。

我正在尝试实现这个:

dt.Rows(13, 14, 15, 16, 17).Style.Fill.BackgroundColor = XLColor.Red;

有两种可能的方法:

dt.Rows(13, 17)... // first and last row as number

dt.Rows("13, 14, 15, 16, 17").... // string with row numbers like in Excel itself

对于特定的单元格,您可以使用类似的调用(如问题中的 M2 ... Q2):

dt.Range(2, 14, 2, 18)... // row and column numbers for a rectangular area of cells

dt.Range("M2:Q2")... // string like in Excel