ClosedXML - C#中的样式行和列

时间:2016-02-09 14:08:12

标签: c# excel closedxml

我正在开发一个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;

1 个答案:

答案 0 :(得分:1)

有两种可能的方法:

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