简体   繁体   中英

Read excel Data as string from Excel using ODBC

i am trying to read excel data to C# using ODBC here is my code

 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 . Any suggestion . i Already tried CStr but no help .

For a previous discussion of similar problem here, please check following:

DBNull in non-empty cell when reading Excel file through OleDB

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..

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. However, because Excel allows mixed datatypes (and databases do not), it occasionally gets it wrong.

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.

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
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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