Exported excel file from datagridview shows error when opened VB

Nek

I'm doing an export in VB. I was able to export the values of the datagridview to excel but when I open the file, it shows this error.

Error

I'm using the following code to export the values of the datagridview.

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 sPath As String = String.Empty
    Dim dlgSave As New SaveFileDialog
    Dim i As Integer
    Dim j As Integer

    dlgSave.DefaultExt = "xls"
    dlgSave.Filter = "Microsoft Excel|*.xls"

    If dlgSave.ShowDialog = System.Windows.Forms.DialogResult.OK Then
        xlApp = New Microsoft.Office.Interop.Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")


        For i = 0 To dgvFile.RowCount - 1
            For j = 0 To dgvFile.ColumnCount - 1
                For k As Integer = 1 To dgvFile.Columns.Count
                    xlWorkSheet.Cells(1, k) = dgvFile.Columns(k - 1).HeaderText
                    xlWorkSheet.Cells(i + 2, j + 1) = dgvFile(j, i).Value.ToString()
                Next
            Next
        Next

        Dim sFileName As String = Replace(dlgSave.FileName, ".xlsx", ".xlx")

        xlWorkSheet.SaveAs(sFilename)
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        Dim res As MsgBoxResult
        res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
        If (res = MsgBoxResult.Yes) Then
            Process.Start(sFileName)
        End If
    End If

NOTE This code works fine. It exports the values without any problem. It just shows the error when I open the excel file. The exported file has no problem when I click "Yes". It shows the correct values. I just want this error to stop showing when I open the file.

Can anyone help me fix this solution? I will be truly grateful to anyone who can lead me. Thanks. :)

Nek

I figured it out. Thanks to akhil kumar.

My Excel uses .xlsx extension so I changed

dlgSave.DefaultExt = "xls"

to

dlgSave.DefaultExt = "xlsx"

and removed the Replace function to make it look like this.

Dim sFileName As String = dlgSave.FileName

This works perfectly. This problem was sorted. :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

DataGridView need to be exported into an excel file

From Dev

Store Excel file exported from Pandas in AWS

From Dev

Excel file generated by table2excel jQuery plugin throws error when opened

From Dev

Writing to a file with QFile fails, without error code, when already opened in Excel

From Dev

from DataGridView saving to text file issue when saving date values on vb.net

From Dev

How to correctly format an alpha-numerical in an exported csv file opened in excel

From Dev

Error in fsproj file prevents it from being opened

From Dev

Import Excel file to Datagridview in C# or VB.Net

From Dev

VB.NET get path from the file the user opened

From Dev

VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

From Dev

VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

From Dev

getting data from excel file to datagridview

From Dev

Code only runs on Excel 2007 if file is opened from within Excel

From Dev

Exporting data from DataGridView as Excel table using VB.NET

From Dev

How to open a excel file that has been exported from java arraylist

From Dev

File.ReadAllLines() fails to read from a file that is opened by Excel

From Dev

Why do I get error when replacing datagridview values in vb?

From Dev

Error "Child list for field [Sheet1$] cannot be created" when loading data from excel to datagridview

From Dev

VB.net Find And Replace from Data in a DataGridView in a text file

From Dev

Auto MsgBox when Excel file is opened and a condition is met

From Dev

Update excel file when opened if a new version is available

From Dev

ARKit - Error loading DAE file exported from Maya

From Dev

Error when update data from DataGridView

From Dev

Include links in exported excel file

From Dev

VB - DataGridView Hour column shows as decimal and Update DataGridView

From Dev

Set PowerPoint presentation when already opened (From Excel)

From Dev

Bring Access Window On Top When Opened From Excel

From Dev

Cognos 10.2.2: Excel Report Redirects To Portlet When Opened from Portlet

From Dev

HTML not working when opened from file, works from ftp server

Related Related

  1. 1

    DataGridView need to be exported into an excel file

  2. 2

    Store Excel file exported from Pandas in AWS

  3. 3

    Excel file generated by table2excel jQuery plugin throws error when opened

  4. 4

    Writing to a file with QFile fails, without error code, when already opened in Excel

  5. 5

    from DataGridView saving to text file issue when saving date values on vb.net

  6. 6

    How to correctly format an alpha-numerical in an exported csv file opened in excel

  7. 7

    Error in fsproj file prevents it from being opened

  8. 8

    Import Excel file to Datagridview in C# or VB.Net

  9. 9

    VB.NET get path from the file the user opened

  10. 10

    VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

  11. 11

    VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

  12. 12

    getting data from excel file to datagridview

  13. 13

    Code only runs on Excel 2007 if file is opened from within Excel

  14. 14

    Exporting data from DataGridView as Excel table using VB.NET

  15. 15

    How to open a excel file that has been exported from java arraylist

  16. 16

    File.ReadAllLines() fails to read from a file that is opened by Excel

  17. 17

    Why do I get error when replacing datagridview values in vb?

  18. 18

    Error "Child list for field [Sheet1$] cannot be created" when loading data from excel to datagridview

  19. 19

    VB.net Find And Replace from Data in a DataGridView in a text file

  20. 20

    Auto MsgBox when Excel file is opened and a condition is met

  21. 21

    Update excel file when opened if a new version is available

  22. 22

    ARKit - Error loading DAE file exported from Maya

  23. 23

    Error when update data from DataGridView

  24. 24

    Include links in exported excel file

  25. 25

    VB - DataGridView Hour column shows as decimal and Update DataGridView

  26. 26

    Set PowerPoint presentation when already opened (From Excel)

  27. 27

    Bring Access Window On Top When Opened From Excel

  28. 28

    Cognos 10.2.2: Excel Report Redirects To Portlet When Opened from Portlet

  29. 29

    HTML not working when opened from file, works from ftp server

HotTag

Archive