将嵌套的Gridview导出为Excel错误

时间:2015-11-26 09:40:01

标签: c# asp.net excel gridview

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

这是嵌套的Gridview: Nested Gridview

2 个答案:

答案 0 :(得分:1)

试试这个:

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

        }

答案 1 :(得分:0)

使用以下代码

更改以下行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);

还可以在删除0索引处的列

之前使dt.Columns具有值
if(dt.Columns.Count > 0)
{
    dt.Columns.RemoveAt(0);
}