Pls. Help!!! HOW DO I IMPORT AND EXPORT

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 38
Reputation: omotoyosi is an unknown quantity at this point 
Solved Threads: 1
omotoyosi omotoyosi is offline Offline
Light Poster

Pls. Help!!! HOW DO I IMPORT AND EXPORT

 
0
  #1
Jan 17th, 2008
Hello All,
I'm Here Again!!! How Do I Import From Excel Or Csv To Vb.net Database.
Moreso, I Want A Code To Export From My Database To Excel Or Csv.


Great Programmers
Pls, Help Me Out It Is Urgent!!!!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 49
Reputation: Mr.Wobbles is an unknown quantity at this point 
Solved Threads: 2
Mr.Wobbles Mr.Wobbles is offline Offline
Light Poster

Re: Pls. Help!!! HOW DO I IMPORT AND EXPORT

 
0
  #2
Jan 18th, 2008
In order to interface with excel you have to make the reference to it, go to Project -> Add Reference -> under the COM tab select Microsoft Excel 11.0 Object Library (could be a different number based on your version).

After that you can use functions that are reserved for Excel this next part can get pretty tedious. It is probably best to create a loop that keeps calling information from your DB dynamically. This is an example of some code I used to export:

  1. Sub exportToExcel()
  2.  
  3. 'Declare Variables to create the excel application, workbook, sheet and chart
  4. Dim xlApp As Excel.Application
  5. Dim xlWkb As Excel.Workbook
  6.  
  7. 'Create the excel application
  8. xlApp = CreateObject("Excel.Application")
  9.  
  10. 'Add the workbook to the excel application
  11. xlWkb = xlApp.Workbooks.Add
  12.  
  13. 'Set up connection for database
  14. Dim con As New OleDb.OleDbConnection
  15. Dim ds As New DataSet
  16. Dim da As OleDb.OleDbDataAdapter
  17.  
  18. Dim sql As String
  19.  
  20. con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = CONNECTION STRING"
  21.  
  22. 'Open the Connection to the database
  23. con.Open()
  24.  
  25. 'Pull information from the database
  26. sql = "SELECT * FROM table_name"
  27. da = New OleDb.OleDbDataAdapter(sql, con)
  28.  
  29. da.Fill(ds, "Survey Results")
  30.  
  31. Dim cb As New OleDb.OleDbCommandBuilder(da)
  32.  
  33. Dim numRows As Integer = ds.Tables("Survey Results").Rows.Count - 1
  34.  
  35. xlSessionCommentsExport(numRows, ds, xlWkb)
  36. xlBestExport(numRows, ds, xlWkb)
  37. xlTopicsExport(numRows, ds, xlWkb)
  38. xlNumericalExport(numRows, ds, xlWkb)
  39. xlSummaryExport(numRows, ds, xlWkb, con)
  40. With xlWkb
  41. .Sheets("Sheet1").Select()
  42. .ActiveSheet.Delete()
  43. .Sheets("Sheet2").Select()
  44. .ActiveSheet.Delete()
  45. .Sheets("Sheet3").Select()
  46. .ActiveSheet.Delete()
  47. End With
  48.  
  49. xlApp.Visible = True
  50. con.Close()
  51. End Sub

Because it can get pretty long I used functions to actually insert the data, the xlSessionCommentsExport is probably the shortest so i will give that to you

  1. Private Sub xlSessionCommentsExport(ByVal numRows, ByVal ds, ByVal xlWkb)
  2.  
  3. Dim i As Integer
  4. Dim dsRow As DataRow
  5.  
  6. 'Add the worksheet to the workbook
  7. Dim xlSessionComments As Excel.Worksheet
  8. xlSessionComments = xlWkb.Worksheets.Add
  9. xlSessionComments.Name = "Session Comments"
  10.  
  11. With xlSessionComments
  12. .Range("A1:B1").Merge()
  13. .Range("A1:B1").FormulaR1C1 = "Session Comments"
  14.  
  15. For i = 0 To numRows
  16. 'get the current row
  17. dsRow = ds.Tables("Survey Results").Rows(i)
  18.  
  19. 'get the values required for this worksheet out of the current
  20. 'row and assign them to variables
  21. sessionComments = CheckDBNullStr(dsRow.Item("sessionComments"))
  22. .Range("B" & i + 2).FormulaR1C1 = sessionComments
  23. Next
  24. .Columns("B").WrapText = True
  25. .Columns("B:B").ColumnWidth = 110
  26. .Rows("1:" & numRows + 2).autofit()
  27. End With
  28.  
  29. End Sub

In order to get what would actually be used to put the information in Excel go into Excel and create a Macro that does what you want to do and then there is a way to look at the VB code that does that macro. You will have to change a little bit, but the majority should work just fine.

Other than that I have never imported anything, but it should be the same except backwards. You will have to open the actual file from the program and use these functions to read it in - google a little bit that should be pretty easy. If I wasn't clear on anything don't hesitate to ask.
Mr.Wobbles~
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC