ClosedXML 删除第一行

ClosedXML deleted first row

我正在使用 ClosedXML 导出 Excel 文件。我的问题是:1-第一行已删除 2-未应用列宽。我认为第一行被删除是因为我正在使用 2 行 headers,我尝试解决但没有成功。这是我的源代码:

protected void ExportExcel()
{
    String constr = "Password=*****;Persist Security Info=True;User ID=*****;Initial Catalog=*****;Data Source=******;";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("select ServiceName,Sdate,Dfrom,Dto,Peroid,Activity,Sector,WorkStation,Place,ConNumber,ErrorDes,Solutions from ServiceCrashies where Sdate between '" + Request.Form[FromDate.UniqueID] + "' and '" + Request.Form[ToDate.UniqueID] + "'"))
        {
            //con.Open();
            //int numberofrows = cmd.ExecuteNonQuery();
            //System.Diagnostics.Debug.WriteLine("Numberof rows" + numberofrows);

            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    try
                    {
                        sda.Fill(dt);
                        using (XLWorkbook wb = new XLWorkbook())
                        {
                            var ws = wb.Worksheets.Add(dt, "الأعطال");
                            Response.Clear();
                            Response.Buffer = true;
                            Response.Charset = "";
                            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            Response.AddHeader("content-disposition", "attachment;filename=WeeklyReport.xlsx");
                            using (MemoryStream MyMemoryStream = new MemoryStream())
                            {
                                var rngHeaders = ws.Range("A1:L2");
                                ws.Rows().AdjustToContents();
                                ws.Columns().AdjustToContents();
                                rngHeaders.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                                rngHeaders.Style.Font.Bold = true;
                                rngHeaders.Style.Fill.BackgroundColor = XLColor.OrangeRyb;
                                rngHeaders.Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
                                rngHeaders.Style.Border.InsideBorder = XLBorderStyleValues.Thin;
                                rngHeaders.Cell("A1").Value = "اسم الخدمة";
                                rngHeaders.Cell("B1").Value = "التاريخ";
                                rngHeaders.Cell("A2").Value = " ";
                                rngHeaders.Cell("B2").Value = " ";
                                rngHeaders.Range("C1:E1").Merge();
                                rngHeaders.Cell("C1").Value = "وقت الانقطاع"; 
                                rngHeaders.Cell("C2").Value = "من";
                                rngHeaders.Cell("D2").Value = "إلى";
                                rngHeaders.Cell("E2").Value = "المدة";
                                rngHeaders.Range("F1:J1").Merge();
                                rngHeaders.Cell("F1").Value = "الجهةالجهات المتضررة";
                                rngHeaders.Cell("F2").Value = "النشاط";
                                rngHeaders.Cell("G2").Value = "القطاع";
                                rngHeaders.Cell("H2").Value = "منطقة الإعمال";
                                rngHeaders.Cell("I2").Value = "الموقع/المدينة";
                                rngHeaders.Cell("J2").Value = "عدد المستفيدين المتأثرين";
                                rngHeaders.Cell("K1").Value = "وصف المشكلة وأسباب حدوثها";
                                rngHeaders.Cell("L1").Value = "الحل/الإجراء المتبع";
                                rngHeaders.Cell("K2").Value = " ";
                                rngHeaders.Cell("L2").Value = " ";

                                wb.SaveAs(MyMemoryStream);
                                MyMemoryStream.WriteTo(Response.OutputStream);
                                Response.Flush();
                                Response.End();
                            }
                        }
                    }
                    catch (SqlException ex) { }
                }
            }
        }
    }
}
  1. 将数据表放入工作表后,您正在将值写入第 1 行和第 2 行的单元格。在定义 rngHeaders.
  2. 之前使用 ws.FirstRow().InsertRowsAbove(2);
  3. 填写单元格后使用ws.Rows().AdjustToContents();。 ClosedXml 需要知道单元格中的内容以调整它们的宽度(并且您不需要第二次调用 ws.Columns().AdjustToContents();)。