943,700 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 18208
  • VB.NET RSS
Aug 27th, 2007
0

connecting excel to vb.net

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apchidara is offline Offline
18 posts
since Aug 2007
Aug 27th, 2007
0

Re: connecting excel to vb.net

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.
VB.NET Syntax (Toggle Plain Text)
  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. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 27th, 2007
1

Re: connecting excel to vb.net

You should just be able to do something like:

VB.NET Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 9th, 2008
0

Re: connecting excel to vb.net

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bruce2424 is offline Offline
23 posts
since Jun 2008
Oct 23rd, 2008
0

Re: connecting excel to vb.net

hi apchidara! here's how I did it using visual studio express and vb.net windows apps. hope you like it

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rmjagnaan is offline Offline
4 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in VB.NET Forum Timeline: which OPERATING SYSTEM is Best???
Next Thread in VB.NET Forum Timeline: Unable to retrieve String data from Excel to SQL Server database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC