简体   繁体   English

使用ODBC从Excel中将Excel数据读取为字符串

[英]Read excel Data as string from Excel using ODBC

i am trying to read excel data to C# using ODBC here is my code 我正在尝试使用ODBC将Excel数据读取到C#中,这是我的代码

 string    lstrFileName = "Sheet1";
            //string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="+path+ ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
            string strConnString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq=E:\\T1.xlsx;Extensions=xls/xlsx;Persist Security Info=False";
            DataTable ds;
            using (OdbcConnection oConn = new OdbcConnection(strConnString))
            {
                using (OdbcCommand oCmd = new OdbcCommand())
                {
                    oCmd.Connection = oConn;

                    oCmd.CommandType = System.Data.CommandType.Text;
                    oCmd.CommandText = "select A   from [" + lstrFileName + "$]";

                    OdbcDataAdapter oAdap = new OdbcDataAdapter();
                    oAdap.SelectCommand = oCmd;

                    ds = new DataTable();
                    oAdap.Fill(ds);
                    oAdap.Dispose();



                    // ds.Dispose();
                }

            }

my sample data A 1 2 3 AA BB its data table its read 1,2,3 and two blank row i can understand because of first row its deciding data type , but how can i convert as String and read all row . 我的样本数据A 1 2 3 AA BB其数据表的读取1,2,3和两个空白行,我可以理解,因为第一行决定了数据类型,但是我如何转换为字符串并读取所有行。 Any suggestion . 任何建议。 i Already tried CStr but no help . 我已经尝试过CStr,但没有帮助。

For a previous discussion of similar problem here, please check following: 对于此处类似问题的先前讨论,请检查以下内容:

DBNull in non-empty cell when reading Excel file through OleDB 通过OleDB读取Excel文件时非空单元格中的DBNull

As a workaround, you may also format the column as "text"(ie in Excel, select column, right click "Format Cells..."), though this might be impractical if you will process large number of files or if you must not touch the file.. 作为一种解决方法,您也可以将列设置为“文本”格式(即在Excel中,选择列,右键单击“设置单元格格式...”),尽管如果要处理大量文件或必须不要碰文件。

This is partially speculation, but when reading an Excel document as a database, the adapter has to make a judgement on datatypes and usually does a pretty good job. 这部分是推测,但是当将Excel文档作为数据库读取时,适配器必须对数据类型做出判断,并且通常做得很好。 However, because Excel allows mixed datatypes (and databases do not), it occasionally gets it wrong. 但是,由于Excel允许使用混合数据类型(而数据库不允许),因此有时会出错。

My recommendation would to be to not use a data adapter, and just read in every field as an object type. 我的建议是不要使用数据适配器,而应将每个字段作为对象类型读取。 From there, you can easily cast them to strings (StringBuilder, ToString(), etc) or even TryParse into fields you suspect they should be, ignoring the ODBC datatype. 从那里,您可以轻松地将它们转换为字符串(StringBuilder,ToString()等),甚至可以将TryParse转换为您怀疑它们应为字段的字段,而无需考虑ODBC数据类型。

Something like this would be a boilerplate for that: 这样的事情将是一个样板:

using (OdbcCommand oCmd = new OdbcCommand())
{
    oCmd.Connection = oConn;

    oCmd.CommandType = System.Data.CommandType.Text;
    oCmd.CommandText = "select A   from [" + lstrFileName + "$]";

    using (OdbcDataReader reader = oCmd.ExecuteReader())
    {
        object[] fields = new object[reader.FieldCount];

        while (reader.Read())
        {
            reader.GetValues(fields);

            // do something with fields
        }
    }

}

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

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