asp.net mvc - How to export to pdf, word, excel file -
i use asp.net mvc 3. have these 2 requirement. frist 1 creating invoice in application. want export datas pdf , word , excel file. downloaded itextsharp dll, can tel me there anyother alternative datas in ui pdf, word , excel document? second need print document after clicking print button. how connect printer print button in exported document ?
you can use snippet.
this one great. take on it.
this example of usage:
html
<asp:gridview id="gridview1" runat="server" autogeneratecolumns = "false" font-names = "arial" font-size = "11pt" alternatingrowstyle-backcolor = "#c2d69b" headerstyle-backcolor = "green" allowpaging ="true" onpageindexchanging = "onpaging" > <columns> <asp:boundfield itemstyle-width = "150px" datafield = "customerid" headertext = "customerid" /> <asp:boundfield itemstyle-width = "150px" datafield = "city" headertext = "city"/> <asp:boundfield itemstyle-width = "150px" datafield = "country" headertext = "country"/> <asp:boundfield itemstyle-width = "150px" datafield = "postalcode" headertext = "postalcode"/> </columns> </asp:gridview>
c# pdf example
protected void btnexportpdf_click(object sender, eventargs e) { response.contenttype = "application/pdf"; response.addheader("content-disposition", "attachment;filename=gridviewexport.pdf"); response.cache.setcacheability(httpcacheability.nocache); stringwriter sw = new stringwriter(); htmltextwriter hw = new htmltextwriter(sw); gridview1.allowpaging = false; gridview1.databind(); gridview1.rendercontrol(hw); stringreader sr = new stringreader(sw.tostring()); document pdfdoc = new document(pagesize.a4, 10f,10f,10f,0f); htmlworker htmlparser = new htmlworker(pdfdoc); pdfwriter.getinstance(pdfdoc, response.outputstream); pdfdoc.open(); htmlparser.parse(sr); pdfdoc.close(); response.write(pdfdoc); response.end(); }
c# excel example
protected void btnexportexcel_click(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); gridview1.allowpaging = false; gridview1.databind(); //change header row white color gridview1.headerrow.style.add("background-color", "#ffffff"); //apply style individual cells gridview1.headerrow.cells[0].style.add("background-color", "green"); gridview1.headerrow.cells[1].style.add("background-color", "green"); gridview1.headerrow.cells[2].style.add("background-color", "green"); gridview1.headerrow.cells[3].style.add("background-color", "green"); (int = 0; < gridview1.rows.count;i++ ) { gridviewrow row = gridview1.rows[i]; //change color white row.backcolor = system.drawing.color.white; //apply text style each row row.attributes.add("class", "textmode"); //apply style individual cells of alternating row 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"); row.cells[3].style.add("background-color", "#c2d69b"); } } gridview1.rendercontrol(hw); //style format numbers string string style = @"<style> .textmode { mso-number-format:\@; } </style>"; response.write(style); response.output.write(sw.tostring()); response.flush(); response.end(); }
Comments
Post a Comment