excel - export gridview value into .xlsx file in vb.net -
i want create application can export desired gridview result new excel file. if file same name exists ask give new name else create new file.
try out
private sub buttonexport_click(byval sender system.object, byval e system.eventargs) handles buttonexport.click dim rowstotal, colstotal short dim i, j, ic short system.windows.forms.cursor.current = system.windows.forms.cursors.waitcursor dim xlapp new excel.application try dim excelbook excel.workbook = xlapp.workbooks.add dim excelworksheet excel.worksheet = ctype(excelbook.worksheets(1), excel.worksheet) xlapp.visible = true rowstotal = datagridview1.rowcount - 1 colstotal = datagridview1.columns.count - 1 excelworksheet .cells.select() .cells.delete() ic = 0 colstotal .cells(1, ic + 1).value = datagridview1.columns(ic).headertext next = 0 rowstotal - 1 j = 0 colstotal .cells(i + 2, j + 1).value = datagrid1.rows(i).cells(j).value next j next .rows("1:1").font.fontstyle = "bold" .rows("1:1").font.size = 10 .cells.columns.autofit() .cells.select() .cells.entirecolumn.autofit() .cells(1, 1).select() end catch ex exception msgbox("export excel error " & ex.message) 'release alloacted resources system.windows.forms.cursor.current = system.windows.forms.cursors.default xlapp = nothing end try end sub
Comments
Post a Comment