Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.ClickĮnd ClassWhat will happen here is that it will load all the info into the DataGridView, I clik on a button, then all the info from the DataGridView gets exported to Excel. ApprenticeDBDataSet.StudentInfo)ĭtX = CType( ApprenticeDBDataSet.StudentInfo, DataTable) Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Range(ExlCell(iX) & Xcounter.ToString()).Value = Xdr(iX).ToString() Range(ExlCell(iX) & Xcounter.ToString()).Value = "" 'check if empty vals from db Range(ExlCell(Xcounter) & "1").ColumnWidth = 50 'width of columns ReDim ExlCell(2) 'only 3 columns in datagridviewĮxlCell(2) = "C" 'column3 - for more columns just add in same sequenceĭim excelApp As New Excel.Application 'new excel appĭim excelBook As Excel.Workbook = 'new excel workbookĭim excelWorksheet As Excel.Worksheet = _ĬType(excelBook.Worksheets(1), Excel.Worksheet) 'new excel worksheetįor Each Xdc As DataColumn In dtX.Columns It doesn't save the info though, it just opens it in Excel but with the excel objects's you can save this into an.
![export datagridview to excel vb net 2010 export datagridview to excel vb net 2010](https://i.ytimg.com/vi/gAgoQwYV5Sk/maxresdefault.jpg)
Note : I've made this sub to on purpose put a value into a column, then the next row and column, etc., just so that you can see what exactly how powerful this sub is.
![export datagridview to excel vb net 2010 export datagridview to excel vb net 2010](https://www.calendarpedia.com/images-large/2022-calendar-p.png)
![export datagridview to excel vb net 2010 export datagridview to excel vb net 2010](https://www.gemboxsoftware.com/spreadsheet/examples/402/content/create-write-excel-file-in-cs-vb.png)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickĮnd SubThe XportToXcel sub, will export everything to to Excel. Cells(i, i - 1).Value = dr.Item(dc.ColumnName) Cells(1, c).Value = dc.ColumnName.ToString Code: Public Sub XportToXcel(ByVal dt As DataTable)ĭim XBook As Excel.Workbook = ĭim XSheet As Excel.Worksheet = CType(XBook.Worksheets(1), Excel.Worksheet)