User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 423,380 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,836 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 3395 | Replies: 2
Reply
Join Date: Aug 2007
Location: france, malaysia
Posts: 18
Reputation: apchidara is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
apchidara apchidara is offline Offline
Newbie Poster

Question connecting excel to vb.net

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Posts: 321
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Rep Power: 3
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: connecting excel to vb.net

  #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  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: connecting excel to vb.net

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

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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 12:43 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC