使用 Oledb 读取 Excel 文件 - 将 Excel 文件的内容仅视为文本
我正在使用 C# 和 OleDb 从 Excel 2007 文件读取数据。
我使用的连接字符串是:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
以下是读取 excel 的代码:
private OleDbConnection con = null;
private OleDbCommand cmd = null;
private OleDbDataReader dr = null;
private OleDbDataAdapter adap = null;
private DataTable dt = null;
private DataSet ds = null;
private string query;
private string conStr;
public MainWindow()
{
this.InitializeComponent();
this.query = "SELECT * FROM [Sheet1$]";
this.conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\301591\\Desktop\\Fame.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
}
private void btnImport_Click(object sender, RoutedEventArgs e)
{
this.ImportingDataSetWay();
}
private void ImportingDataSetWay()
{
con = new OleDbConnection(conStr);
cmd = new OleDbCommand(query, con);
adap = new OleDbDataAdapter(cmd);
ds = new DataSet();
adap.Fill(ds);
this.grImport.ItemsSource = ds.Tables[0].DefaultView;
}
这里 grImport 是我的 WPF 数据网格,我正在使用自动生成的列。
如何确保 Excel 中存储的内容始终作为字符串读取。 我不允许修改任何注册表值来实现此目的。有没有更好的方法来读取excel?请指导我。如果您需要任何其他信息,请告诉我。
问候, 普里扬克
I am using C# and OleDb to read data from an excel 2007 file.
Connection string I am using is:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
Following is the code to read excel:
private OleDbConnection con = null;
private OleDbCommand cmd = null;
private OleDbDataReader dr = null;
private OleDbDataAdapter adap = null;
private DataTable dt = null;
private DataSet ds = null;
private string query;
private string conStr;
public MainWindow()
{
this.InitializeComponent();
this.query = "SELECT * FROM [Sheet1$]";
this.conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\301591\\Desktop\\Fame.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
}
private void btnImport_Click(object sender, RoutedEventArgs e)
{
this.ImportingDataSetWay();
}
private void ImportingDataSetWay()
{
con = new OleDbConnection(conStr);
cmd = new OleDbCommand(query, con);
adap = new OleDbDataAdapter(cmd);
ds = new DataSet();
adap.Fill(ds);
this.grImport.ItemsSource = ds.Tables[0].DefaultView;
}
Here grImport is my WPF Data-Grid and I am using auto-generated columns.
How can I make sure the content stored in Excel will always be read as a string.
I am not allowed to modify any of the registry values to achieve this. Is there any better way to read excel. Please guide me. If you need any other information do let me know.
Regards,
Priyank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试如下所示的 oledb 提供程序连接字符串吗?
HDR=NO 表示 oledb 将读取所有行作为数据 [NO HEADER]。因此,由于标题列都是文本,因此它将把所有列中的所有行数据视为文本。将数据填充到 DataSet 后,您必须删除第一行,因为它不是数据。
Could you try oledb provider connection string as follow.
HDR=NO means oledb will read all rows as data [NO HEADER]. So as your header columns are all text, it will treat all row data in all columns as text. After filling data into DataSet, you have to remove first row as it is not data.
我们发现的一个修复是确保第一行包含标题。
即确保您的列名称位于第一行。如果可以的话。
然后在您的代码中,您必须以编程方式忽略第一行,而
同时,如果需要的话,从中删除您的列名称。
在您的连接字符串中使用它。
我不确定这一点
One fix we found, is to ensure that the first row contains a header.
i.e. make sure that your column names are in the first row. If that's possible.
Then in your code, you have to programmatically ignore the first row, while
at the same time scarfing your column names from it, if need be.
Use this in your connection string.
I'm not sure of this
我有类似的问题..我通过拆分连接字符串解决了这个问题,如以下字符串中所述。请注意,在扩展属性之后.. 还有 (char)34 包围 IMEX=1 除了字符串之外。如果没有使用 (char)34 包围,则会给出错误“无法找到 ISAM”。希望这也能解决您对 ACE 提供商的问题
I had similar issue.. i resolved it by splitting the connectionstring as mentioned in following string. Please note that after extended properties.. there is (char)34 to surround IMEX=1 addition to the string. without surrounding with (char)34, it will give error "cant find ISAM". Hope this resolves your issue for ACE provider also