Hi!
Error occured in my codes.
Is there a way to cast DataTable to Excel.DataTable?
Here is the codes for reference:
Dim f As New FolderBrowserDialog
Try
If f.ShowDialog() = DialogResult.OK Then
System.Threading.Thread.CurrentThread.CurrentCulture = _
System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oApp As Excel.Application
oExcel = CreateObject("Excel.Application")
oApp = GetObject(, "Excel.Application")
oBook = oExcel.Workbooks.Add(Type.Missing)
oSheet = oBook.Worksheets(1)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
Dim DTB As DataTable = New System.Data.DataTable
grdvwAmortization.DataSource = DTB
For Each dc In DTB.Columns
colIndex = colIndex + 1
oSheet.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In DTB.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In DTB.Columns
colIndex = colIndex + 1
oSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
Dim fileName As String = "\ExportedAuthors" + ".xls"
Dim finalPath = f.SelectedPath + fileName
oSheet.Columns.AutoFit()
oBook.SaveAs(finalPath, XlFileFormat.xlWorkbookNormal, Type.Missing, _
Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, _
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
ReleaseObject(oSheet)
oBook.Close(False, Type.Missing, Type.Missing)
ReleaseObject(oBook)
oExcel.Quit()
ReleaseObject(oExcel)
GC.Collect()
MessageBox.Show("Export done successfully!")
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK)
End Try
Thank you so much!
This is the last error for my codes.