[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

import excel to .net C#

Posted on 2013-01-26
5
Medium Priority
?
312 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
[X]
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
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 664 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 668 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 668 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 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