Solved

Opening an Excel File in VB.NET WinForms App

Posted on 2004-10-18
5
1,025 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 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