[Solved] Excel Error: The file format and the extension of .xls dont match

thewall813
 
on Sep 16, 2021 11:37 PM
380 Views

I am trying to maintain simple log file on button click on form.

I am able to do perfectly but there is one error when opening log excel file.

Its is saying that "The file format and the extension of .xls don't match."

My issue is only the error message which i am getting when try to open that excel file

I am opening it in office 2016

Download FREE API for Word, Excel and PDF in ASP.Net: Download
thewall813
 
on Sep 20, 2021 03:53 AM

 

Dim xlapp As Object
xlapp = CreateObject("Excel.Application")

Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet

Dim FilePath As String = "C:\1.xls"


If System.IO.File.Exists(FilePath) Then
	xlWorkBook = xlapp.workbooks.open(FilePath, False)
Else
	xlWorkBook = xlapp.workbooks.add()
End If


If String.IsNullOrEmpty(xlWorkBook.Path) Then
	xlWorkBook.SaveAs(FilePath, Excel.XlFileFormat.xlWorkbookNormal)
End If

If xlWorkBook.Sheets.Count() < 1 Then
	xlWorkSheet = CType(xlWorkBook.Worksheets.Add(), Excel.Worksheet)
	xlWorkSheet.Name = "Log Sheet"
Else
	xlWorkSheet = xlWorkBook.Worksheets(1)
End If

Dim SheetRow As Integer = 0

	For SheetRow = 1 To xlWorkSheet.Rows.Count
		If Trim(xlWorkSheet.Cells(SheetRow, 1).value) = "" Then
					xlWorkSheet.Name = "Log Sheet"

                        xlApp.Cells(1, 1) = "File access path "
                        xlApp.Cells(1, 2) = "File Name"
                        xlApp.Cells(1, 3) = "File Access By User"
                        xlApp.Cells(1, 4) = "Date"
                        xlApp.Cells(1, 5) = "Time"

                        xlWorkSheet.Columns.Range("A1:E1").Font.Bold = True
                        xlWorkSheet.Columns.Range("A1:E1").Font.Color = Color.Blue
                        xlWorkSheet.Columns.Range("A1:E1").Font.Size = 15

                        Exit For
                    End If
Next

  Dim iRow As Integer = 0
                Dim iCol As Integer = 0

                For iRow = 3 To xlWorkSheet.Rows.Count
                    If Trim(xlWorkSheet.Cells(iRow, 1).value) = "" Then

                        xlWorkSheet.Cells(iRow, 1).Value = sDirectory
                        xlWorkSheet.Cells(iRow, 2).Value = sFileName

                        Dim AccessDate As String = String.Empty
                        Dim AccessTime As String = String.Empty

                        AccessDate = AccessDate.Trim & DateTime.Now.Day
                        AccessDate = AccessDate.Trim & "-" & DateTime.Now.ToString("MMMM")
                        AccessDate = AccessDate.Trim & "-" & DateTime.Now.Year & " "

                        AccessTime = AccessTime.Trim & DateTime.Now.Hour
                        AccessTime = AccessTime.Trim & ":" & DateTime.Now.Minute & " "
                        AccessTime = AccessTime.Trim & " " & DateTime.Now.ToString("tt")


                        xlWorkSheet.Cells(iRow, 3).Value = My.Settings.timeSheetLoginUserType
                        xlWorkSheet.Cells(iRow, 4).Value = AccessDate
                        xlWorkSheet.Cells(iRow, 5).Value = AccessTime



                        Exit For        ' BAIL OUT IF REACHED THE LAST ROW.
                    Else
                        
                    End If

                Next

xlWorkBook.Save()
xlApp.DisplayAlerts = False

xlWorkBook.Close()

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlWorkSheet = Nothing

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
xlWorkBook = Nothing

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing