vb.net - How to export/import data from/to datagridview to a Excel file? -
i using microsoft office 2007 , visual studio 2010.
i want save data of datagridview excel file once click button on windows form.
also want load data excel file datagridview clicking button.
pls help...i new vb unable write code. plsss help..
this rather big question , there's number of solutions.
1) writing excel.. there 3 common ways this, 1) write data out csv format , excel happily read it. 2) use excel interop create workbook , write work sheet it. requires excel installed , 3) use oledb create excel.
2) reading excel... again can use excel interop read excel workbooks , again need excel installed, , can use oledb read excel files.
there other ways, these commonly used ones.
on balance approach using oledb in first instance , there's lots of examples on stackoverflow on how read , write excel.
if need access of excel's formatting or charting facilities need interop. it's relatively easy use , again there's lots of examples of how it.
edit:
at it's simple...
first add reference microsoft.office.interop.excel under project properties, then...
imports excel = microsoft.office.interop.excel public class frmexcelexport private sub frmexcelexport_load(sender system.object, e system.eventargs) handles mybase.load dgvdatatoexport.columns.add("c1", "column 1") dgvdatatoexport.columns.add("c2", "column 2") dgvdatatoexport.rows.add("col1-row1", "col2-row1") dgvdatatoexport.rows.add("col1-row2", "col2-row2") end sub private sub btnexporttoexcel_click(sender object, e eventargs) handles btnexporttoexcel.click dim xlapp excel.application = new excel.application xlapp.sheetsinnewworkbook = 1 dim xlworkbook excel.workbook = xlapp.workbooks.add dim xlworksheet excel.worksheet = xlworkbook.worksheets.item(1) xlworksheet.name = "example_export" nrow = 0 dgvdatatoexport.rows.count - 1 ncol = 0 dgvdatatoexport.columns.count - 1 xlworksheet.cells(nrow + 1, ncol + 1) = dgvdatatoexport.rows(nrow).cells(ncol).value next ncol next nrow xlapp.displayalerts = false xlworkbook.saveas("c:\example.xlsx", excel.xlfileformat.xlworkbookdefault, type.missing, type.missing, type.missing, type.missing, _ excel.xlsaveasaccessmode.xlnochange, excel.xlsaveconflictresolution.xllocalsessionchanges) xlworkbook.close() xlapp.quit() end sub end class
but, there's massive amounts of stuff on over internet. adding it
Comments
Post a Comment