Reading and Writing Excel Spreadsheets - Worksheets Using ADO - Net C# DbProviderFactory
Reading and Writing Excel Spreadsheets - Worksheets Using ADO - Net C# DbProviderFactory
NET C# DbProviderFactory
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
Home
About
Blog
Main
Syndication Sarasota Web Developer Orchard CMS Developer
It has been an absolute exhausting work week. My eyes, wrists, and back are sore from too many hours of coding. Thankfully I get to relax much of the Memorial Day Weekend :) I hope you all enjoy the weekend, too. This week a client sent me an Excel Spreadsheet that needed to populate several tables in a SQL Server Database. To know me knows I hate data entry of any kind and there was no chance I was entering the Excel data in manually. Thankfully, we don't have to. You can use the OleDb Managed Data Provider in the .NET Framework to read an Excel Spreadsheet using ADO.NET and C# just like you would with a
1 of 7
11/8/2011 11:02 AM
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
database. Shown below is a simple spreadsheet that lists a few cities ( Bradenton, Sarasota, and Tampa ) in Florida. Notice I have renamed the worksheet to Cities as opposed to the default of Sheet1. Also notice that the first row contains headers of the columns. These changes will impact the way we access the information as you will see in a moment.
You will first need a connection string to connect to the Excel Workbook, which would be the following:
2 of 7
11/8/2011 11:02 AM
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
called Book1.xls, and the first row is a header row containing the names of the columns.
Once you have the connection string all normal ADO.NET coding applies. Here is some sample code that reads each row of the excel worksheet using DbDataReader. You don't have to use the DbProviderFactory Classes. I thought I would show it just for kicks.
string connectionString = @"Provider=Microsoft.Jet. OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""; DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = connectionString; using (DbCommand command = connection.CreateCommand()) { // Cities$ comes from the name of the worksheet command.CommandText = "SELECT ID,City,State FROM [Cities$]"; connection.Open(); using (DbDataReader dr = command.ExecuteReader()) { while (dr.Read()) { Debug.WriteLine(dr["ID"].ToString()); } } } }
ASP.NET MVC
ASP.NET MVC 4 Developer Preview Released ASP.NET MVC 4 Roadmap ASP.NET MVC 3 Tutorials ASP.NET MVC 3 Videos ASP.NET MVC 3 Training
Orchard CMS
Custom Orchard Tumblr Theme - Orchard Theme Development Head First HTML5 Programming Book Review Building Web Apps with JavaScript Blog Post Created On Date and Time in Orchard CMS Orchard CMS Development Content Types with Title Part and ITitleAspect in Orchard 1.3 - Orchard Web Developer
3 of 7
11/8/2011 11:02 AM
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
Here is another example of reading an Excel spreadsheet using ADO.NET and a DataSet.
string connectionString = @"Provider=Microsoft.Jet. OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""; DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); DbDataAdapter adapter = factory.CreateDataAdapter(); DbCommand selectCommand = factory.CreateCommand(); selectCommand.CommandText = "SELECT ID,City,State FROM [Cities$]"; DbConnection connection = factory.CreateConnection(); connection.ConnectionString = connectionString; selectCommand.Connection = connection; adapter.SelectCommand = selectCommand; DataSet cities = new DataSet(); adapter.Fill(cities); gridEX1.SetDataBinding(cities.Tables[0], ""); gridEX1.RetrieveStructure();
I was binding to the Janus GridEx Control, which is why you see gridEX1 above. You could easily replace those 2 lines with
dataGridView1.DataSource = cities.Tables[0].DefaultView;
Head First jQuery Book Review - Orchard CMS Development Delete Content Types in Orchard 1.3 - Orchard CMS Web Developer Upgraded Website to Orchard 1.3 - Orchard CMS Development HTML5 The Missing Manual Book Review - Orchard Web Developer Previewing Unpublished Blog Posts and Pages in Orchard 1.3 Orchard 1.3 Features Markdown Support for Pages, Blog Posts and Content Authoring Orchard 1.3 Released Forms API, Tokens, Content Item Preview Microsoft WebMatrix 2 for Small Website Development Orchard CMS Navigation Admin Menu and LocalNav Custom Orchard Themes Converting Existing Web Design and HTML Templates CoffeeScript Development Sarasota JavaScript Developer JavaScript Enlightenment Book Review - JavaScript for ASP.NET Web Developers jQuery and JavaScript Development using Firefox Scratchpad Gamification by Design Implementing Game Mechanics in Web and Mobile Apps JavaScript Web Applications Book Review - backbone.js spine.js Orchard Website Performance Settings Configuring the Orchard Warmup Module Search Engine Optimization and Orchard CMS - Semantic HTML Orchard CMS and SQL Server
4 of 7
11/8/2011 11:02 AM
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
Here I will add a 4th city, Tampa, to the list of cities in Florida. This inserts it right into the Excel Worksheet as you would expect.
string connectionString = @"Provider=Microsoft.Jet. OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""; DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = connectionString; using (DbCommand command = connection.CreateCommand()) { command.CommandText = "INSERT INTO [Cities$] (ID, City, State) VALUES(4,\"Tampa\",\"Florida\")"; connection.Open(); command.ExecuteNonQuery(); } }
Let's modify the name of the first city from Bradenton to Venice in the Excel Spreadsheet using ADO.NET:
CE - Free Website Database Firefox Scratchpad for JavaScript Development New Orchard Theme and Website - Orchard CMS Web Developer HTML5 and CSS3 Support in Visual Studio 2010 jQuery Intellisense in Microsoft Expression Web 4 Web Design Using Expression Web 4 - Snippets Expression Web 4 SP2 Thumbnails for Images in Folder List CoffeeScript and Orchard CMS Module Development Creating a Google +1 Part and Custom Module in Orchard CMS - Part 1 Adding Google +1 and Twitter Social Networking Links to Orchard CMS Upgraded Website to Orchard 1.2 Orchard 1.2 Performance Settings - Warmup Module Goes Native Orchard 1.2 Released - Bug Fixes and Improved Performance Delete Content Types in Orchard CMS with Custom Module Add Widget Layer to Orchard CMS - Usability Workflow Improvement Custom Orchard Module that Plays HTML5 Video with Flash Fallback
string connectionString = @"Provider=Microsoft.Jet. OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""; DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = connectionString;
Categories
ADO.NET 2.0 ADO.NET 3.0 ADO.NET Data Services ADO.NET Entity Framework Agile & Iterative Development Agile Software Development Applying UML and Design Patterns
5 of 7
11/8/2011 11:02 AM
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
using (DbCommand command = connection.CreateCommand()) { command.CommandText = "Update [Cities$] Set City = \"Venice\" WHERE ID = 1"; connection.Open(); command.ExecuteNonQuery(); } }
Conclusion
ASP.NET ASP.NET 2.0 ASP.NET 3.5 ASP.NET Dynamic Data ASP.NET Security C# C# 2.0 C# 3.0 Code Generation Design Patterns C# LINQ to SQL - DLINQ O/R Mappers SQL Server SQL Server Management Objects ( SMO )
It is just too cool that we can use ADO.NET and the OleDb Managed Data Provider in the .NET Framework to insert, update, and delete information in an Excel Spreadsheet like it was a database. View my other ADO.NET Tutorials. Have a great Memorial Day weekend!
6 of 7
11/8/2011 11:02 AM
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
2011 DavidHayden.com
7 of 7
11/8/2011 11:02 AM