c# - one column in GridView won't export to excel sheet -
i have gridview displaying fields database , function export gridview excel spreadsheet column not displaying litchecklistno.
<asp:gridview runat="server" <columns> <asp:boundfield datafield="vehiclereg" headertext="registration" sortexpression="vehiclereg"></asp:boundfield> <asp:templatefield headertext="checklist number" sortexpression="checklistno"> <itemtemplate> <asp:literal id="litchecklistno" runat="server"> </asp:literal> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="checklistdate" headertext="checklist date" sortexpression="checklistdate" dataformatstring="{0:dd/mm/yyyy}"></asp:boundfield> <asp:boundfield datafield="maintenanceno" headertext="maintenance no" sortexpression="maintenanceno"></asp:boundfield> </columns> </asp:gridview>
the code export gridview:
protected void toexcel(gridview grid, string name, string filename) { if (!filename.contains(".xls")) { filename = filename + ".xls"; } string style = "<style><!--table@page{mso-header-data:\"&c" + name + " date/: &d/ page &p\"; mso-page-orientation:landscape; mso-page-scale:89;} br {mso-data-placement:same-cell;} --></style>"; response.buffer = true; response.contenttype = "application/vnd.ms-excel"; response.addheader("content-disposition", "attachment; filename=" + filename + ""); this.enableviewstate = false; response.write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">"); response.write("<head>"); response.write(style); response.write("<meta http-equiv=\"content-type\" content=\"text/html;charset=windows-1252\">"); response.write("<!--[if gte mso 9]>"); response.write("<xml>"); response.write("<x:excelworkbook>"); response.write("<x:excelworksheets>"); response.write("<x:excelworksheet>"); response.write("<x:name>" + name + " table</x:name>"); response.write("<x:worksheetoptions>"); response.write("<x:panes>"); response.write("</x:panes>"); response.write("<x:print>"); response.write("<x:validprinterinfo/>"); response.write("<x:scale>89</x:scale>"); response.write("</x:print>"); response.write("</x:worksheetoptions>"); response.write("</x:excelworksheet>"); response.write("</x:excelworksheets>"); response.write("</x:excelworkbook>"); response.write("</xml>"); response.write("<![endif]-->"); response.write("</head>"); response.write("<body>"); system.io.stringwriter ostringwriter = new system.io.stringwriter(); system.web.ui.htmltextwriter ohtmltextwriter = new system.web.ui.htmltextwriter(ostringwriter); clearcontrols(grid); grid.renderbegintag(ohtmltextwriter); grid.headerrow.rendercontrol(ohtmltextwriter); foreach (gridviewrow row in grid.rows) { row.rendercontrol(ohtmltextwriter); } grid.footerrow.rendercontrol(ohtmltextwriter); grid.renderendtag(ohtmltextwriter); response.write(ostringwriter.tostring()); response.write("</body>"); response.write("</html>"); response.end(); }
litchecklistno link in gridview opens page when clicked why i'm not using boundfield in rest of gridview why isn't being exported spreed sheet? literal?
have @ post. having trouble exporting gridview excel well. using masterpage export page using masterpage.
follow link. please note vb.net, can convert code c#.
exporting gridview excel vb.net
hope helps.
Comments
Post a Comment