Jump to content

..:: using openFileDialog ::..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
bahrain

bahrain

    Newbie

  • Members
  • PipPip
  • 23 posts
Hello Guys

I'v 2 Questions and I'll appreciate your help..


How can I use the open file dialog??
and how can I import information from Excel ??

I'm using VB.Net 2008


thanx in advance

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
As to your first question:


  • Add the OpenFileDialog from the toolbox to your project.
  • Give it a name (such as dlgOpen or something)
  • Use the following code when you want to open it:
    If dlgOpen.ShowDialog() = DialogResult.OK Then
      Dim fileName As String = dlgOpen.FileName
    End If
    

As to the second question, it depends on what you want to import. Assuming it is just data, you need the data exported as a CSV file. Then you can read it in using System.IO.

If you need more than just data, you can automate Excel by adding a COM reference and manipulating the API.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
vizhin

vizhin

    Newbie

  • Members
  • Pip
  • 4 posts
Hi,I have quickly put something together I hope will help you upload data from excel. You need to have reference to Excel Automation Library for the code to work.

Private Sub Upload()

Dim myDataset As New DataSet()


Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= " + "C:\Uploads\" + FileName + ";" & _
"Extended Properties=""Excel 8.0;"""

''You must use the $ after the object you reference in the spreadsheet. In this example its sheet1$
Dim myData As New OleDbDataAdapter("SELECT * FROM [sheet1$]", strConn)


myData.TableMappings.Add("Table", "ExcelTest")
myData.Fill(myDataset)

'now the data is in the dataset u can view in grid or save to db

End Sub

#4
bahrain

bahrain

    Newbie

  • Members
  • PipPip
  • 23 posts
Thanx Xav , vizhin

Can You Explain More pleas..

how can I add reference to Excel Automation Library ??

should the user add $ after all the sheets? .

and if there are more than one sheet would the code work ?

#5
vizhin

vizhin

    Newbie

  • Members
  • Pip
  • 4 posts
Hi

Q)How can I add reference to Excel Automation Library ??

A) - right click your project and click "Add References"
- goto the "COM" tab
- look for "Microsoft Excel 11.0 Object library" and double click it
(am using office 2003 and could be different depending on your version)
-the refrence should appear under "References"


Q) Should the user add $ after all the sheets?

A) No you dont have to add "$" on all sheets. You only append "$" to the sheet name in code.

Q) If there are more than one sheet would the code work ?

A) The code will work. However you have to work on each sheet seperately i.e.
Dim mData1 As New OleDbDataAdapter("SELECT * FROM [sheet1$]", strConn)

Dim mData2 As New OleDbDataAdapter("SELECT * FROM [sheet2$]", strConn)

Dim mData3 As New OleDbDataAdapter("SELECT * FROM [sheet3$]", strConn)

Assuming that that your book has sheets : Sheet1,Sheet2 and Sheet3

Regards

#6
bahrain

bahrain

    Newbie

  • Members
  • PipPip
  • 23 posts
I added The Reference It's not working

I also added
Imports Microsoft.Office.Interop.Excel

and nothing yet !!

What should I do ??

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
What is the error message you are getting, if any?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
bahrain

bahrain

    Newbie

  • Members
  • PipPip
  • 23 posts
Type 'OleDbDataAdapter' is not defined

This is the error...

#9
vizhin

vizhin

    Newbie

  • Members
  • Pip
  • 4 posts
hi

You have to import the following workspace.

-------------------------------------------------------------------------
Imports System.Data.OleDb

#10
bahrain

bahrain

    Newbie

  • Members
  • PipPip
  • 23 posts
Thanx guys I Found the solution . :)

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
If you do not wish to import a namespace, you can simply refer to the class by putting the namespace, then a period (.) followed by the class.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums