Link to home
Start Free TrialLog in
Avatar of nathanpz
nathanpz

asked on

Opening an Excel File in VB.NET WinForms App

I have a file that I would like to open in excel if the user clicks on the button of a windows form.

What is the simplest VB.NET code for this task?
Will there be any version issues depending on what Excel version the client computer has?
Does a reference have to be made to an Excel OLB in the .NET solution?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of 123654789987
123654789987

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nathanpz
nathanpz

ASKER

Ok, that's pretty good, but I should have mentioned that I have a csv file to open. I thought that if the .csv extension was present, that Excel would take care of the rest. Unfortunately it doesn't. Is there a way around this. I already changed the delimeter from "\t" to "," but it didn't help.

Here is the code I have used:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim ExcelObj As Excel.Application
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ''''''''''''''''''''''''''''''
        ''open excel
        ''''''''''''''''''''''''''''''
        ExcelObj = New Excel.Application()

        Dim theWorkbook As Excel.Workbook
        theWorkbook = ExcelObj.Workbooks.Open(_path & "\SSresult.csv", 0, False, 5, "", "", True, Excel.XlPlatform.xlWindows,"," , False, False, 0, True)
        ExcelObj.Visible = True

    End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Any ideas?
U can convert the file from .csv to .xls this way.

 Public Function ConvertCSVToXLS(ByVal strCSVFile As String, ByVal strXLSFile As String)
  Dim oExcel As Excel.Application
  Set oExcel = New Excel.Application
 
  Dim oBook As Workbook
  Set oBook = oExcel.Workbooks.Open(strCSVFile)
    oBook.SaveAs strXLSFile, xlExcel9795
  Set oBook = Nothing
 
  oExcel.Quit
  Set oExcel = Nothing
End Function

Thanks 123....