If all you want to do is put the text file data into an Excel file then just open the text file in excel and use the SaveAs method on the WorkBook.
Code:Imports Excel = Microsoft.Office.Interop.Excel Imports Microsoft.Office Public Class Form9 Public Sub OpenExcelDemo(ByVal FileName As String) If IO.File.Exists(FileName) Then Dim xlApp As Excel.Application = Nothing Dim xlWorkBook As Excel.Workbook = Nothing xlApp = New Excel.Application xlApp.DisplayAlerts = False xlWorkBook = xlApp.Workbooks.Open(FileName) xlApp.Visible = True 'MessageBox.Show("File was opened") xlWorkBook.SaveAs(Filename:="C:\Ajunk2015\TestSaveAs.xlsx", FileFormat:=Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook) xlApp.Quit() ReleaseComObject(xlWorkBook) ReleaseComObject(xlApp) Else MessageBox.Show("'" & FileName & "' not located. Try one of the write examples first.") End If End Sub Public Sub ReleaseComObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing End Try End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click OpenExcelDemo("C:\ajunk2015\book1.txt") End Sub End Class




Reply With Quote
