How to Save excel file in vb.net -
previously , trying export gridview value excel. code given below able export data excel. still not able save automatically excel file fixed folder suppose in c:/ drive. code have written export excel given below.
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
can on here please me out problem how save excel file automatically in vb.net??
the saveas method defined excel.workbook
at end of try
, before catch
, write :
excelbook.saveas(<some path here>, etc...)
refers here more informations.
and exit excel, write in finally
block @ start :
xlapp.workbooks.close() xlapp.quit()
Comments
Post a Comment