使用OleDbDataAdapter

时间:2015-05-29 08:37:38

标签: c# excel import datatable oledbdataadapter

当我尝试导入Excel工作表时,它不会获取某些整数值。我正在使用 DevExpress GridControl从Grid 导出数据。导出后我 将某些单元格(具有空白值)的值更改为整数 让我们说123然后在导入时它不会获取该整数DataTable中的值。

我在DevExpress支持中心发布了同样的问题" Export values in Improper Cell"。他们说问题来自MSDN而不是他们的控制。请从给定的DevExpress链接下载样本。还可以观看附带的视频以获取更多详细信息。

我使用以下代码进行导入。

 private System.Data.DataTable GetDataTableFromFile(string fileName)
    {
        string query = string.Empty;
        //// string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + fileName + "';Extended Properties=Excel 8.0;";
        string connectionStringV12 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;";
        string connectionStringV4 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source={0};Extended Properties=Excel 8.0;";
        System.Data.DataTable dataTable = new System.Data.DataTable();
        OleDbConnection obedbConnection = new OleDbConnection(string.Format(connectionStringV4, fileName));
        try
        {
            if (obedbConnection.State != ConnectionState.Open)
            {
                obedbConnection.Open();
            }
        }
        catch (Exception)
        {
            ////Diff. Connetion String
            obedbConnection = new OleDbConnection(string.Format(connectionStringV12, fileName));
        }

        ////Get First SheetName From Xls File 
        string sheetName = GetSheetNameFromFile(obedbConnection);

        if (sheetName != null)
        {
            ////Query for Reading all Data from File
            query = "select * from [" + sheetName + "]";
        }

        if (!string.IsNullOrEmpty(query))
        {
            OleDbDataAdapter data = new OleDbDataAdapter(query, obedbConnection);
            data.Fill(dataTable);
        }
        return dataTable;
    }

    /// <summary>
    /// Gets First SheetName of excel File 
    /// </summary>
    /// <param name="con">Connection object.</param>
    /// <returns>return sheet name.</returns>
    private string GetSheetNameFromFile(OleDbConnection con)
    {
        try
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            var oledbTableSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            if (oledbTableSchema.Rows.Count > 0)
            {
                string sheetName = oledbTableSchema.Rows[0].ItemArray[2].ToString();
                if (string.IsNullOrEmpty(sheetName))
                {
                    throw new Exception("Sheet Not Found");

                }
                return sheetName;
            }
            return string.Empty;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}

那么,任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

IMEX = 1 属性添加到我的连接字符串中,解决了我的问题。

    string connectionStringV4 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source={0};Extended Properties=Excel 8.0;IMEX=1;";

以下帖子中也报告了类似的行为

Not able to read integer in excel file consistantly

如需更详细的解答,请访问以下链接:

Data not fills correctly in DataTable using OleDbDataAdapter