Upload
manoj-kumar-g
View
71
Download
0
Embed Size (px)
DESCRIPTION
Read Excel File Into DataSet in ASP.net Using C#
Citation preview
highlights off
9,890,640 members (52,431 online)
Not quite what you are looking for? You may want to try:Fast Excel file reader with basic functionalityAnyDataFileToXmlConverter Class/Utility
×
home quick answers discussions
features community helpread excel spreadsheet in c# and display into datatable
Articles » Web Development » ASP.NET » Samples
Tip
Browse Code
Stats
Revisions
Alternatives
Comments &Discussions (8)
Add your ownalternative version
About Article
How to read Excel file datainto a DataSet in ASP.NETusing C#.
Type Tip/Trick
Licence CPOL
First Posted 14 Dec 2012
Views 17,351
Bookmarked 14 times
C# ASP.NET Dev
Intermediate
Next
Read Excel File into DataSet inASP.NET Using C#By Mannava Siva Aditya, 14 Dec 2012
IntroductionIn this example I will explain how to read Excel file data into aDataSet in ASP.NET using C#.
I have created an Excel file which contains the Slno, FirstName,LastName and Location which is shown below.
We would be putting all the data into the grid view and withthe help of filter with the dropdown list box, we would filterthe Slno and display accordingly, else display all the cellvalues in the grid.
ASP:
Collapse | Copy Code
4.20 (5 votes)
articles
Read Excel File into DataSet in ASP.NET Using C# - CodeProject http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...
1 of 7 25/05/2013 01:11
Top News
The Next Version ofAndroid - Some of What'sComing
Get the Insider News free eachmorning.
Related Videos
Learn.NET and C# in 60 days -Lab 11(Day 4) :-Displaying CustomerScreen
<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();
Read Excel File into DataSet in ASP.NET Using C# - CodeProject http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...
2 of 7 25/05/2013 01:11
Displaying JSON Data
Related ArticlesCSpreadSheet - A Class to Readand Write to Excel and TextDelimited Spreadsheet
Fast Excel file reader with basicfunctionality
BasicExcel - A Class to Read andWrite to Microsoft Excel
Converting a List of Data toXML using Microsoft Excel 2003
How to create Microsoft Excel2007 files on the server
Microsoft Office XML formats,defective by design?
Yet another way to GenerateExcel documentsprogrammatically
Creating basic Excel workbookwith Open XML
Excel to SQL without JET or OLE(Version 2)
Using ASP.NET MVC and theOpenXML API to Stream ExcelFiles
A Quick Guideline for MicrosoftWindows PowerShell: Part 3
RefEdit Emulation for .NET
AnyDataFileToXmlConverterClass/Utility
NASA Space Shuttle TVSchedule Transfer to OutlookCalendar
DataTable to Excel
How to Read and WriteODF/ODS Files (OpenDocumentSpreadsheets)
Placing images in Excel usingautomation
Excel to SQL without JET or OLE
Displaying Charts in SharePointusing Excel Services
Read Excel in ASP.NET
ResxWriter: Generating .resxfiles from an Excel spreadsheet
// 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 exceptionscatch (Exception ex){}finally{ oledbConn.Close();}}// close of method GemerateExceLData
LicenseThis article, along with any associated source code and files, islicensed under The Code Project Open License (CPOL)
About the Author
Mannava SivaAdityaWeb Developer
India Member
Follow on Twitter Google
I am a 28 year old software web developer from Hyderabad,India. I have been working since approximately age 25. Whereas in IT Development industry since 27. I am MicrosoftCertified Technology Specialist.
I have taught myself in development, beginning withMicrosoft's technologies ASP.NET, Approximately 3 years ago, I
Read Excel File into DataSet in ASP.NET Using C# - CodeProject http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...
3 of 7 25/05/2013 01:11
Article Top
1 Tweet 7
Rate this: Poor Excellent Vote
Add a Comment or Question
Search this forum Go
was given an opportunity to work as a freelance in the techfield. Now I am working as a web developer where my rolesmake me purely in web based technology solutions whichmanage and control access to applications and patientinformation stored in legacy systems, client-serverapplications. I too had an opportunity to train some IT professionals withtechnical skills in development area. Which became mypassion. I have worked on various .NET framework versions(2.0 , 3.5,4.0) and have been learning every new technology beingintroduced. Currently, I am looking forward to working in R &D in .Net to create distributed, reusable applications.
Comments and Discussions
Profile popups Spacing Relaxed Noise Medium Layout
Open All Per page 25 Update
First Prev Next
Alireza_1362 12 May '13 - 6:54
Good Job
Reply · View Thread · Permalink · Bookmark
JamesPChadwick 21 Dec '12 - 10:39
Why not make use of the Interop Assemblies for Office?
Like 3
Thanks
My voteof 1
Read Excel File into DataSet in ASP.NET Using C# - CodeProject http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...
4 of 7 25/05/2013 01:11
Reply · View Thread · Permalink · Bookmark
xx_Sandman_xx 17 Dec '12 - 10:28
Do you have Excel installed on your web server or areyou 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 isthat there is a 255 character limit AND the maximumlength of the first 8 rows in a column sets the maximumfor the document. So if row 9 has 255 characters onlythe initial max length is available...
Reply · Email · View Thread · Permalink · Bookmark
Mannava Siva Aditya 17 Dec '12 - 19:58
newOleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=" +path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");This it self is the provider for microsoft excel. And comming tofield limitation it will read what ever data is been entered!
Reply · Email · View Thread · Permalink · Bookmark
xx_Sandman_xx 18 Dec '12 - 12:05
In general this is a good start but it is incompleteand the responses to my questions are incorrect.Perhaps you missunderstood my question aboutthe ACE provider but any provider needs to havea driver installed on the computer. I'm not surehow you think this all works but the provider isnot part of .net. In search of some reference
Wheredo youget theprovider?
Re:Wheredo youget theprovider?
Re:Wheredo youget theprovider?
Read Excel File into DataSet in ASP.NET Using C# - CodeProject http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...
5 of 7 25/05/2013 01:11
Permalink | Advertise | Privacy | MobileWeb01 | 2.6.130523.1 | Last Updated 14 Dec 2012
Article Copyright 2012 by Mannava Siva AdityaEverything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid
material outside of my statements I discovered avery complete article here.http://yoursandmyideas.wordpress.com/2011/02/05/how-to-read-or-write-excel-file-using-ace-oledb-data-provider/[^]In the future I would suggest if you don'tunderstand the question or know the answer notto post guess as fact.
Reply · Email · View Thread · Permalink · Bookmark
Carsten V2.0 14 Dec '12 - 22:40
Very useful tipp!Thanks for sharing... If I am right your connection-string supports onlyXLSX-documents. I think it would be helpful to post thestring for older versions, too.
Reply · View Thread · Permalink · Bookmark
Mannava Siva Aditya 16 Dec '12 - 19:27
It would support both xls and xlsx. as we had takenthe latest one.
Reply · Email · View Thread · Permalink · Bookmark
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 Last Update: 23 May '13 -19:36 Refresh 1
General News Suggestion Question Bug Answer Joke Rant Admin
My voteof 5
Re: Myvote of5
Re:Myvoteof 5
Read Excel File into DataSet in ASP.NET Using C# - CodeProject http://www.codeproject.com/Tips/509179/Read-Excel-File-into-DataSe...
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