Solved

Opening an Excel File in VB.NET WinForms App

Posted on 2004-10-18
5
966 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:nathanpz
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
123654789987 earned 500 total points
ID: 12344689
To open a Excel file on a button click..

1. U need to add Microsoft Excel 9.0 Object library reference in your project. Yes there will be problems if the client computer  has a different version of Microsoft Office. So it is better that during the time of deployment u compile the project with the same version as that present on the client computer

2. Open a excel file on button click

Dim ExcelObj as Excel.Application
Sub ExcelBtn_Click(sender As Object, e As EventArgs)
             ExcelObj = new Excel.Application()
me.openFileDialog1.FileName = "*.xls";
  if (me.openFileDialog1.ShowDialog() == DialogResult.OK)
 
  // ***********  Here is the call to Open a Workbook in Excel ****************
  // It uses most of the default values (except for the read-only which we set to true)
   Dim theWorkbook  as   Excel.Workbook
   theWorkbook = ExcelObj.Workbooks.Open(
          openFileDialog1.FileName, 0, true, 5,
          "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
          0, true)

         End if
      End Sub

For more processing u can follow this link

http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp
0
 

Author Comment

by:nathanpz
ID: 12344924
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?
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12345035
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

0
 
LVL 18

Expert Comment

by:armoghan
ID: 12345047
0
 

Author Comment

by:nathanpz
ID: 12345166
Thanks 123....
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question