Connectivity of Excel 2013 to ASP.Net Web Application

Introduction

Thsi article describes how to access an Excel 2013 file in an ASP.NET Web Application. It will help you to access the Excel file information from your web application project.

Prerequisites

Please install the following Office drivers depending upon your Office file:

Now we will proceed using the following sections:

  • Excel file Creation
  • Web Application

Excel File Creation

Now we will create an Excel 2013 using the following procedure.

Step 1: Open Excel 2013

Step 2: Create some records as shown below:

Excel File

Web Application

Now we will create the ASP.NET Web Application with the following steps:

Step 1: Open Visual Studio.

Step 2: Create an ASP.NET Web Application with an Empty Project Template.

Step 3: Add a Web Form from the Solution Explorer and use a GridView in your web form.

Step 4: Add a class named DAL.CS and replace the boilerplate code with the following code:

 

  1. using System.Data;  
  2. using System.Data.OleDb;  
  3. namespace ExcelApplication  
  4. {  
  5.     public class DAL  
  6.     {  
  7.         OleDbDataAdapter DbAdap;  
  8.         DataTable dt;  
  9.         public DataTable Get_ExcelSheet()  
  10.         {  
  11. OleDbConnection DbCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Nimit\\ExcelApplication.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"");  
  12.             DbAdap = new OleDbDataAdapter("SELECT * FROM [Sheet1$]",DbCon);  
  13.             dt = new DataTable();  
  14.             DbAdap.Fill(dt);  
  15.             return dt;  
  16.         }  
  17.     }  
  18. }  

 

Step 5: In WebForm1.cs file, modify your code as in the following:

 

  1. using System;  
  2. namespace ExcelApplication  
  3. {  
  4.     public partial class WebForm1 : System.Web.UI.Page  
  5.     {  
  6.         DAL obj = new DAL();  
  7.         protected void Page_Load(object sender, EventArgs e)  
  8.         {  
  9.             if (!IsPostBack == true)  
  10.             {  
  11.                 Get_Data();  
  12.             }  
  13.         }  
  14.         void Get_Data()  
  15.         {  
  16.             GridView1.DataSource = obj.Get_ExcelSheet();  
  17.             GridView1.DataBind();  
  18.         }  
  19.     }  
  20. }  

 

Step 6: Open the Configuration Manager of your solution form the Solution Explorer.

Configuration Manager of Application

Step 7: Select the x86 platform for the application.

Active Solution Platform

Step 8: Debug the application.

Excel Page

Note: Please ensure that the Excel file is closed when you run the application.

Summary

This article will help you to access the Office features like Excel in your ASP.NET Web Application. Happy Coding, Thanks for reading.


Similar Articles