Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is this possible to display selcted excel sheet data into datagrid without database. send me snippet if there
Posted
Updated 12-Jun-13 18:17pm
v2
Comments
_Amy 13-Jun-13 0:22am    
Don't be so lazy. Try doing something by your own. Search in Google, read the articles and try the codes. If you stuck anywhere then only ask the questions.

Hi,
Check this out

string connString = "";
string strFileType = ".xlsx";
string path = @"C:\Usr\Book1.xlsx";
//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\"";
}

OleDbConnection con = new OleDbConnection(connString);
OleDbCommand cmdnew = new OleDbCommand();
cmdnew.CommandType = System.Data.CommandType.Text;
cmdnew.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmdnew);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmdnew.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmdnew;
dAdapter.Fill(dtExcelRecords);
Grd.DataSource = dtExcelRecords;
Grd.DataBind();
con.Close();
con.Dispose();
 
Share this answer
 
 
Share this answer
 
check out this solution to display selected excel sheet data into gridview. I think its helping to you

string sourceFile = "", worksheet = "SheetName";

sourceFile = Server.MapPath("~//Files//demo09.xls");

string strcnn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + sourceFile + ";Extended Properties=Excel 8.0";

OleDbConnection cnn = null;
OleDbCommand cmd = null;
OleDbDataAdapter da = null;


cnn = new OleDbConnection(strcnn);
cnn.Open();

cmd = new OleDbCommand("Select * from [" + worksheet + "$]", cnn);
cmd.CommandType = CommandType.Text;


da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

if(dt.Rows.Count != 0)
{
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
 
Share this answer
 

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