locked
Export to Excel with Nested Listview RRS feed

  • Question

  • User-1072848465 posted

    I had to update my code from 4.0 to 4.5 and in the process I changed how my nested Listview was accessing data.

    In 4.0, the code used is below and when I exported to excel it worked great.

    <asp:ListView ID="Second_MemberList" runat="server" ItemPlaceholderID="secondTemplate" DataSource='<%# getMemberOfFamily(Convert.ToInt32(Eval("SecondMember"))) %>'>
             <ItemTemplate>
                <%# Eval("firstName") + " " + Eval("lastName")  %>
             </ItemTemplate>
             <LayoutTemplate>
                 <div id="secondTemplate" runat="server"></div>
             </LayoutTemplate>
    </asp:ListView>

    in 4.5 I changed the code to what you see below to model binding and now when I export to excel I get a blank worksheet

    <asp:ListView ID="Second_MemberList" runat="server" ItemPlaceholderID="secondTemplate" ItemType="SherlockMember" SelectMethod="Second_MemberList_GetData">
           <ItemTemplate>
              <%# Item.firstName + " " + Item.lastName %>
           </ItemTemplate>
           <LayoutTemplate>
               <div id="secondTemplate" runat="server"></div>
            </LayoutTemplate>
    </asp:ListView>

    The table that is displaying the data between 4.0 and 4.5 look identical.

    Here is the code I am using to Export To Excel

    protected void exportToExcel_btn_Click(object sender, EventArgs e)
        {
            ExcelListView.Visible = true;
            ExportIntoExcel(ExcelListView, "Families Not Contacted", "familiesnotcontacted");
        }
    
    public void ExportIntoExcel(ListView lvExport, string Header, string FileName)
        {
            try
            {
                System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".xls");
                // If you want to export it into word document just specify  “application/vnd.ms-word” as ContentType. The listview contains will be exported to word document.  
                System.Web.HttpContext.Current.Response.Charset = "";
                System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter stringWrite = new StringWriter();
                stringWrite.Write(Header);
                stringWrite.WriteLine();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                HtmlForm frm = new HtmlForm();
                lvExport.Parent.Controls.Add(frm);
                frm.Controls.Add(lvExport);
                frm.RenderControl(htmlWrite);
                System.Web.HttpContext.Current.Response.Write(stringWrite.ToString());
    
            }
            catch (Exception ex)
            {
            }
            finally
            {
                System.Web.HttpContext.Current.Response.End();
            }
        }

    Does anyone have a remedy for this issue?

    Thanks in advance

    Brad

    Friday, December 23, 2016 2:51 PM

All replies