在阅读xl表时显示错误 [英] While reading the xl sheet showing errors

查看:114
本文介绍了在阅读xl表时显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从xl工作表中读取数据我为此编写代码但在执行时显示错误

错误如下



 {Microsoft Access数据库引擎无法打开或写入文件'D:\\Gopi.Ch \\DotNetTasks \\XL Sheet \\XL Sheet \\ \\\Files'。它已经由其他用户专门打开,或者你需要获得查看和写入数据的权限。} 





我的代码关注



  string  connString = ConfigurationManager.ConnectionStrings [< span class =code-string>  xls]。ConnectionString; 
// 创建连接对象
OleDbConnection oledbConn = new OleDbConnection(connString);
尝试
{
string path = @ D:\ Gopi.Ch\DotNetTasks\XL Sheet \XL Sheet \File\gopi.xlsx;
OleDbConnection con_excel = new OleDbConnection( Provider = Microsoft.ACE.OLEDB.12.0;数据源= +路径+ ;扩展属性= Excel 8.0\" );
OleDbDataAdapter adpt = new OleDbDataAdapter( SELECT国家,州,城市,Pincode来自[Sheet1 $],con_excel);
DataSet ds = new DataSet();
adpt.Fill(ds, tbl_temp);
DataTable dt = new DataTable();
dt = ds.Tables [ FooTable];
/ * List< SetData> setData = new List< SetData>();
foreach(DataRow dr in dt.Rows)
{

} * /


gdXlData.DataSource = ds.Tables [< span class =code-string>
tbl_temp];
gdXlData.DataBind();
}
catch
{
}
finally
{
// 关闭连接
oledbConn。关();
}



我做了什么错误并且已经安装了microsoft oledb 12.0



谢谢和问候

解决方案

,con_excel);
DataSet ds = new DataSet() ;
adpt.Fill(ds, tbl_temp);
DataTable dt = new DataTable();
dt = ds.Tables [ FooTable];
/ * List< SetData> setData = new List< SetData>();
foreach(dt.Rows中的DataRow dr)
{

} * /


gdXlData。 DataSource = ds.Tables [ tbl_temp];
gdXlData.DataBind();
}
catch
{
}
finally
{
// 关闭连接
oledbConn。关();
}



我做了什么错误并且已经安装了microsoft oledb 12.0



谢谢和问候


2个可能的原因:

1)检查文件的路径!你确定你曾经两次使用 XL Sheet 吗?

2)连接字符串错误(请参阅我对该问题的评论)

Excel连接字符串 - ConnectionStrings.com [ ^ ]

正确的连接字符串应如下所示:

 Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\ ProperPathToExcelFile.xlsx; Extended Properties = Excel 12.0 ; HDR = YES; 


I want to read the data from a xl sheet i wrote the code for that but showing error while executing
the error is following

{"The Microsoft Access database engine cannot open or write to the file 'D:\\Gopi.Ch\\DotNetTasks\\XL Sheet\\XL Sheet\\Files'. It is already opened exclusively by another user, or you need permission to view and write its data."}



my code following

string connString = ConfigurationManager.ConnectionStrings["xls"].ConnectionString;
            // Create the connection object
            OleDbConnection oledbConn = new OleDbConnection(connString);
            try
            {
                string path = @"D:\Gopi.Ch\DotNetTasks\XL Sheet\XL Sheet\File\gopi.xlsx";
                OleDbConnection con_excel = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 8.0");
                OleDbDataAdapter adpt = new OleDbDataAdapter("SELECT Country,State,City,Pincode FROM [Sheet1$]", con_excel);
                DataSet ds = new DataSet();
                adpt.Fill(ds, "tbl_temp");
                DataTable dt = new DataTable();
                dt = ds.Tables["FooTable"];
               /* List<SetData> setData = new List<SetData>();
                foreach (DataRow dr in dt.Rows)
                {

                } */

                gdXlData.DataSource = ds.Tables["tbl_temp"];
                gdXlData.DataBind();
            }
            catch
            {
            }
            finally
            {
                // Close connection
                oledbConn.Close();
            }


What Mistake i did and already installed microsoft oledb 12.0

thanks and regards

解决方案

", con_excel); DataSet ds = new DataSet(); adpt.Fill(ds, "tbl_temp"); DataTable dt = new DataTable(); dt = ds.Tables["FooTable"]; /* List<SetData> setData = new List<SetData>(); foreach (DataRow dr in dt.Rows) { } */ gdXlData.DataSource = ds.Tables["tbl_temp"]; gdXlData.DataBind(); } catch { } finally { // Close connection oledbConn.Close(); }


What Mistake i did and already installed microsoft oledb 12.0

thanks and regards


2 possible reasons:
1) Check path to the file! Are you sure you used XL Sheet twice?
2) Connection string is wrong (see my comment to the question)
Excel connection strings - ConnectionStrings.com[^]
Proper connection string should look like:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\ProperPathToExcelFile.xlsx;Extended Properties="Excel 12.0;HDR=YES";


这篇关于在阅读xl表时显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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