_HAWK_, on 29 January 2012 - 04:45 AM, said:
Imports System.Runtime.InteropServices
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
'oExcel = CreateObject("Excel.Application") 'also late binding
oExcel = New Excel.Application ' <- use this instead
oExcel.Visible = True
oBook = oExcel.Workbooks.Add
Dim csvFile As String = Dir("J:\vb\RizRandom\RizRandom\Results\poo.csv")
Dim oBook2 As Excel.Workbook = oExcel.Workbooks.Open("J:\vb\RizRandom\RizRandom\Results\poo.csv")
oBook.Sheets.Add() ' add a new worksheet
'no late binding here
CType(oBook2.Sheets(1), Excel.Worksheet).Cells.Copy()
CType(oBook.Sheets(1), Excel.Worksheet).Paste()
CType(oBook2.Sheets(1), Excel.Worksheet).Name = csvFile
'The line below will save only the active workbook with .xls extention that Exel Viewer can open
oExcel.DisplayAlerts = False 'ovoid vb6 constants
oExcel.AlertBeforeOverwriting = False
oBook.SaveAs("J:vb\RizRandom\RizRandom\Results\TestUBM.xlsx") '-4143) 'J:\vb\RizRandom\RizRandom\
oBook.Close()
oExcel.Quit()
'these methods will properly dispose the process for excel
'check the task manager and sometimes you will see it open
Marshal.FinalReleaseComObject(oExcel)
Marshal.FinalReleaseComObject(oBook)
oExcel = Nothing
oBook = Nothing
'no need to call GC.Collect now
End Sub
Thank you Hawk
I modified it so I could use open an existing excel file to add in the copied csv data.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
'oExcel = CreateObject("Excel.Application") 'also late binding
oExcel = New Excel.Application ' <- use this instead
oExcel.Visible = True
oBook = oExcel.Workbooks.Add("J:\vb\RizRandom\RizRandom\Results\UBM.xlsx")
Dim csvFile As String = Dir("J:\vb\RizRandom\RizRandom\Results\poo.csv")
Dim oBook2 As Excel.Workbook = oExcel.Workbooks.Open("J:\vb\RizRandom\RizRandom\Results\poo.csv")
oBook.Sheets.Add() ' add a new worksheet
'no late binding here
CType(oBook2.Sheets(1), Excel.Worksheet).Cells.Copy()
CType(oBook.Sheets(1), Excel.Worksheet).Paste()
CType(oBook.Sheets(1), Excel.Worksheet).Name = csvFile
'oBook.Sheets(1).name = csvFile
'The line below will save only the active workbook with .xls extention that Exel Viewer can open
oExcel.DisplayAlerts = False 'ovoid vb6 constants
oExcel.AlertBeforeOverwriting = False
oBook.SaveAs("J:vb\RizRandom\RizRandom\Results\UBM.xlsx") '-4143) 'J:\vb\RizRandom\RizRandom\
oBook.Close()
oExcel.Quit()
'these methods will properly dispose the process for excel
'check the task manager and sometimes you will see it open
Marshal.FinalReleaseComObject(oExcel)
Marshal.FinalReleaseComObject(oBook)
oExcel = Nothing
oBook = Nothing
However the rest of my apllication also generates csv files. Is it sensible to add each csv file as above to a different worksheet? Once all of these have been combined into the excel file the user has to email it back to me.

New Topic/Question
Reply





MultiQuote




|