Text File to Excel .xlsx

  • Thread starter Thread starter stizalke
  • Start date Start date
S

stizalke

Guest
Hi. its me again. I have a problem with exporting text file to excel. What I means is I need to export from text file format to .xlsx file. Last time I asked about separate text file based on pipeline delimiters. But this time I only want to export without separate any delimiters.

So far this is the code that I tried to export from .txt to .xlsx.

Sub xCelTxt()

'read the text file
Dim fileToRead As String = "OH.txt"
Dim fileStream As StreamReader = File.OpenText(fileToRead)
Dim readContents As String = fileStream.ReadToEnd()

'Create a new instance of Excel
Dim xlApp As Application = New Application
xlApp = CreateObject("Excel.Application")

Dim workbooks As Workbooks = xlApp.Workbooks
Dim workbook As Workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet)
Dim worksheet As Worksheet = CType(workbook.Worksheets(1), Worksheet)

Dim lines() As String = File.ReadAllLines(fileToRead)
Dim i As Integer = 0
Do While (i < lines.Length)
worksheet.Columns("A").Cells(i).Value = lines(i)
i = (i + 1)
Loop

'Save as Excel workbook and Quit Excel
workbook.SaveAs("OH.xlsx", readContents)
workbook.Close()
xlApp.Quit()


End Sub


I only wants the file in .xlsx are exactly same with the .txt file.

Mostly I found the method that will separate or split the text file. :(

I am a very new to this programming.

Please help me T_T

Continue reading...
 
Back
Top