简体   繁体   中英

Import data from any excel file to gridview

I want to import any Excel file which I will browse and want to show it in ASP.Net GridView Server Controls.

But I think my connectionstring is giving me problem.

When I run, it gives " data source name not found and no default driver specified ".

I am using ODBC data source provider which is Microsoft Excel driver .

Here is the connection string which I wrote. "path" is the path which I will extract when the file is uploaded.

connString ="Driver={Microsoft Excel Driver(*.xls,*xlsx,*.xlsm,*.xlsb)}; Dbq=";
connString = connString + path +";Dsn=dsn;defaultdir=D:\\Users\\avantika.borikar;Extended Properties=Excel 12.0;HDR=Yes;IMEX=2;" ; 

Try This

suppose you have excel sheet with two columns "Name" and "Location" open excel sheet select both the columns and assign a name .. suppose "mylist"

How to Assign Name

1-open excel and select both the columns including header (Name and Location)

2-right click go to "Define names"

3-assign Name "mylist"

create dsn say "mydsn"

use following code

it will work

protected void Page_load(object sender,EventArgs e)
{
System.Data.Odbc.OdbcConnection conn=new OdbcConnection("DSN=mydsn");
OdbcDataAdapter ad=new OdbcDataAdapter("select * from mylist",conn);
DataSet ds=new DataSet();
ad.Fill(ds);
GridView1.DataSource=ds;
GridView1.DataBind();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM