Exporting all records from datagridview to Excel VB.NET -
i used code export records excel , works fine, exports 1 row. there anyway record data in datagridview? also, i'm not familiar crystal report in vb want use button click event..
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.applicationclass xlworkbook = xlapp.workbooks.add(misvalue) xlworksheet = xlworkbook.sheets("sheet1") = 0 datagridview1.rowcount - 2 j = 0 datagridview1.columncount - 1 k integer = 1 datagridview1.columns.count 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("d:\vbexcel.xlsx") xlworkbook.close() xlapp.quit() releaseobject(xlapp) releaseobject(xlworkbook) releaseobject(xlworksheet) msgbox("you can find file d:\vbexcel.xlsx") end sub private sub releaseobject(byval obj object) try system.runtime.interopservices.marshal.releasecomobject(obj) obj = nothing catch ex exception obj = nothing gc.collect() end try
i can't comment 1 posted code cause have few reputation...
change for loop
.
'for headers = 1 datagridview1.columncount xlworksheet.cells(1, i) = datagridview1.columns(i - 1).headertext 'for items j = 1 datagridview1.rowcount xlworksheet.cells(j + 1, i) = datagridview1(i - 1, j - 1).value.tostring() next next
Comments
Post a Comment