ASP.NET将带有动态列的gridview导入excel

时间:2018-05-15 09:31:22

标签: asp.net excel

尽管有关gridview导入的主题数量,我还没有找到解决问题的方法,希望你能帮到我。实际上,我尝试使用按钮将gridview动态生成导出为ex​​cel但是当我点击按钮时没有任何附加但是使用gridview预设相同的代码工作正常:

            InitialiserGrid();

        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment;filename=TableauAnnalyse.xls");
        Response.ContentType = "application/excel";
        Response.ContentEncoding = System.Text.Encoding.Unicode;
        Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());


        StringWriter stringwriter = new StringWriter();
        HtmlTextWriter htmtextwriter = new HtmlTextWriter(stringwriter);

        GridViewAnnalyse.RenderControl(htmtextwriter);
        Response.Write(stringwriter.ToString());
        Response.End();

生成列的代码:

int k = 0;
        while (k < GridViewSite.Rows.Count)
        {
            TemplateField site = new TemplateField
            {
                HeaderText = GridViewSite.Rows[k].Cells[1].Text
            };
            GridViewAnnalyse.Columns.Add(site);
            k++;
        }

        int n = 0;
        while (n < GridViewAnnalyse.Rows.Count)
        {
            int m = 3;
            while (m < GridViewAnnalyse.Rows[n].Cells.Count)
            {
                int p = 0;
                while (p < GridViewSite.Rows.Count)
                {
                    if (GridViewAnnalyse.Columns[m].HeaderText == GridViewSite.Rows[p].Cells[1].Text)
                    {
                        int o = 0;
                        while (o < GridViewAnnalyseS.Rows.Count)
                        {
                            if (GridViewSite.Rows[p].Cells[0].Text == GridViewAnnalyseS.Rows[o].Cells[2].Text)
                            {
                                if (GridViewAnnalyseS.Rows[o].Cells[4].Text == GridViewAnnalyse.Rows[n].Cells[0].Text)
                                {
                                    GridViewAnnalyse.Rows[n].Cells[m].Text = GridViewAnnalyseS.Rows[o].Cells[0].Text;
                                }
                            }
                            o++;
                        }
                    }
                    p++;
                }
                m++
           }
           n++
       }

感谢您的帮助

0 个答案:

没有答案