DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Read Excel Files Using Microsoft Office Interop Assemblies in ASP.NET C#

Read Excel Files Using Microsoft Office Interop Assemblies in ASP.NET C#

Read through the quick tutorial to learn how to read Excel files and store them in SQL Server with Office Interop Assemblies and a bit of C#.

Karthik Elumalai user avatar by
Karthik Elumalai
·
Jul. 23, 16 · Tutorial
Like (1)
Save
Tweet
Share
12.08K Views

Join the DZone community and get the full member experience.

Join For Free

background

a few days ago, i was required to read excel files and store those values in our sql server database. so, in this example am going to show how to get four basic import data types: excel work book name, worksheet count in that workbook, name of the first worksheet, and finally the value of the first cell in that worksheet.

prerequisites

kindly ensure you add the following .dll as shown below.


add


add


namespace

using excel = microsoft.office.interop.excel;

c# code

protected void btngetexcelfiledetails_click(object sender, eventargs e)   
{  
    try   
    {  
        //create a instance for the excel object  
        excel.application oexcel = new excel.application();  

        //specify the file name where its actually exist  
        string filepath = @ "d:\tpms\uploaded_boq\raveena_boq_from_db.xlsx";  

        //pass that to workbook object  
        excel.workbook wb = oexcel.workbooks.open(filepath);  


        // statement get the workbookname  
        string excelworkbookname = wb.name;  

        // statement get the worksheet count  
        int worksheetcount = wb.worksheets.count;  

        excel.worksheet wks = (excel.worksheet) wb.worksheets[1];  

        // statement get the firstworksheetname  

        string firstworksheetname = wks.name;  

        //statement get the first cell value  
        var firstcellvalue = ((excel.range) wks.cells[1, 1]).value;  

    } catch (exception ex)   
    {  

        string error = ex.message;  
    }  

}  


i hope the above information was useful! please let me know your thoughts.

ASP.NET Microsoft Office Database

Published at DZone with permission of Karthik Elumalai. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Easy Smart Contract Debugging With Truffle’s Console.log
  • Using AI and Machine Learning To Create Software
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Kubernetes vs Docker: Differences Explained

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: