【问题标题】:Export nested Gridview to Excel error将嵌套的 Gridview 导出到 Excel 错误
【发布时间】:2015-11-26 09:40:01
【问题描述】:

我需要使用从搜索中获得的以下代码将嵌套的 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,而 Gridview grvPayrollDetails 是嵌套的子 Gridview。请帮忙! 我遵循了这个指南:Export Nested GridView (GridView inside GridView) to Excel in ASP.Net

这是嵌套的 Gridview : Nested Gridview

【问题讨论】:

  • 使用调试器 Luke!并检查超出范围的值。顺便说一句,您在哪一行得到了这个异常?
  • 在这一行出现错误:GridView grvPayrollDetails = (GridView)grvPayroll.Rows[1].FindControl("grvPayrollDetails");但我不明白为什么?
  • 那么你的gridview 真的在那一行之前有2行?

标签: c# asp.net excel gridview


【解决方案1】:

使用以下代码更改您的以下行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 应该是 &gt;= 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.Columns 在删除索引为 0 的列之前具有价值

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

【讨论】:

  • 我修改了代码,但仍然出现同样的错误:GridView grvPayrollDetails = (GridView)grvPayroll.Rows[1].FindControl("grvPayrollDetails");
  • 有没有添加row.Cells.Count >= 3个条件
  • 是的,我做到了。但它仍然在以下行出现相同的错误: GridView grvPayrollDetails = (GridView)grvPayroll.Rows[1].FindControl("grvPayrollDetails");
  • 嗨,我修改了代码以更正代码中的错误行
  • 我尝试了你所有的代码,但都失败了。谢谢你的帮助,我尝试了另一种方法,没有发生错误,但我只得到一个没有数据的 excel 文件。请访问此主题,也许您可​​以帮助我:stackoverflow.com/questions/33940893/…
【解决方案2】:

试试这个:

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();
            }

        }

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    相关资源
    最近更新 更多