Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

import excel to .net C#

Posted on 2013-01-26
5
306 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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