Asked by:
Export to Excel with Nested Listview

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
-
User1324658857 posted
hi buddy,
Please try to add datasource in listview, hope this link could help you:
Monday, December 26, 2016 8:59 AM -
User-1072848465 posted
aliceyoung, sorry I may have not been detailed enough in my post. The code that I posted is generating the data and displaying it correctly on the page. It is when I export the table that contains the data to Excel when I get a blank worksheet. It is perplexing that the data is not being transferred to Excel.
Tuesday, December 27, 2016 2:50 PM -
User1324658857 posted
Hi
please refer to this link: https://www.codeproject.com/Questions/514997/listviewplusdatapluswriteplustoplusexcelplususingp
Friday, December 30, 2016 11:26 AM