connecting excel to vb.net

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

Join Date: Aug 2007
Posts: 18
Reputation: apchidara is an unknown quantity at this point 
Solved Threads: 0
apchidara apchidara is offline Offline
Newbie Poster

connecting excel to vb.net

 
0
  #1
Aug 27th, 2007
hi every one I have a lot of excel data sheets and I need to work with them . how can I connect my code with the excel files . is it the same as ADO.net while u do with access?
please help me or send me a link about EXCEL ANV VB.NET CONNECTIVITY i al so lost .
thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: connecting excel to vb.net

 
0
  #2
Aug 27th, 2007
Here's a function for loading an excel spreadsheet using ADO.NET. It's in C#, but you should know how to convert between the two languages.
  1. using System;
  2. using System.Data;
  3. using System.Diagnostics;
  4. using System.ComponentModel;
  5. using System.Collections.Generic;
  6. using System.Data.OleDb;
  7.  
  8.  
  9. public class ExcelSpreadsheet {
  10. private List<string> _worksheets = new List<string>();
  11. [Description( "Gets a list of worksheet names matching the DataSet table names." )]
  12. public List<string> WorkSheets { get { return _worksheets; } }
  13.  
  14. private DataSet _spreadsheet = new DataSet();
  15. [Description( "Gets a DataSet with each table representing one worksheet in the spreadsheet" )]
  16. public DataSet SpreadSheet { get { return _spreadsheet; } }
  17.  
  18.  
  19. // Disable the default constructor
  20. private ExcelSpreadsheet() { }
  21.  
  22.  
  23. public ExcelSpreadsheet( string filename ) {
  24. _spreadsheet = this.LoadSpreadsheet( filename, false );
  25. }
  26.  
  27.  
  28. public ExcelSpreadsheet( string filename, bool hasHeader ) {
  29. _spreadsheet = this.LoadSpreadsheet( filename, hasHeader );
  30. }
  31.  
  32.  
  33. /// <summary> Load the worksheet names </summary>
  34. private IEnumerable<string> GetWorksheets( OleDbConnection connection ) {
  35. try {
  36. DataTable schema = connection.GetOleDbSchemaTable(
  37. OleDbSchemaGuid.Tables
  38. , new object[] { null, null, null, "TABLE" } );
  39.  
  40. foreach ( DataRow row in schema.Rows ) {
  41. _worksheets.Add( row["TABLE_NAME"].ToString() );
  42. }
  43.  
  44. return _worksheets;
  45. } catch ( Exception ex ) {
  46. Trace.Write( ex.Message );
  47. throw;
  48. }
  49. }
  50.  
  51.  
  52. private DataSet LoadSpreadsheet( string filename, bool hasHeader ) {
  53. try {
  54. DataSet spreadsheet = new DataSet();
  55. string connectionString =
  56. @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
  57. + filename
  58. + ";Extended Properties=\"Excel 8.0;HDR="
  59. + ( hasHeader ? "Yes" : "No" )
  60. + ";IMEX=1\";";
  61.  
  62. using ( OleDbConnection connection = new OleDbConnection( connectionString ) ) {
  63. connection.Open();
  64.  
  65. using ( OleDbCommand command = new OleDbCommand() ) {
  66. using ( OleDbDataAdapter adapter = new OleDbDataAdapter( command ) ) {
  67. foreach ( string ws in this.GetWorksheets( connection ) ) {
  68. command.Connection = connection;
  69. command.CommandText = "select * from [" + ws + "]";
  70.  
  71. DataTable table = new DataTable( ws );
  72. adapter.Fill( table );
  73.  
  74. spreadsheet.Tables.Add( table );
  75. }
  76. }
  77. }
  78.  
  79. connection.Close();
  80. }
  81.  
  82. return spreadsheet;
  83. } catch ( Exception ex ) {
  84. Trace.Write( ex.Message );
  85. throw;
  86. }
  87. }
  88. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: connecting excel to vb.net

 
0
  #3
Aug 27th, 2007
You should just be able to do something like:

  1. Dim blah As Excel.Application

and then access the various methods. I'm not sure if you need to add it as a com object or something?

Another alternative, although not as flexible, would be to save your excel file as a comma separated value file, and parse in the stuff using string split (",") methods etc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 23
Reputation: bruce2424 is an unknown quantity at this point 
Solved Threads: 0
bruce2424 bruce2424 is offline Offline
Newbie Poster

Re: connecting excel to vb.net

 
0
  #4
Oct 9th, 2008
you can connect excel file through vb.net with the help of oledb. check the following link.

http://vb.net-informations.com/excel...xcel_oledb.htm

bruce.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: rmjagnaan is an unknown quantity at this point 
Solved Threads: 0
rmjagnaan rmjagnaan is offline Offline
Newbie Poster

Re: connecting excel to vb.net

 
0
  #5
Oct 23rd, 2008
hi apchidara! here's how I did it using visual studio express and vb.net windows apps. hope you like it

  1. Try
  2. Dim opn as New OpenFileDialog
  3. opn.InitialDirectory = "c:\wheremyfileis\myXLfiles"
  4. opn.Filter = "Excel Files (*.xls)|*.xls"
  5. opn.FilterIndex = 1
  6. If opn.Showdirectory = Windows.Forms.DialogResult.Ok Then
  7. dim sb as system.text.stringbuilder = new system.text.stringbuilder("")
  8. sb.append("Provider=Microsoft.Jet.OLEDB.4.0;")
  9. sb.append("Data Source= ")
  10. sb.append(opn.FileName)
  11. sb.append(";Extended Properties = Excel 8.0;")
  12. CNx = New system.data.oledb.oledbconnection(mycnn)
  13. CNx.Open()
  14. end if
  15. DAx = New oledb.oledbdataadapter("select rcode, pcode, etc., "the exact column names found in your excel ", CNx)
  16. DTx = New DataTable
  17. DAx.Fill(DTx)
  18. Catch ex as exception
  19. End try
  20.  

don't forget the imports e.g., imports system.data.oledb
also don't forget to declare the dim DAx as oledbdataadapter and DTx as system.data.datatable
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC