Solved

Import / Export Excel Spreadsheets into and from datasets using Visual Studio 2008 VB9

Posted on 2008-10-03
7
2,893 Views
Last Modified: 2013-11-26
I have a VB9 / Visual Studio 2008 application, which uses a .mdb database. I select data from the database with a  select button using an sql statement so that it shows only selected data in the grid on screen. I then have the option to print just that selected data. I now need to add the option to export just the selected data to an excel spreadsheet. Similarly, I also need the option to import from the same  spreadsheet that was previously created by the above process, into that selected dataset, so that it only updates the records present in the selection.
I have no idea how to do this in Visual Studio VB, and need the easiest, shortest method to do this.
0
Comment
Question by:leevee1606
[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
  • 5
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
JasonChandler earned 500 total points
ID: 22632394
Hi there I have some code to import spread sheets. I cant really help you with the Export part as I have not had to do that. I have a grid that already allows me to export data.
I created this code for importing spread sheets on a web app i had to make.

It allows you to Import the sheet into a dataset or to an XML file.
I have just cut this out of my project so please test it first, but it should do what you want.

NOTE: It will only select all the data on the first sheet of the Spread Sheet.
[("SELECT * FROM [Sheet1$]"] but you could do a range of cells if you wanted to
by changing the Select statement.

Hope this helps

Jason



Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration.ConfigurationManager
Imports ClientEmail.StoredProcedureCallerClasses
Imports System.IO
 
Public Class ExcelWrapper
 
 
    Public Function ReadExcelFileToDataSet() As DataSet
 
        ' Create connection string variable. Modify the "Data Source" 
        ' parameter as appropriate for your environment. 
        Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + HttpContext.Current.Server.MapPath("~/Imports/Excel/TEST_IMPORT.xls") + ";" + "Extended Properties=Excel 8.0;"
        ' Create connection object by using the preceding connection string. 
        Dim objConn As New OleDbConnection(sConnectionString)
        ' Open connection with the database. 
        objConn.Open()
        ' The code to follow uses a SQL SELECT command to display the data from the worksheet. 
        ' Create new OleDbCommand to return data from worksheet. 
        Dim objCmdSelect As New OleDbCommand("SELECT * FROM [Sheet1$]", objConn)
        ' Create new OleDbDataAdapter that is used to build a DataSet 
        ' based on the preceding SQL SELECT statement. 
        Dim objAdapter1 As New OleDbDataAdapter()
        ' Pass the Select command to the adapter. 
        objAdapter1.SelectCommand = objCmdSelect
        ' Create new DataSet to hold information from the worksheet. 
        Dim objDataset1 As New DataSet()
        ' Fill the DataSet with the information from the worksheet. 
        objAdapter1.Fill(objDataset1, "XLData")
 
        ' Clean up objects. 
        objConn.Close()
 
        Return objDataset1
    End Function
 
 
    Public Function ReadExcelFileToXML(ByVal sFileName As String) As String
 
        Dim Objprofile As ProfileCommon = DirectCast(HttpContext.Current.Profile, ProfileCommon)
        Dim FPath As String = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings.Item("ExcelXMLCache"))
        Dim myXMLfile As String = FPath & Path.GetFileNameWithoutExtension(sFileName) & ".xml"
        Dim myFileStream As New System.IO.FileStream(myXMLfile, System.IO.FileMode.Create)
        Dim MyXmlTextWriter As New System.Xml.XmlTextWriter _
           (myFileStream, System.Text.Encoding.Unicode)
        Dim rtnFileName As String = String.Empty
        Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + HttpContext.Current.Server.MapPath("~/Imports/Excel/" & sFileName) + ";" + "Extended Properties=Excel 8.0;"
        Dim objConn As New OleDbConnection(sConnectionString)
        objConn.Open()
        Dim objCmdSelect As New OleDbCommand("SELECT * FROM [Sheet1$]", objConn)
        Dim objAdapter1 As New OleDbDataAdapter()
        objAdapter1.SelectCommand = objCmdSelect
        Dim objDataset1 As New DataSet()
        objAdapter1.Fill(objDataset1, "XLData")
        objDataset1.WriteXml(MyXmlTextWriter, XmlWriteMode.IgnoreSchema)
        'objDataset1.WriteXml(MyXmlTextWriter, XmlWriteMode.WriteSchema)
 
        'Set Profile XML File.
        Objprofile.XMLFileName = myXMLfile
        rtnFileName = myXMLfile
 
        ' Clean up objects. 
        objConn.Close()
        objDataset1.Dispose()
        objAdapter1.Dispose()
        MyXmlTextWriter.Flush()
        myFileStream.Dispose()
        myFileStream = Nothing
        MyXmlTextWriter = Nothing
 
 
        Return rtnFileName
    End Function
End Class

Open in new window

0
 
LVL 2

Expert Comment

by:JasonChandler
ID: 22632413
Ok just reread your question, If you use the ReadExcelFileToDataSet Method in my code you can load the sheet data into a dataset, from there you can save the changes in the dataset. See this link for help, or maybe somebody else can give you the code, I am really busy at present. I can look at this later however, but cant promise when, sorry.
http://msdn.microsoft.com/en-us/library/xzb1zw3x(VS.80).aspx
0
 
LVL 2

Expert Comment

by:JasonChandler
ID: 22632421
One more thing..
Delete the following lines from my code if you use it
Imports System.Configuration.ConfigurationManager
Imports ClientEmail.StoredProcedureCallerClasses
 They do not apply to the code.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:leevee1606
ID: 22632492
Thanks for that, Jason - will try the code shortly. Just one question - you said you have a grid that allows you to export data - how do you set up a grid to allow you to export data?
0
 
LVL 2

Expert Comment

by:JasonChandler
ID: 22632519
Hi I did not set up the Grid I bought a 3rd party one:
http://www.janusys.com/controls/
0
 
LVL 2

Expert Comment

by:JasonChandler
ID: 22632573
0
 

Author Closing Comment

by:leevee1606
ID: 31502697
Thanks Jason - after much fiddling , managed to get it going using your code as a base. And the export side was easy - I wasn't thinking - crystal reports has a built-in export function, so I just created a report and export to excel from there. Anyway, thanks for the code which helped me get the import side going!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 88
Visual C# Windows Project - Clone Form 4 35
Subtract dates in vb.net 6 29
VB .net 2010 Byte array 2 14
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

731 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