如何将数据从excel文件导入gridview。 [英] How to import data from excel file into gridview.

查看:243
本文介绍了如何将数据从excel文件导入gridview。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft Office Access数据库引擎找不到对象'StudentDetails.xlsx'。确保对象存在,并正确拼写其名称和路径名。

  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();



但在da.fill中,它给异常找不到对象'StudentDetails.xlsx'。

解决方案

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

请按照以下链接:

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[^]


这篇关于如何将数据从excel文件导入gridview。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆