c# - gridview data export to excel in asp.net -
i tried transfer grid view data excel .... output blank excel sheet.how solve problem?is there code transfer grid view value excel sheet data base?
protected void btnexcel_click1(object sender, eventargs e) { response.clear(); response.buffer = true; response.addheader("content-disposition", "attachment;filename=gridviewexport.xls"); response.charset = ""; response.contenttype = "application/vnd.ms-excel"; stringwriter sw = new stringwriter(); htmltextwriter hw = new htmltextwriter(sw); gvdetails.allowpaging = false; gvdetails.databind(); gvdetails.headerrow.style.add("background-color", "#ffffff"); gvdetails.headerrow.cells[0].style.add("background-color", "green"); gvdetails.headerrow.cells[1].style.add("background-color", "green"); gvdetails.headerrow.cells[2].style.add("background-color", "green"); (int = 0; < gvdetails.rows.count;i++ ) { gridviewrow row = gvdetails.rows[i]; row.backcolor = system.drawing.color.white; row.attributes.add("class", "textmode"); if (i % 2 != 0) { row.cells[0].style.add("background-color", "#c2d69b"); row.cells[1].style.add("background-color", "#c2d69b"); row.cells[2].style.add("background-color", "#c2d69b"); } } string style = @"<style> .textmode { mso-number-format:\@; } </style>"; response.write(style); response.output.write(sw.tostring()); response.end(); }
your sheet blank because string writer in null. here may help
system.web.ui.htmltextwriter htmlwrite = new htmltextwriter(stringwrite); gridview1.rendercontrol(htmlwrite);
here full code
protected void button1_click(object sender, eventargs e) { response.clear(); response.addheader("content-disposition", "attachment; filename=filename.xls"); response.contenttype = "application/vnd.xls"; system.io.stringwriter stringwrite = new system.io.stringwriter(); system.web.ui.htmltextwriter htmlwrite = new htmltextwriter(stringwrite); gridview1.rendercontrol(htmlwrite); response.write(stringwrite.tostring()); response.end(); }
Comments
Post a Comment