Combine csv file with existing excel file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rizzsid11
    New Member
    • Nov 2011
    • 15

    Combine csv file with existing excel file.

    My application (I'm using vb.net 2008)generates an excel file with login details and a csv file. How do I add the csv contents to the open excel file? If I can get the hang of this then I can combine six csv file with the ecel file
    Code:
    rivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim oExcel As Object
            Dim oBook As Object
            Dim oSheet As Object
    
            oExcel = CreateObject("Excel.Application")
            oBook = oExcel.Workbooks.Open("J:\vb\RizRandom\RizRandom\UBM.xlsx") ' Add
            oExcel.Visible = True
            oSheet = oBook.Worksheets(1)
         
    
            Dim csvFile As String = Dir("J:\*.csv") ' vb\RizRandom\poo.csv")
            
            Do While csvFile <> ""
                oBook.Open("J:\" & csvFile)
                oBook.Sheets.Add() ' add a new worksheet
                oBook.Sheets(1).cells.copy()
                oBook.Sheets(1).paste()
                oBook.Sheets(1).name = csvFile 'rename the worksheet
                csvFile = Dir()
            Loop
        
            oExcel.DisplayAlerts = vbFalse
            oExcel.AlertBeforeOverwriting = vbFalse
            oBook.SaveAs("J:\vb\RizRandom\RizRandom\TestUBM.xlsx")
            oBook.Close()
           
            oExcel = Nothing
            GC.Collect()
        End Sub
Working...