What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 21886 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Reading, Opening Excel File in C# Using Microsoft.Office.Introp.Excel Object

Reading, Opening Excel File in C# Using Microsoft.Office.Introp.Excel Object

2 vote(s)
Rating: 4.5 out of 5
Article posted by Hiremath on 5/23/2011 | Views: 77052 | Category: ASP.NET | Level: Beginner | Points: 250 red flag


Here i am trying to help you to how to open and read the EXCEL File and bind the data to gridview server control in asp.net using C# language.
Its very easy way compared to others.

Download


 Download source code for Reading, Opening Excel File in C# Using Microsoft.Office.Introp.Excel Object


Introduction

Hi all. Here Iam trying to help to open and read the Excel file in ASP.NET Using C# 3.5 language. In My solution I am using  Mirosoft.Office.Interop.Excel Object by adding reference from .net tab.

References


Namespace:
Using Excel=Microsoft.Office.Interop.Excel; 
Using System.Reflection;

   Missing.Value attribute is used in this code as parameter in functions because in C# 3.5 optional parameters are not allowed.( Else you may get error "No Overload Method 'Open' takes single arguments")

Procedure:

1> Creat a Excel file which contain user Information Ex: Id,Name,Status,Contact (FileName: Mydata.xls)

2>Add this File to your Project.

3> Add Reference on Right click on References and select Add references and click .net tab and add

Microsoft.Office.Interop.Excel Object.

4> Add GridView in desing (.aspx) file.

   

Code to Open and Read Data From Excel File.


Use the following code in page load in your aspx.cs file.

if (!Page.IsPostBack)
            {
                Excel.Application appExl;
                Excel.Workbook workbook;
                Excel.Worksheet NwSheet;
                Excel.Range ShtRange;
                appExl = new Excel.ApplicationClass();
 

                  //Opening Excel file(myData.xlsx)
                workbook = appExl.Workbooks.Open(Server.MapPath("myData.xlsx"), Missing.Value, Missing.Value,Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
                int Cnum = 0;
                int Rnum = 0;
     
                ShtRange = NwSheet.UsedRange; //gives the used cells in sheet
               
                  //Reading Excel file.
               //Creating datatable to read the containt of the Sheet in File.
                DataTable dt = new DataTable();
                dt.Columns.Add("ID");
                dt.Columns.Add("Name");
                dt.Columns.Add("Status");
                dt.Columns.Add("Contact");

                for (Rnum = 2; Rnum <= ShtRange.Rows.Count; Rnum++)
                {
                    DataRow dr = dt.NewRow();
//Reading Each Column value From sheet to datatable Colunms                  
                    for (Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
                 {
                       dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
                    }
                    dt.Rows.Add(dr); // adding Row into DataTable
                    dt.AcceptChanges();
                }

                workbook.Close(true, Missing.Value, Missing.Value);
                appExl.Quit();

                 gvOne.DataSource = dt;//DataSource to GrigView(Id:gvOne)
                gvOne.DataBind();
      }


Conclusion

I tried this above code in my project and its working well. Even you can Edit this data in GridView and Youcan Update  the changes in Same Excel file.

Hope its helps you. thank you.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About hiremath V.K

Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Friday, April 29, 2011
Level:Starter
Status: [Member]
Biography:
 Responses
Posted by: Akiii | Posted on: 23 May 2011 09:09:27 AM | Points: 25

Hi Hiremath,

Could you please clarify, what is "Missing.Value" in your code ?

Thanks and Regards
Akiii

Posted by: Akiii | Posted on: 23 May 2011 09:21:54 AM | Points: 25

hi....
i have tested the code and it is working without the "missing.value" !
I have used
workbook = appExl.Workbooks.Open(Server.MapPath("myData.xlsx"));


I see this is your first article, keep it up...

Thanks and Regards
Akiii

Posted by: Rashmi.mb | Posted on: 24 May 2011 09:45:22 AM | Points: 25

Nice to see u here.....p2

Posted by: Hiremath | Posted on: 25 May 2011 06:55:13 AM | Points: 25

HI All
"Missing.value " is used because to avoid Error LIke "NO OverLoad Method 'Open' takes 1 argument(s)".
(I am using vs 2008 and c# 3.0)
Thank you
Hiremath V.K

Posted by: Hiremath | Posted on: 25 May 2011 06:57:12 AM | Points: 25

@Akiii
hi
i tried without using "Missing.Value" but i got an Error.

Thank you.

Posted by: Akiii | Posted on: 25 May 2011 09:53:22 AM | Points: 25

hi hiremath....

its working fine for me.....i am using VS 2010......

Regards
Akiii

Posted by: Akiii | Posted on: 25 May 2011 11:49:52 PM | Points: 25

what error did u get if you didn't write the "Missing.value" ??

Regards
Akiii

Posted by: Hiremath | Posted on: 26 May 2011 04:05:03 AM | Points: 25

hi Akiii
Error is
" Error LIke "NO OverLoad Method 'Open' takes 1 argument(s)".
(
at this line
appExl.Workbooks.Open(Server.MapPath("myData.xlsx"));
getting boss
)
May be vorsion problem. plz once try in vb2008 and c# 3.0
Thankyou.





Posted by: Sudhindra18 | Posted on: 10 Jun 2011 06:54:40 AM | Points: 25

Excellent Article hiremath... Keep writing it up..

Sudhindra

Posted by: Ashwini.nerkar | Posted on: 05 Sep 2011 08:52:21 AM | Points: 25

Thanks ! It helped me a lot.

Posted by: Vignesh.jvm | Posted on: 20 Dec 2011 08:43:02 AM | Points: 25

Hi, Thanks to Post.

In case if empty the data its through the error. using this
dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
so use, before the typecast
dr[Cnum - 1] = (string) (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2;

Thank You! May be its help others.

Posted by: Ybonda | Posted on: 14 Jun 2012 10:52:11 AM | Points: 25

Here is nice solution (C# Read Excel and Show in WPF DataGrid): http://www.codearsenal.net/2012/06/c-sharp-read-excel-and-show-in-wpf.html

>> Write Response - Respond to this post and get points
Related Posts

Type Fundamentals

Hi, The other day I was facing problem while migrating my Sql Server database tables into MySql database. I tried to google it but couldn't find any great solution that can do it through code easily. Thought to share this simple code to all of you.

In this article we are going to see the fileupload without postback.. We are going to do this by using ICallbackEventHandler..

Ask any developer which is the best place in a .NET class to clean unmanaged resources?, 70% of them will say the destructor. Although it looks the most promising place for cleanup it has a huge impact on performance and memory consumption. Writing clean up code in the destructor leads to double GC visits and thus affecting the performance multifold times. In order to validate the same we will first start with bit of theory and then we will actually see how GC algorithm performance is impacted using destructor. So we will first understand the concept of generations and then we will see the finalize dispose pattern.

In this article we shall learn How to create a slide show images from the database in ASP.NET.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 2:35:33 PM