import excel data to datatable and display in grid in c#, asp

Upload: thinh-tran-van

Post on 24-Feb-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 Import Excel Data to DataTable and Display in Grid in C#, ASP

    1/4

    highlig

    Not quite what you are looking for? You may want to try:

    Display MS Excel Sheets and Charts in ASPX Pages using C#

    A Simple protocol to v iew aspx pages without IIS implemented in C#

    aspx excel

    home quick answers discussions features community helparticles

    Articles Web Development ASP.NET Utilities

    Tushar Parmar, 13 Jul 2015 CPOL Rate this:

    Import ExcelData to DataTable and Display in Grid in C#,

    ASP.NET

    Here is an example of how to import fullexcelfile into a DataTable and bind it with a data grid. also the solution for error "The

    Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine" is included.

    Introduction

    The example shows how you can directly import fullExcelsheet into a datatable in C#. I have created an example of the same with

    importing both *.xls& *.xlsxfile. The common problem when using this method will be "The Microsoft.ACE.OLEDB.12.0provider is not registered on the local machine". I have also added a so lution for the same.

    Using the CodeLet us create GUI for the same as below on anaspxpage:

    Hide Shrink Copy Code

    YesNo

  • 7/24/2019 Import Excel Data to DataTable and Display in Grid in C#, ASP

    2/4

    runat="server" AllowPaging="false">

    This will allow you to upload Excelfile for import.

    Now for importing Excelfile, we will use "Microsoft.Jet.OLEDB.4.0" (for Excelfiel 97-03 format) &"Microsoft.ACE.OLEDB.12.0". The core part of importing Excelfile is to use the below two connections strings:

    Hide Copy Code

    1.

    Hide Copy Code

    2.

    Now our C# code behind file will be as shown below:

    Hide Shrink Copy Code

    usingSystem;usingSystem.Collections.Generic;

    usingSystem.Configuration;usingSystem.Data;usingSystem.Data.OleDb;usingSystem.IO;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespace Excel_Import{

    public partial classWebForm1 : System.Web.UI.Page {

    protected voidPage_Load(objectsender, EventArgs e) { }

    protected voidbtnUpload_Click(objectsender, EventArgs e) {

    if(FileUploadControl.HasFile)

    {stringExt = Path.GetExtension(FileUploadControl.PostedFile.FileName);if(Ext == ".xls"|| Ext == ".xlsx")

    { lblErrorMessage.Visible = false;

    stringName = Path.GetFileName(FileUploadControl.PostedFile.FileName);stringFolderPath = ConfigurationManager.AppSettings["FolderPath"];stringFilePath = Server.MapPath(FolderPath + Name);

    FileUploadControl.SaveAs(FilePath); FillGridFromExcelSheet(FilePath, Ext, ddlIsHeaderExists.SelectedValue); }

    else { lblErrorMessage.Visible = true; lblErrorMessage.InnerText = "Please upload valid ExcelFile";

    ExcelGridView.DataSource = null;ExcelGridView.DataBind();

    } } }

    private voidFillGridFromExcelSheet(stringFilePath, stringext, stringisHader) {

    stringconnectionString = "";if(ext == ".xls")

    { //For Excel97-03 connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties='Excel8.0;HDR={1}'"; }

    else if(ext == ".xlsx") { //For Excel07 and greater connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};Extended Properties='Excel8.0;HDR={1}'"; } connectionString = String.Format(connectionString, FilePath, isHader);

    rt Excel Data to DataTable and Display in Grid in C#, ASP.NET - C... http://www.codeproject.com/Tips/1008064/Import-Excel-Data-to

    2/18/2016

  • 7/24/2019 Import Excel Data to DataTable and Display in Grid in C#, ASP

    3/4

    OleDbConnection conn = newOleDbConnection(connectionString); OleDbCommand cmd = newOleDbCommand(); OleDbDataAdapter dataAdapter = newOleDbDataAdapter(); DataTable dt = newDataTable(); cmd.Connection = conn;

    //Fetch 1st Sheet Name

    conn.Open(); DataTable dtSchema; dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

    string ExcelSheetName = dtSchema.Rows[0]["TABLE_NAME"].ToString(); conn.Close();

    //Read all data of fetched Sheet to a Data Table

    conn.Open();

    cmd.CommandText = "SELECT * From ["+ ExcelSheetName + "]"; dataAdapter.SelectCommand = cmd; dataAdapter.Fill(dt); conn.Close();

    //Bind Sheet Data to GridView

    ExcelGridView.Caption = Path.GetFileName(FilePath);ExcelGridView.DataSource = dt;ExcelGridView.DataBind();

    } }}

    This is how we can add Excelimport with C#.

    There will be a common error which I faced after using "Microsoft.ACE.OLEDB.12.0" is "TheMicrosoft.ACE.OLEDB.12.0 provider is not registered on the local machine". I have a clear solution for that.

    Download the below two EXE files:

    https://www.microsoft.com/en-in/download/details.aspx?id=132551.

    https://www.microsoft.com/en-in/download/details.aspx?id=237342.

    Install both of them & restart the machine once.

    Whoo... Error gone...

    Enjoy!

    License

    This article, along with any associated source code and files, is licensed under

    The Code Project Open License (CPOL)

    Share

    About the Author

    EMAIL TWITTER

    Tushar ParmarUnited States

    No Biography provided

    rt Excel Data to DataTable and Display in Grid in C#, ASP.NET - C... http://www.codeproject.com/Tips/1008064/Import-Excel-Data-to

    2/18/2016

  • 7/24/2019 Import Excel Data to DataTable and Display in Grid in C#, ASP

    4/4

    Permalink| Advertise | Privacy| Terms of Use| Mobile

    Web04 | 2.8.160217.1 | Last Updated 13 Jul 2015Chn Ngn ng

    Article Copyright 2015 by Tushar Parmar

    Everything else Copyright CodeProject, 1999-2016

    Layout: fixed|

    fluid

    Search Comments Go

    You may also be interested in...

    Display MS Excel Sheets and Charts

    in ASPX Pages using C#

    ASPxGridView Excel style -

    Extending functionality

    A Simple protocol to view aspx

    pages without IIS implemented in

    C#

    Intel IoT Gateways: Publishing

    Data to an MQTT Broker Using

    Python

    Behind the scenes of ASPX files Intel IoT Gateway: Windows 10

    Getting Started Guide

    Comments and Discussions

    You must Sign Into use this message board.

    Profile popups Spacing Relaxed Layout Open All Per page 25 Update

    First Prev Next

    Ciprian Beldi 13-Jul-15 23:32

    Message Automatically Removed

    modified 14-Jul-15 8:01am.

    Joan Magnet 13-Jul-15 0:55

    Hi, you can remove the error by forcing the compiler to select the default platform target: x86.

    Project properties - Build - Platform target: Any CPU -> x86

    Sign In View Thread Permalink 5.00/5 (1 vote)

    Last Visit: 31-Dec-99 19:00 Last Update: 17-Feb-16 18:25 Refresh 1

    General News Suggestion Question Bug Answer Joke Praise Rant Admin

    Message Automatically Removed

    Another way to remove the error

    rt Excel Data to DataTable and Display in Grid in C#, ASP.NET - C... http://www.codeproject.com/Tips/1008064/Import-Excel-Data-to

    2/18/2016