且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据从excel文件导入gridview。

更新时间:2022-10-17 23:15:25

您需要以正确的方式定义连接字符串和查询。

请按照以下链接:

http://www.connectionstrings.com/excel-2007 [ ^ ]

如何使用OLEDB从Excel文件中读取 [ ^ ]

使用OLEDB读取Excel 2003和2007文件 [ ^ ]

The Microsoft Office Access database engine could not find the object 'StudentDetails.xlsx'. Make sure the object exists and that you spell its name and the path name correctly.

string query = "SELECT [UserName],[Education],[Location] FROM [StudentDetails.xlsx]";
        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();


But in da.fill it gives exception could not find the object 'StudentDetails.xlsx'.

You need to define connection string and query in proper way.
Please, follow below links:
http://www.connectionstrings.com/excel-2007[^]
How to read from an Excel file using OLEDB [^]
Reading Excel 2003 and 2007 Files Using OLEDB[^]