简体   繁体   English

使用Oledb读取Excel文件-将Excel文件的内容视为仅文本

[英]Reading Excel-file using Oledb - treating content of excel file as Text only

I am using C# and OleDb to read data from an excel 2007 file. 我正在使用C#和OleDb从Excel 2007文件中读取数据。

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: 以下是读取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. 这里grImport是我的WPF数据网格,并且我使用的是自动生成的列。

How can I make sure the content stored in Excel will always be read as a string. 我如何确保存储在Excel中的内容将始终作为字符串读取。 I am not allowed to modify any of the registry values to achieve this. 我不允许修改任何注册表值来实现此目的。 Is there any better way to read excel. 有没有更好的方法来阅读excel。 Please guide me. 请指导我。 If you need any other information do let me know. 如果您需要其他任何信息,请告诉我。

Regards, Priyank 问候,普里扬克

Could you try oledb provider connection string as follow. 您可以按以下方式尝试oledb提供程序连接字符串吗?

HDR=NO means oledb will read all rows as data [NO HEADER]. HDR = NO表示oledb将读取所有行作为数据[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. 将数据填充到DataSet中之后,您必须删除第一行,因为它不是数据。

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";

One fix we found, is to ensure that the first row contains a header. 我们发现的一个解决方法是确保第一行包含标题。 ie 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. 在您的连接字符串中使用它。

     IMEX=1;HDR=NO;

I'm not sure of this 我不确定

     TypeGuessRows=0;ImportMixedTypes=Text

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. 请注意,在扩展属性之后..字符串的IMEX = 1加上(char)34。 without surrounding with (char)34, it will give error " cant find ISAM" . 没有用(char)34包围,它将给出错误“ 找不到ISAM” Hope this resolves your issue for ACE provider also 希望这也可以为ACE供应商解决您的问题

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                        "Data Source=" + Server.MapPath("UploadedExcel/" + FileName + ".xls") +
                        ";Extended Properties=" +
                        (char)34 + "Excel 8.0;IMEX=1;" + (char)34;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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