asp.net - Export Gridview connected to MYSQL data to excel -
i have simple form created in visual studio (vb) has data gridview connected table in mysql (hosted in remote server).
i have below code export grid view excel takes long time export (around 15 minutes).
the table in mysql small (1000 rows , 60 columns).
is there better way export complete mysql table excel?
please help
code:
dim xlapp microsoft.office.interop.excel.application dim xlworkbook microsoft.office.interop.excel.workbook dim xlworksheet microsoft.office.interop.excel.worksheet dim misvalue object = system.reflection.missing.value dim integer dim j integer xlapp = new microsoft.office.interop.excel.application xlworkbook = xlapp.workbooks.add(misvalue) xlworksheet = xlworkbook.sheets("sheet1") = 0 datagridview1.rows.count - 1 j = 0 datagridview1.columns.count - 1 k integer = 1 datagridview1.columns.count on error resume next xlworksheet.cells(1, k) = datagridview1.columns(k - 1).headertext xlworksheet.cells(i + 2, j + 1) = datagridview1(j, i).value.tostring() next next next xlworksheet.saveas("c:\users\username\desktop\vbexcel.xlsx") xlworkbook.close()
is there 2 ways :
- using rdlc reports.
- using gridview exporting.
first way need created rdlc report, retrieving data datatable, call code:
dim mydatasource reportdatasource = new reportdatasource("reportdataset", mydatatable) reportviewer1.localreport.reportpath = server.mappath("myrdlcreportpath") rvsmartcardsissues.localreport.enableexternalimages = true rvsmartcardsissues.localreport.datasources.clear() rvsmartcardsissues.localreport.datasources.add(mydatasource ) rvsmartcardsissues.localreport.refresh() dim warnings warning() = nothing dim streamids string() = nothing dim mimetype string = nothing dim encoding string = nothing dim extension string = nothing dim bytes byte() bytes = rvsmartcardsissues.localreport.render("excel", nothing, mimetype, encoding, extension, streamids, warnings) httpcontext.current.response.buffer = true httpcontext.current.response.clear() httpcontext.current.response.contenttype = mimetype httpcontext.current.response.addheader("content-disposition", "attachment; filename=exportedfilename.xls") httpcontext.current.response.binarywrite(bytes) httpcontext.current.response.flush() httpcontext.current.response.end()
second way based on function :
public shared sub exportgridviewtoexcelgridview(byval filename string, byref gvr gridview, byref currentpage page) dim htmlform system.web.ui.htmlcontrols.htmlform = new system.web.ui.htmlcontrols.htmlform() currentpage.controls.add(htmlform) htmlform.controls.add(gvr) currentpage.response.clear() currentpage.response.buffer = true currentpage.response.addheader("content-disposition", "attachment; filename=" & filename) currentpage.response.contenttype = "application/vnd.ms-excel" currentpage.response.contentencoding = system.text.encoding.utf8 currentpage.response.charset = "" currentpage.enableviewstate = false using strwriter new stringwriter dim htmlwrt htmltextwriter = new htmltextwriter(strwriter) htmlform.rendercontrol(htmlwrt) htmlwrt.flush() currentpage.response.write(strwriter.tostring) currentpage.response.end() end using end sub
make sure when use second way set gridview paging property false.
Comments
Post a Comment