You are on page 1of 1

Public Sub Create_ExcelFile_InCsvFormat(ByVal ExcelFileName As String,

ByVal CsvFileName As String)

If IO.File.Exists(ExcelFileName) Then
Dim xlApp As Excel.Application = Nothing
Dim xlWorkBooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim xlWorkSheet As Excel.Worksheet = Nothing

xlApp = New Excel.Application


xlApp.DisplayAlerts = False

xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(ExcelFileName)

xlWorkSheet = CType(xlWorkBook.ActiveSheet, Excel.Worksheet)

xlWorkBook.SaveAs(CsvFileName, FileFormat:=Excel.XlFileFormat.xlCSVWindows)

xlWorkBook.Close()

xlApp.UserControl = True
xlApp.Quit()

If Not xlWorkSheet Is Nothing Then


Marshal.FinalReleaseComObject(xlWorkSheet)
xlWorkSheet = Nothing
End If

If Not xlWorkBook Is Nothing Then


Marshal.FinalReleaseComObject(xlWorkBook)
xlWorkBook = Nothing
End If

If Not xlWorkBooks Is Nothing Then


Marshal.FinalReleaseComObject(xlWorkBooks)
xlWorkBooks = Nothing
End If

If Not xlApp Is Nothing Then


Marshal.FinalReleaseComObject(xlApp)
xlApp = Nothing
End If
Else
MessageBox.Show("Failed to located " & ExcelFileName)
End If
End Sub

You might also like