I need help taking a tab-delimited text file and dumping it into a new worksheet in Excel. I get a ton of errors when I attempt to name the WorkSheet or add it to the WorkBook. Any suggestions? I want to be able to control the name of the new worksheet.
VB.NET:
Private Sub btnMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMerge.Click
Dim err As Boolean = False
Try
If Not File.Exists(tbTextFile.Text) Then
MsgBox("Text File doesn't exist")
err = True
End If
If Not File.Exists(tbExcelFile.Text) Then
Dim xlApp As Excel.Application = New Excel.Application
xlApp.Workbooks.Add()
xlApp.Workbooks(1).SaveAs(tbExcelFile.Text)
xlApp.Workbooks(1).Close()
xlApp.Quit()
End If
If Not err Then
Dim fs As FileStream = New FileStream(tbTextFile.Text.Trim, FileMode.Open, FileAccess.Read)
Dim sr As StreamReader = New StreamReader(fs)
Dim values As String()
Dim xlApp As Excel.Application = New Excel.Application
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(tbExcelFile.Text.Trim)
Dim xlWorkSheet As Excel.Worksheet = New Excel.Worksheet
Do While sr.Peek() >= 0
values = Split(sr.ReadLine, ControlChars.Tab)
'Console.WriteLine(values(1))
For i = 0 To values.GetUpperBound(0)
Console.WriteLine(values(i))
'xlWorkSheet.Name = "testme"
'xlWorkSheet.Cells(1, i + 1) = values(i)
Next
Loop
xlWorkBook.Worksheets.Add(xlWorkSheet)
xlWorkBook.Close()
xlApp.Quit()
End If
Catch ex As Exception
Throw ex
End Try
End Sub