Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Opening an Excel File in VB.NET WinForms App

Posted on 2004-10-18
5
Medium Priority
?
1,091 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 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

916 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