Solved

Opening an Excel File in VB.NET WinForms App

Posted on 2004-10-18
5
922 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now