将嵌套网格视图导出到Excel错误

本文关键字:Excel 错误 嵌套 网格 视图 | 更新日期: 2023-09-27 18:24:23

我需要使用从搜索中获得的以下代码将嵌套的Gridview导出到Excel,但当我单击"导出"按钮时,它引发了一个错误:

中发生类型为"System.ArgumentOutOfRangeException"的异常mscorlib.dll,但未在用户代码中处理

这是代码:

protected void ExportExcel(object sender, EventArgs e)
        {
            DataTable dt = new DataTable("GridView_Data");
            GridView grvPayrollDetails = (GridView)grvPayroll.Rows[1].FindControl("grvPayrollDetails");
            foreach (TableCell cell in grvPayroll.HeaderRow.Cells)
            {
                dt.Columns.Add(cell.Text);
            }
            foreach (TableCell cell in grvPayrollDetails.HeaderRow.Cells)
            {
                dt.Columns.Add(cell.Text);
            }
            dt.Columns.RemoveAt(0);
            foreach (GridViewRow row in grvPayroll.Rows)
            {
                GridView grvPayrollDetailscell = (row.FindControl("grvPayrollDetails") as GridView);
                for (int j = 0; j < grvPayrollDetailscell.Rows.Count; j++)
                {
                    dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text, grvPayrollDetailscell.Rows[j].Cells[0].Text, grvPayrollDetailscell.Rows[j].Cells[1].Text);
                }
            }
            using (XLWorkbook wb = new XLWorkbook())
            {
                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=grvPayrollDetails.xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }
        }

grvPayroll是主网格视图,Gridview grvPayrollDetails是嵌套的子网格视图。请帮忙!我遵循了这个指南:在ASP.Net 中将嵌套的GridView(GridView内部的GridView)导出到Excel

这是嵌套网格视图:嵌套网格视图

将嵌套网格视图导出到Excel错误

用下面的代码更改下面的行GridView grvPayrollDetails = (GridView)grvPayroll.Rows[1].FindControl("grvPayrollDetails");

GridView grvPayrollDetails = null;
foreach (GridViewRow row in grvPayroll.Rows)
{
    if (row.HasControls())
    {
        foreach (Control ctrl in row.Controls)
        {
            if (ctrl.ID == "grvPayrollDetails" && grvPayrollDetails != null)
            {
                grvPayrollDetails = (GridView)ctrl;
                break;
            }
        }
        if (grvPayrollDetails != null)
        {
            break;
        }
    }
}

您应该修改for循环以运行count-1,并使用if条件来检查row.Cells应该是>= 3

for (int j = 0; j < grvPayrollDetailscell.Rows.Count - 1; j++)
{
    if(row.Cells.Count >= 3)
    {
                    dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text, grvPayrollDetailscell.Rows[j].Cells[0].Text, grvPayrollDetailscell.Rows[j].Cells[1].Text);

还要确保你的dt。列在删除0索引处的列之前具有值

if(dt.Columns.Count > 0)
{
    dt.Columns.RemoveAt(0);
}

试试这个:

public override void VerifyRenderingInServerForm(Control control)
        {
            /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
               server control at run time. */
        }
        protected void ExportExcel(object sender, EventArgs e)
        {
            grvPayroll.AllowPaging = false;
            var grvPayrollDetails = new GridView();
            for (var i = 0; i < grvPayroll.Rows.Count; i++)
            {
                grvPayrollDetails = (GridView)grvPayroll.Rows[i].FindControl("grvPayrollDetails");
                grvPayrollDetails.AllowPaging = false;
                BindGrid(SortField);
            }
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=PayrollExport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                grvPayrollDetails.AllowPaging = false;
                this.BindGrid(SortField);
                grvPayrollDetails.Font.Name = "Times New Roman";
                grvPayrollDetails.BackColor = Color.Transparent;
                grvPayrollDetails.GridLines = GridLines.Both;
                grvPayrollDetails.RenderControl(hw);
                Response.Write(Regex.Replace(sw.ToString(), "(<a[^>]*>)|(</a>)", " ", RegexOptions.IgnoreCase));
                Response.Flush();
                Response.End();
            }
        }