473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

importing Excel files to datagrids

Hello All,

I know how to import a specific named excel sheet into a datagrid
using ADO.NET by setting up a JET connection and then SELECTing data
from the sheet. However, for a real world application, I would
typically not know what the names of the sheets are going to be before
I open the Excel file.

I am wondering if there is a SELECT statement that would get me the
list of sheet names so that I can place them in a combobox and
selecting one will load the appropriate sheet in a datagrid. *** I am
trying to do this without resorting to using COM and the Office object
model!!!! ***. I am hoping some bright, well informed person knows
if there is some undocumented SELECT statement I can use to get
information from the Excel file.

Thanks,

Conrad
Nov 20 '05 #1
3 8918
I have a form that has a textbox named miscinfo which is where the user is
supposed to type the name of the sheet they want to import. When they Enter
it, this code runs to provide them with a list of sheets for Excel or a list
of Tables for Access. The results are placed in a Listbox named lbTableNames
which is right next to the textbox. The user can either type the name
directly or select the entry in the listbox.
Private Sub miscinfo_Enter( ByVal sender As Object, ByVal e As
System.EventArg s) Handles miscinfo.Enter
Dim strConn, strFileFormat, strLocalFileNam e As String
Dim TableNames As DataTable
Dim objDAO As DAO

Try
Cursor.Current = Cursors.WaitCur sor
strLocalFileNam e = Me.txtPath.Text
strFileFormat = Me.lbFileFormat .Text

If strFileFormat = "Excel" Or strFileFormat = "Access" Then
If strLocalFileNam e <> String.Empty Then
Me.lbTableNames .Visible = True
If strFileFormat = "Excel" Then
strConn = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" &
strLocalFileNam e & ";Extended Properties=""Ex cel 8.0;HDR=YES"""
'get the list of tables in the Excel file
'sheets end with $ and named ranges do not.
End If
If strFileFormat = "Access" Then
strConn = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" &
strLocalFileNam e
'get the list of tables in the Access mdb file
End If

objDAO = New DAO(strConn)
objDAO.GetSchem a(TableNames, strSQL, "OLEDB")

With Me.lbTableNames
.DisplayMember = "TABLE_NAME "
.ValueMember = "TABLE_NAME "
.DataSource = TableNames
.SelectedIndex = -1
End With
End If
End If

Catch ex As Exception
MessageBox.Show (ex.Message, ex.Source & " - " & ex.TargetSite.N ame,
MessageBoxButto ns.OK, MessageBoxIcon. Information)
Finally
objDAO = Nothing
Cursor.Current = Cursors.Default
End Try
End Sub
=============== =============== =============== =============

Private Sub lbTableNames_Se lectedIndexChan ged(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
lbTableNames.Se lectedIndexChan ged
Me.miscinfo.Tex t = CStr(Me.lbTable Names.SelectedV alue)
End Sub
=============== =============== =============== =============

My DAO class has a GetSchema method:

Public Sub GetSchema(ByRef dt As DataTable, ByVal strSQL As String, ByVal
DBtype As String)
'retrieve structure information into a datatable using the FillSchema
method of a DataAdapter object
If DBtype = "SQL Server" Then
Dim da As SqlDataAdapter
Dim cnn As New SqlConnection(m ConnStr)
Dim cmd As New SqlCommand(strS QL, cnn)
Try
cmd.CommandType = CommandType.Tex t
cnn.Open()
da = New SqlClient.SqlDa taAdapter(cmd)
da.FillSchema(d t, SchemaType.Sour ce)

Catch exc As Exception

Finally
cnn.Close()
End Try
ElseIf DBtype = "Oracle" Then
Dim da As OracleDataAdapt er
Dim cnn As New OracleConnectio n(mConnStr)
Dim cmd As New OracleCommand(s trSQL, cnn)
Try
cmd.CommandType = CommandType.Tex t
cnn.Open()
da = New OracleClient.Or acleDataAdapter (cmd)
da.FillSchema(d t, SchemaType.Sour ce)
Catch exc As Exception

Finally
cnn.Close()
End Try
ElseIf DBtype = "OLEDB" Then
Dim cnn As New OleDbConnection (mConnStr)
Try
cnn.Open()
'KB309488 - the Object array is for filtering the returned data
(only TABLEs are returned not Views, etc.)
dt = cnn.GetOleDbSch emaTable(OleDbS chemaGuid.Table s, New Object()
{Nothing, Nothing, Nothing, "TABLE"})
Catch exc As Exception

Finally
cnn.Close()
End Try
End If
End Sub
--
Joe Fallon

"Conrad F" <co*********@ho tmail.com> wrote in message
news:6a******** *************** ***@posting.goo gle.com...
Hello All,

I know how to import a specific named excel sheet into a datagrid
using ADO.NET by setting up a JET connection and then SELECTing data
from the sheet. However, for a real world application, I would
typically not know what the names of the sheets are going to be before
I open the Excel file.

I am wondering if there is a SELECT statement that would get me the
list of sheet names so that I can place them in a combobox and
selecting one will load the appropriate sheet in a datagrid. *** I am
trying to do this without resorting to using COM and the Office object
model!!!! ***. I am hoping some bright, well informed person knows
if there is some undocumented SELECT statement I can use to get
information from the Excel file.

Thanks,

Conrad

Nov 20 '05 #2
Open the excel file.. You can foreach the Sheets collection and each sheet
will have a name property. You don't want to use COM or Office, but how
else would you know what to select from? You'll need to know a sheet name
to use a select, but since you can walk through the collection of sheets in
probably 3-5 lines of code....

HTH,

Bill
"Conrad F" <co*********@ho tmail.com> wrote in message
news:6a******** *************** ***@posting.goo gle.com...
Hello All,

I know how to import a specific named excel sheet into a datagrid
using ADO.NET by setting up a JET connection and then SELECTing data
from the sheet. However, for a real world application, I would
typically not know what the names of the sheets are going to be before
I open the Excel file.

I am wondering if there is a SELECT statement that would get me the
list of sheet names so that I can place them in a combobox and
selecting one will load the appropriate sheet in a datagrid. *** I am
trying to do this without resorting to using COM and the Office object
model!!!! ***. I am hoping some bright, well informed person knows
if there is some undocumented SELECT statement I can use to get
information from the Excel file.

Thanks,

Conrad

Nov 20 '05 #3
On 18 Sep 2003 07:18:52 -0700, co*********@hot mail.com (Conrad F) wrote:

¤ Hello All,
¤
¤ I know how to import a specific named excel sheet into a datagrid
¤ using ADO.NET by setting up a JET connection and then SELECTing data
¤ from the sheet. However, for a real world application, I would
¤ typically not know what the names of the sheets are going to be before
¤ I open the Excel file.
¤
¤ I am wondering if there is a SELECT statement that would get me the
¤ list of sheet names so that I can place them in a combobox and
¤ selecting one will load the appropriate sheet in a datagrid. *** I am
¤ trying to do this without resorting to using COM and the Office object
¤ model!!!! ***. I am hoping some bright, well informed person knows
¤ if there is some undocumented SELECT statement I can use to get
¤ information from the Excel file.

Public Function ListExcelTables NET() As Boolean

Dim ExcelConnection As System.Data.Ole Db.OleDbConnect ion
Dim ExcelTables As DataTable

Try

ExcelConnection = New
System.Data.Ole Db.OleDbConnect ion("Provider=M icrosoft.Jet.OL EDB.4.0;" & _
"Data Source=e:\My Documents\Book1 0.xls;Extended Properties=Exce l 8.0;")

ExcelConnection .Open()

ExcelTables =
ExcelConnection .GetOleDbSchema Table(System.Da ta.OleDb.OleDbS chemaGuid.Table s, New Object() {Nothing,
Nothing, Nothing, "TABLE"})

frmMain.DataGri d1.DataSource = ExcelTables

Catch ex As Exception
MessageBox.Show (ex.Message)
Finally
ExcelConnection .Close()

End Try

End Function
Paul ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2472
by: Steve Chatham | last post by:
I use the following code: Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged Dim sFile As String = Session("User") & "-Customer List-" & Today() sFile = sFile.Replace("/", "") RbtnExport.Visible = False Select Case RbtnExport.SelectedItem.Value Case "Excel" Response.ContentType = "application/x-msexcel"
0
1277
by: Steve Chatham | last post by:
I need to export multiple datagrids (where we have a drill-through web application) into an Excel workbook. For instance, when you click on the first datagrid, you get a subset of the sql table. Subsequent clicks on those items in the table further filter the request, until you go from a business area down to a single SKU. Thus far, I have gotten it to work, but there have been size issues (I guess, as smaller files seem to do okay),...
9
3002
by: | last post by:
I have a web page written in asp.net that has multiple datagrids on it that would need to be exported to Excel. Each of the datagrids would be a subset of what the datagrid above it was. Thus far, I've had no luck in finding anything to work reliably. Some of the files I've saved (using a radio button click event), will open fine in Excel, others give me an "Unable to open file" message. Any help appreciated.
2
3170
by: Snozz | last post by:
The short of it: If you needed to import a CSV file of a certain structure on a regular basis(say 32 csv files, each to one a table in 32 databases), what would be your first instinct on how to set this up so as to do it reliably and minimize overhead? There are currently no constraints on the destination table. Assume the user or some configuration specifies the database name, server name, and filename+fullpath. The server is SQL...
5
3167
by: hharriel | last post by:
Hi, I am hoping someone can help me with an issue I am having with excel and ms access. I have collected data (which are in individual excel files) from 49 different school districts. All districts have used the same excel template and populated the same 32 data fields (columns). I created one large excel file from all 49 files which gives me a master table of 60,000 or so records. I have tried to import this master table into access...
28
19231
by: kkadakia | last post by:
I get a daily excel file for a entire month which I want to transfer into Access at the end of the month. So, there are around 20-25 excel files I get by the end of the month, and I would like to import them into Access using a button. The data contained in the excel files is similar, so there should no formatting issues while importing. I searched through the forums and found the code by mmccarthy for importing excel files. I tried using...
1
5154
by: thadson | last post by:
Hi, I'm trying to import specific cells from MS Excel 2000 spreadsheets to MS Access 2000 tables then move the spreadsheets to a different directory. I'm very new to this and I'm having trouble to implement this. I have worked out so far the code to import certain cells into 1 table, but I do not know how to import some other cells into another tables so the data would be connected and remain together. So lets say that I have 2 tables...
1
1312
by: =?Utf-8?B?Sm9obiBXYWxrZXI=?= | last post by:
Hi, I am using the code below to export a webpage to Excel. The webpage has three datagrids on it and they are all exported to Excel properly and everything looks very nice, but we would really only like two of the three datagrids to be exported. Is there a way to exclude certain sections of the webpage from being exported to Excel? "DG" in the code here is one of the datagrids on the page: ...
3
3000
by: scoots987 | last post by:
What do others do if you need to import excel files into SQL Server? My main problems are 1) zipcode formatting issues. If the column is a mix of zip and zip+4, I have problems retrieving all zipcodes. 2) If the last column contains NULL no information is imported. All this with using the Management console using Import data in SQL Server 2005. I am simply trying to import the data into NEW databases.
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.