使用datapager进行Listview控制:如何导出到Excel并获取所有数据

时间:2014-03-18 16:44:42

标签: c# excel listview sqldatasource datapager

我遇到的问题是将所有数据从带有datapager的Listview控件导出到excel。这是我的代码:

    private void ExportListView()
    {

        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("content-disposition", "attachment;filename=sample.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";

        StringBuilder sb = new StringBuilder();
        StringWriter objStringWriter = new StringWriter(sb);
        HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);

        //gvSample is Gridview server control
        HtmlForm frm = new HtmlForm();
        ListView1.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(ListView1);

        DataPager pager = (DataPager)ListView1.FindControl("DataPager1");
        pager.PageSize = 1000000;
        frm.RenderControl(objHtmlTextWriter);

        Response.ContentEncoding = Encoding.Unicode;
        Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
        Response.Write(objStringWriter);
        Response.End();


        //string attachment = "attachment; filename=Data.xls";
        //Response.ClearContent();
        //Response.AddHeader("content-disposition", attachment);
        //Response.ContentType = "application/ms-excel";
        //StringWriter sw = new StringWriter();
        //HtmlTextWriter htw = new HtmlTextWriter(sw);



        //// Create a form to contain the grid


        //HtmlForm frm = new HtmlForm();
        //ListView1.Parent.Controls.Add(frm);
        //frm.Attributes["runat"] = "server";
        //frm.Controls.Add(ListView1);


        //frm.RenderControl(htw);
        //Response.Write(sw.ToString());
        //Response.End();
    }

任何有关如何做的信息的人都会很棒。分页是使这不可能的原因。我需要以某种方式告诉数据采集器禁用或最大化所有数据。我只是卡住了。 Gridview有一百万个例子,但ListView没有,我想知道是否有人有这个答案。请帮忙!!谢谢!

0 个答案:

没有答案