error when exporting to Excel

  • Thread starter Thread starter monemas
  • Start date Start date
M

monemas

Guest
Hi

I have a problem when exporting date from datagrid to Excel.

The project was built in Windows 7 64-bit , office 2007 in pc
The following export code works well on the same device with the previous specifications

Excel is not exported on a different device .

What's wrong ?

Note : I want the code to export to any version of Office 2007,to 2019

error


1514605.png


dll


1514606.png


my code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")

Dim lRow As Long = 0
Dim row As Long = 0
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
Try
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
xlApp = New Excel.Application()
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DATAG_CLIEN.RowCount - 2
For j = 0 To DATAG_CLIEN.ColumnCount - 1
For k As Integer = 1 To DATAG_CLIEN.Columns.Count
xlWorkSheet.Cells(1, k) = DATAG_CLIEN.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DATAG_CLIEN(j, i).Value.ToString()
Next
Next
Next
xlWorkBook.Sheets("Sheet2").Delete()
xlWorkBook.Sheets("Sheet3").Delete()
xlWorkSheet.DisplayRightToLeft = Not xlWorkSheet.DisplayRightToLeft
xlWorkSheet.SaveAs("D:\New folder\" & "excel1.xlsx")
xlWorkBook.Close()
xlApp.Quit()

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub


Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub

Continue reading...
 
Back
Top