Professional Documents
Culture Documents
1 of 4
http://www.codeproject.com/Tips/1008064/Import-Excel-Data-to-DataTa...
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 view aspx pages without IIS implemented in C#
highlights off
Sign in
aspx excel
home
articles
quick answers
discussions
features
community
help
CPOL
Rate this:
3.50 (4 votes)
Here is an example of how to import full excel file 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 full Excel sheet into a datatable in C#. I have created an example of the same with
importing both *.xls & *.xlsx file. The common problem when using this method will be "The Microsoft.ACE.OLEDB.12.0
provider is not registered on the local machine". I have also added a solution for the same.
Go to top
Copy Code
2/18/2016 4:30 PM
Import Excel Data to DataTable and Display in Grid in C#, ASP.NET - C...
2 of 4
http://www.codeproject.com/Tips/1008064/Import-Excel-Data-to-DataTa...
runat="server" AllowPaging="false">
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
This will allow you to upload Excel file for import.
Now for importing Excel file, we will use "Microsoft.Jet.OLEDB.4.0" (for Excel fiel 97-03 format) &
"Microsoft.ACE.OLEDB.12.0". The core part of importing Excel file is to use the below two connections strings:
1.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Excel_Import
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
string Ext = Path.GetExtension(FileUploadControl.PostedFile.FileName);
if (Ext == ".xls" || Ext == ".xlsx")
{
lblErrorMessage.Visible = false;
string Name = Path.GetFileName(FileUploadControl.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + Name);
FileUploadControl.SaveAs(FilePath);
FillGridFromExcelSheet(FilePath, Ext, ddlIsHeaderExists.SelectedValue);
}
else
{
lblErrorMessage.Visible = true;
lblErrorMessage.InnerText = "Please upload valid Excel File";
ExcelGridView.DataSource = null;
ExcelGridView.DataBind();
}
}
}
private void FillGridFromExcelSheet(string FilePath, string ext, string isHader)
{
string connectionString = "";
if (ext == ".xls")
{
//For Excel 97-03
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
}
else if (ext == ".xlsx")
{
//For Excel 07 and greater
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;
Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
}
connectionString = String.Format(connectionString, FilePath, isHader);
Copy Code
Go to top
2/18/2016 4:30 PM
Import Excel Data to DataTable and Display in Grid in C#, ASP.NET - C...
3 of 4
http://www.codeproject.com/Tips/1008064/Import-Excel-Data-to-DataTa...
License
Go to top
This article, along with any associated source code and files, is licensed under
The Code Project Open License (CPOL)
Share
EMAIL
2/18/2016 4:30 PM
Import Excel Data to DataTable and Display in Grid in C#, ASP.NET - C...
4 of 4
http://www.codeproject.com/Tips/1008064/Import-Excel-Data-to-DataTa...
Search Comments
Profile popupsSpacing Relaxed Layout Open All
Go to top
Ciprian Beldi
13-Jul-15 23:32
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 ViewThread Permalink
5.00/5 (1 vote)
General
News
Suggestion
Question
Refresh
Bug
Answer
Chn Ngn ng
Joke
Layout: fixed |
fluid
Praise
Rant
Admin
Article Copyright 2015 by Tushar Parmar
Everything else Copyright CodeProject, 1999-2016
2/18/2016 4:30 PM