Solved

import excel to .net C#

Posted on 2013-01-26
5
300 Views
Last Modified: 2013-02-06
Hello,
I would like to import an excel document. The excel document contains 3 columns (Id, Name, Description).  I would like to show the data in a GridView.  I am using C#. Can you show an example how this is done?
0
Comment
Question by:johnkainn
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 38822151
Hi John,
What kind of gridview? winforms, wpf, asp?

If ASP (I am just guessing), you can read: http://www.geekcamp.us/articles/import_data_from_excel_into_gridview_using_aspnet.aspx
0
 
LVL 6

Expert Comment

by:esolve
ID: 38822706
Sorry I am not at my development PC now but this should get you started.

http://social.msdn.microsoft.com/Forums/da-DK/csharpgeneral/thread/45141eb3-9010-455c-a095-e4f4f709ca08
http://forums.asp.net/t/1536177.aspx


Just ensure that you're using the correct connectionstring.
"provider=Microsoft.ACE.OLEDB.12.0;data source=" + filepath + ";extended properties='Excel 12.0;HDR=YES;'";
0
 

Assisted Solution

by:azizoglua
azizoglua earned 166 total points
ID: 38823980
try this for winforms, for web just open last comment (dataGridView1.DataBindind();)

string connstr = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\excel.xlsx;Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(connstr);
string strSQL = "SELECT Id, Name, Description FROM [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
// dataGridView1.DataBindind();
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 167 total points
ID: 38825897
Have excel sheet with following headings UserName,Education,Location with data as below

UserName],[Education],[Location]
aaaa              MBA           chennai
bbbb             MCA            bangalore


Asp code

<asp:FileUpload ID="fileuploadExcel" runat="server" />&nbsp;&nbsp;
<asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"></asp:Label><br />
<asp:GridView ID="grvExcelData" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>

Open in new window



protected void btnImport_Click(object sender, EventArgs e)
{
string connString = "";
string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
string path = fileuploadExcel.PostedFile.FileName;
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT [UserName],[Education],[Location] FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
grvExcelData.DataSource = ds.Tables[0];
grvExcelData.DataBind();
da.Dispose();
conn.Close();
}

Open in new window

0
 
LVL 13

Accepted Solution

by:
jonnidip earned 167 total points
ID: 38834412
I would STRONGLY suggest you to take a look at: ExcelDataReader.
It can be used as any IDataReader and also lets you read any xls/xlsx format into DataTable.

I am sure you will not be disappointed!

Regards.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now