ClosedXML - 添加新行而不覆盖数据(第一行)

时间:2016-02-10 12:07:40

标签: c# asp.net datatable row closedxml

我在填写Excel文档之前尝试添加空行。

using (DataTable dt = new DataTable())
{
    sda.Fill(dt);

    using (XLWorkbook wb = new XLWorkbook())
    {
        var ws = wb.Worksheets.Add(dt, "Report");
        var listOfStrings = new List<String>();
        ws.Cell(1, 6).Value = "Service";
        ws.Cell(1, 15).Value = "Invoice";

        ws.Range("A1:L1").Style.Fill.BackgroundColor = XLColor.DarkBlue;
        ws.Range("M1:Q1").Style.Fill.BackgroundColor = XLColor.DarkCandyAppleRed;
        ws.Range("M2:Q2").Style.Fill.BackgroundColor = XLColor.Red;

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;filename=Report.xlsx");

        using (MemoryStream MyMemoryStream = new MemoryStream())
        {
            wb.SaveAs(MyMemoryStream);
            MyMemoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }
    }
}

我只需要将第一行设为空,然后用我的数据填充文档。 但是我一直在覆盖这份文件。

1 个答案:

答案 0 :(得分:3)

要在工作表的第一行上方插入一行,请使用:

ws.Row(1).InsertRowsAbove(1);
                       // ^ number of rows to insert

还有方法InsertRowsBelow()在特定行下面插入新行。有关更多示例,请参阅documentation