Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > VB.NET General Discussion

VB.NET General Discussion VB.NET general discussion area

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-18-2009, 10:11 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5
 
Join Date: Dec 2009
Posts: 2
Reputation: 0
Sinikal is on a distinguished programming path ahead
Exclamation Import Tab Delimited File into Excel

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.

Code:
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-18-2009, 11:09 AM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Location: USA
Posts: 875
Reputation: 499
MattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond repute
Default

Renaming an existing sheet.

Code:
xlWS = CType(xlWB.Sheets(1), Excel.Worksheet)
xlWS.Name = "Example"
Inserting a worksheet after the "Example" sheet

Code:
xlWB.Sheets.Add(After:=xlWB.Sheets("Example"))
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-18-2009, 12:43 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5
 
Join Date: Dec 2009
Posts: 2
Reputation: 0
Sinikal is on a distinguished programming path ahead
Default

I want to add the new worksheet to the very end or very beginning of the WorkBook. I shouldn't need to know the worksheet name.

When I try to copy the values into the worksheet I get this error:

"Unable to cast COM object of type 'Excel.WorksheetClass' to interface type 'Excel._Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D8-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

The code snippet is:

Code:
Do While sr.Peek() >= 0
                    values = Split(sr.ReadLine, ControlChars.Tab)
                    'Console.WriteLine(values(1))

                    For i = 0 To values.GetUpperBound(0)
                        xlWorkSheet.Cells(count, i + 1) = values(i) ' ERROR OCCURING
                    Next

                    count = count + 1
                Loop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-20-2009, 10:57 AM
Tom Tom is offline
VB.NET Forum Idol
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2005
Posts: 709
Reputation: 335
Tom master of VB.NETTom master of VB.NETTom master of VB.NETTom master of VB.NETTom master of VB.NETTom master of VB.NETTom master of VB.NETTom master of VB.NETTom master of VB.NETTom master of VB.NET
Default

If you load your tab delimited file into a dataset/datatable you can output it to an excel file using oledb without the need of automating excel.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 3:55 AM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.