0

案例1:在 .Net Framework Client Profile 中使用 Oledb 读取 excel 时,Oledb 会读取 excel 中的数据,而忽略 excel 中给出的格式

示例:1.98782637 是实际值,但格式化后显示为 1.99。当我从我的代码中读取时,读取的值为 1.98782637。

案例2:使用.Net Framework中的Oledb读取excel时,Oledb读取格式化后excel中可用的数据

示例:1.98782637 是实际值,但格式化后显示为 1.99。当我从我的代码中读取时,读取的值为 1.99。

这是我用于这两种情况的代码。

        DataSet dsoutlier = null;
        OleDbDataAdapter oledbAdapterOutlier = null;
        OleDbConnection oledbConnOutlier = new OleDbConnection();

        string fileName = "C:\\Sample.xlsx";           
       oledbConnOutlier.ConnectionString= "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=0;\'"; 

        if (oledbConnOutlier.State == ConnectionState.Closed)
            oledbConnOutlier.Open();
        oledbAdapterOutlier = new OleDbDataAdapter("select F1,F9 from [Sheet1$] where F1 is not null", oledbConnOutlier);
        dsoutlier = new DataSet();

        try
        {
            oledbAdapterOutlier.Fill(dsoutlier);
            int counter = 0;
            foreach (var item in dsoutlier.Tables[0].Rows)
            {
                Console.Write(dsoutlier.Tables[0].Rows[counter][0]+"  ");
                Console.WriteLine(dsoutlier.Tables[0].Rows[counter++][1]);


            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            oledbConnOutlier.Close();
        }

有没有办法在这两种情况下都包含和忽略格式?

4

0 回答 0