Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating a Windows Forms application and i want to open a Excel sheet using OpenFileDialog and display its contents to a gridview/ListView. Please Help Me...
Posted
Updated 12-Aug-13 21:17pm
v2

 
Share this answer
 
v5
C#
using System.Data;
using System.Data.OleDb;

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from MyObject", con);
DataTable dt = new DataTable();
da.Fill(dt);
 
Share this answer
 
v2
Comments
Maciej Los 12-Aug-13 9:27am    
Please, use text formatting. You aren't newbie.
ridoy 12-Aug-13 9:37am    
done,often they find it difficult to format code.
fjdiewornncalwe 16-Aug-13 9:55am    
It's difficult for him because he is simple plagiarizing answers from other sites as his own without credit. (Source) He seems to have a habit of this. I've been looking at recent answers and they are pretty much all copied without credit.
ridoy 16-Aug-13 13:23pm    
agree with you.i have seen that things also.
Change your Excel sheet into CSV file and try this:
C#
FileUpload1.SaveAs(Server.MapPath("~/tmpdir/") + FileUpload1.FileName);
System.IO.StreamReader myFile = new System.IO.StreamReader(Server.MapPath("~/tmpdir/")    + FileUpload1.FileName);
string myString = myFile.ReadToEnd();
myFile.Close();
string[] lines = myString.Split(new string[] { Environment.NewLine },      StringSplitOptions.None);
 
foreach (string line in lines)
 {
     if (line != "")
      {
          string[] columns = line.Split(',');
 
          //Upload to your DB with your indexes

      }
     else
      {
 
      }
}
 
Share this answer
 
Refer this link. It is exactly the same what you need.

Import Data from Excel to DataGridView in C#[^]

Regards..:)
 
Share this answer
 
Comments
Maciej Los 12-Aug-13 9:21am    
Looks promisingly ;)
+5!
Thanks7872 12-Aug-13 12:30pm    
Thanks Maciej. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900