c# - Export Listview to Excel (troubleshoot) -
i working on web application , need able export listview excel. listview bound (i can see data), export function below isn't working. function seems tripping on <asp:linkbutton>
, <asp:imagebutton>
. aware of way around this?
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"); 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) { } { system.web.httpcontext.current.response.end(); } } protected void export_click(object sender, eventargs e) { if (paidlv.visible == true) { exportintoexcel(paidlv, "income summary", "incomesummary"); } }
update: here listview in entirety can see <asp:linkbutton>s
, <asp:imagebutton>s
.
<asp:listview id="paidlv" runat="server" onpagepropertieschanged="paidlv_pagepropertieschanged" ondatabound="paidlv_databound" onsorting="paidlv_sorting" visible="true"> <layouttemplate> <table class="detail"> <tr> <th> <asp:linkbutton id="lnkdate" runat="server" commandname="sort" commandargument="pmtpaydate1">date</asp:linkbutton> <asp:image runat="server" id="sortrevimage5" imageurl="~/img/arrows.png" visible="false" /> </th> <th> <asp:linkbutton id="lnktname" runat="server" commandname="sort" commandargument="tname">transaction name</asp:linkbutton> <asp:image runat="server" id="sortrevimage2" imageurl="~/img/arrows.png" visible="false" /> </th> <th> <asp:linkbutton id="lnkcname" runat="server" commandname="sort" commandargument="cname">client name</asp:linkbutton> <asp:image runat="server" id="sortrevimage1" imageurl="~/img/arrows.png" visible="false" /> </th> <th> <asp:linkbutton id="lnkhalf" runat="server" commandname="sort" commandargument="pmtdesc1">portion</asp:linkbutton> <asp:image runat="server" id="sortrevimage3" imageurl="~/img/arrows.png" visible="false" /> </th> <th> <asp:linkbutton id="lnkamt" runat="server" commandname="sort" commandargument="pmtamt">amount</asp:linkbutton> <asp:image runat="server" id="sortrevimage4" imageurl="~/img/arrows.png" visible="false" /> </th> <th>delete</th> </tr> <tr id="itemplaceholder" runat="server"></tr> </table> <div class="pager"> <asp:datapager id="paidpager" runat="server" pagesize="20" pagedcontrolid="paidlv"> <fields> <asp:nextpreviouspagerfield shownextpagebutton="false" buttoncssclass="previousnextlink" /> <asp:numericpagerfield buttoncount="10" buttontype="link" numericbuttoncssclass="numericlink" /> <asp:nextpreviouspagerfield showpreviouspagebutton="false" buttoncssclass="previousnextlink" /> </fields> </asp:datapager> </div> </layouttemplate> <itemtemplate> <tr class="altrow rowcolor"> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lbldate"><%#eval("pmtdate", "{0:d}") %></asp:label> </td> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lbltname"><%#eval("tname") %></asp:label> </td> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lblcname"><%#eval("cname") %></asp:label> </td> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lblhalf"><%#eval("half").tostring() == "1" ? "first" : eval("half").tostring() == "2" ? "second" : "full" %></asp:label> </td> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lblamt"><%#eval("pmtamt", "{0:c}") %></asp:label> </td> <td> <asp:imagebutton runat="server" id="btndelete" imageurl="~/img/delete.png" onclick="btndelete_click" postbackurl="~/transaction.aspx" cssclass="deletebutton" commandargument='<%#eval("transactionid") %>' /> </td> <asp:hiddenfield runat="server" id="lblrevid" value='<%#eval("transactionid") %>' /> </tr> </itemtemplate> <alternatingitemtemplate> <tr class="rowcolor"> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lbldate"><%#eval("pmtdate", "{0:d}") %></asp:label> </td> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lbltname"><%#eval("tname") %></asp:label> </td> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lblcname"><%#eval("cname") %></asp:label> </td> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lblhalf"><%#eval("half").tostring() == "1" ? "first" : eval("half").tostring() == "2" ? "second" : "full" %></asp:label> </td> <td class="valigntop" onclick="donav('<%# eval("transactionid","bookdeal.aspx?tid={0}") %>');"> <asp:label runat="server" id="lblamt"><%#eval("pmtamt", "{0:c}") %></asp:label> </td> <td> <asp:imagebutton runat="server" id="btndelete" imageurl="~/img/delete.png" onclick="btndelete_click" postbackurl="~/transaction.aspx" cssclass="deletebutton" commandargument='<%#eval("transactionid") %>' /> </td> <asp:hiddenfield runat="server" id="lblrevid" value='<%#eval("transactionid") %>' /> </tr> </alternatingitemtemplate> <emptydatatemplate> <table id="empty"> <tr> <td id="tdempty"> <br /> sorry, no data found </td> </tr> </table> </emptydatatemplate> </asp:listview>
you try data
private void datatabletoexcel(datatable datatable) { stringwriter writer = new stringwriter(); htmltextwriter htmlwriter = new htmltextwriter(writer); gridview gridview = new gridview(); gridview.datasource = datatable; gridview.autogeneratecolumns = true; gridview.databind(); gridview.rendercontrol(htmlwriter); htmlwriter.close(); response.clear(); response.addheader("content-disposition", "attachment;filename=filename.xls"); response.charset = ""; response.write(writer.tostring()); response.end(); }
Comments
Post a Comment