Sunteți pe pagina 1din 7

Read Excel File into DataSet in ASP.

NET Using C# - CodeProject

http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...

Not quite what you are looking for? You may want to try: Fast Excel file reader with basic functionality AnyDataFileToXmlConverter Class/Utility
9,890,640 members (52,431 online)

highlights off

home features

articles

quick answers help

discussions
read excel spreadsheet in c# and display into datatable

community

Articles Web Development ASP.NET Samples

Next

Tip Browse Code Stats Revisions Alternatives Comments & Discussions (8)

Read Excel File into DataSet in ASP.NET Using C#


By Mannava Siva Aditya, 14 Dec 2012
4.20 (5 votes)

About Article
How to read Excel file data into a DataSet in ASP.NET using C#. Type Licence Tip/Trick CPOL 14 Dec 2012 17,351 14 times

Introduction
In this example I will explain how to read Excel file data into a DataSet in ASP.NET using C#. I have created an Excel file which contains the Slno, FirstName, LastName and Location which is shown below.

First Posted Views

Add your own alternative version

Bookmarked

C# ASP.NET Dev Intermediate

We would be putting all the data into the grid view and with the help of filter with the dropdown list box, we would filter the Slno and display accordingly, else display all the cell values in the grid. ASP:
Collapse | Copy Code

1 of 7

25/05/2013 01:11

Read Excel File into DataSet in ASP.NET Using C# - CodeProject

http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...

<asp:DropDownList ID="ddlSlno" runat="server" OnSelectedIndexChanged="ddlSlno_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="True"> <asp:ListItem Selected="True" Value="Select">- Select -</asp:ListItem> </asp:DropDownList> <asp:GridView ID="grvData" runat="server"> </asp:GridView>

C# code:
Collapse | Copy Code

OleDbConnection oledbConn; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GenerateExcelData("Select"); } } protected void ddlSlno_SelectedIndexChanged(object sender, EventArgs e) { GenerateExcelData(ddlSlno.SelectedValue); } private void GenerateExcelData(string SlnoAbbreviation) { // need to pass relative path after deploying on server string path = System.IO.Path.GetFullPath(@"C:\InformationNew.xls"); /* connection string to work with excel file. HDR=Yes - indicates that the first row contains columnnames, not data. HDR=No - indicates the opposite. "IMEX=1;" tells the driver to always read "intermixed" (numbers, dates, strings etc) data columns as text. Note that this option might affect excel sheet write access negative. */ oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES; IMEX=1;';"); oledbConn.Open(); OleDbCommand cmd = new OleDbCommand(); ; OleDbDataAdapter oleda = new OleDbDataAdapter(); DataSet ds = new DataSet(); // selecting distict list of Slno cmd.Connection = oledbConn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT distinct([Slno]) FROM [Sheet1$]"; oleda = new OleDbDataAdapter(cmd); oleda.Fill(ds, "dsSlno"); // passing list of states to drop-down list ddlSlno.DataSource = ds.Tables["dsSlno"].DefaultView; ddlSlno.DataTextField = "Slno"; ddlSlno.DataValueField = "Slno"; ddlSlno.DataBind();

Top News
The Next Version of Android - Some of What's Coming
Get the Insider News free each morning.

Related Videos

Learn .NET and C# in 60 days Lab 11(Day 4) :Displaying Customer Screen

2 of 7

25/05/2013 01:11

Read Excel File into DataSet in ASP.NET Using C# - CodeProject

http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...

// by default we will show form data for all states but if any state is selected then show data accordingly if (!String.IsNullOrEmpty(SlnoAbbreviation) && SlnoAbbreviation != "Select") { cmd.CommandText = "SELECT [Slno],[FirstName], [LastName],[Location]" + " FROM [Sheet1$] where [Slno]= @Slno_Abbreviation"; cmd.Parameters.AddWithValue("Slno_Abbreviation", SlnoAbbreviation); } else { cmd.CommandText = "SELECT [Slno],[FirstName], [LastName],[Location] FROM [Sheet1$]"; } oleda = new OleDbDataAdapter(cmd); oleda.Fill(ds); // binding form data with grid view grvData.DataSource = ds.Tables[0].DefaultView; grvData.DataBind(); } // need to catch possible exceptions catch (Exception ex) {} finally { oledbConn.Close(); } }// close of method GemerateExceLData

Displaying JSON Data

Related Articles
CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet Fast Excel file reader with basic functionality BasicExcel - A Class to Read and Write to Microsoft Excel Converting a List of Data to XML using Microsoft Excel 2003 How to create Microsoft Excel 2007 files on the server Microsoft Office XML formats, defective by design? Yet another way to Generate Excel documents programmatically Creating basic Excel workbook with Open XML Excel to SQL without JET or OLE (Version 2) Using ASP.NET MVC and the OpenXML API to Stream Excel Files A Quick Guideline for Microsoft Windows PowerShell: Part 3 RefEdit Emulation for .NET

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


Mannava Siva Aditya
Web Developer India Member
Follow on Twitter Google

AnyDataFileToXmlConverter Class/Utility NASA Space Shuttle TV Schedule Transfer to Outlook Calendar DataTable to Excel How to Read and Write ODF/ODS Files (OpenDocument Spreadsheets) Placing images in Excel using automation Excel to SQL without JET or OLE Displaying Charts in SharePoint using Excel Services Read Excel in ASP.NET

I am a 28 year old software web developer from Hyderabad, India. I have been working since approximately age 25. Where as in IT Development industry since 27. I am Microsoft Certified Technology Specialist. I have taught myself in development, beginning with Microsoft's technologies ASP.NET, Approximately 3 years ago, I

ResxWriter: Generating .resx files from an Excel spreadsheet

3 of 7

25/05/2013 01:11

Read Excel File into DataSet in ASP.NET Using C# - CodeProject

http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...

was given an opportunity to work as a freelance in the tech field. Now I am working as a web developer where my roles make me purely in web based technology solutions which manage and control access to applications and patient information stored in legacy systems, client-server applications. I too had an opportunity to train some IT professionals with technical skills in development area. Which became my passion. I have worked on various .NET framework versions(2.0 , 3.5, 4.0) and have been learning every new technology being introduced. Currently, I am looking forward to working in R & D in .Net to create distributed, reusable applications.

Article Top
Like 3 1 Tweet 7

Rate this:

Poor

Excellent

Vote

Comments and Discussions


Add a Comment or Question
Search this forum Profile popups Spacing Relaxed
Open All

Go Noise Medium Per page 25 Layout Update

First Prev Next

Thanks
Good Job

Alireza_1362

12 May '13 - 6:54

Reply View Thread Permalink Bookmark

My vote of 1

JamesPChadwick

21 Dec '12 - 10:39

Why not make use of the Interop Assemblies for Office?

4 of 7

25/05/2013 01:11

Read Excel File into DataSet in ASP.NET Using C# - CodeProject

http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...

Reply View Thread Permalink Bookmark

Where do you get the provider?

xx_Sandman_xx

17 Dec '12 - 10:28

Do you have Excel installed on your web server or are you installing something like Access Database Engine? http://www.microsoft.com/en-us/download /details.aspx?id=23734[^] Did you run into any field limitations? My impression is that there is a 255 character limit AND the maximum length of the first 8 rows in a column sets the maximum for the document. So if row 9 has 255 characters only the initial max length is available...
Reply Email View Thread Permalink Bookmark

Re: Where do you get the provider?

Mannava Siva Aditya

17 Dec '12 - 19:58

new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES; IMEX=1;';"); This it self is the provider for microsoft excel. And comming to field limitation it will read what ever data is been entered!
Reply Email View Thread Permalink Bookmark

Re: Where do you get the provider?

xx_Sandman_xx

18 Dec '12 - 12:05

In general this is a good start but it is incomplete and the responses to my questions are incorrect. Perhaps you missunderstood my question about the ACE provider but any provider needs to have a driver installed on the computer. I'm not sure how you think this all works but the provider is not part of .net. In search of some reference

5 of 7

25/05/2013 01:11

Read Excel File into DataSet in ASP.NET Using C# - CodeProject

http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...

material outside of my statements I discovered a very complete article here. http://yoursandmyideas.wordpress.com/2011/02 /05/how-to-read-or-write-excel-file-usingace-oledb-data-provider/[^] In the future I would suggest if you don't understand the question or know the answer not to post guess as fact.
Reply Email View Thread Permalink Bookmark

My vote of 5

Carsten V2.0

14 Dec '12 - 22:40

Very useful tipp! Thanks for sharing... If I am right your connection-string supports only XLSX-documents. I think it would be helpful to post the string for older versions, too.
Reply View Thread Permalink Bookmark

Re: My vote of 5

Mannava Siva Aditya

16 Dec '12 - 19:27

It would support both xls and xlsx. as we had taken the latest one.
Reply Email View Thread Permalink Bookmark

Re: My vote of 5

Carsten V2.0

16 Dec '12 - 22:05

Ok, sorry! It was my bad... Nevertheless a very useful tipp!


Reply View Thread Permalink Bookmark

Last Visit: 23 May '13 - 6:50 19:36

Last Update: 23 May '13 -

Refresh

General News Suggestion Answer Joke Rant Admin


Permalink | Advertise | Privacy | Mobile Web01 | 2.6.130523.1 | Last Updated 14 Dec 2012

Question

Bug

Layout: fixed | fluid

Article Copyright 2012 by Mannava Siva Aditya Everything else Copyright CodeProject, 1999-2013 Terms of Use

6 of 7

25/05/2013 01:11

Read Excel File into DataSet in ASP.NET Using C# - CodeProject

http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...

7 of 7

25/05/2013 01:11

S-ar putea să vă placă și