【问题标题】:Data not fills correctly in DataTable using OleDbDataAdapter数据未使用 OleDbDataAdapter 正确填写在 DataTable 中
【发布时间】:2015-08-12 01:08:24
【问题描述】:

当我尝试导入 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);
        }
    }
}

那么,谁能帮我解决这个问题?

【问题讨论】:

  • 您在导入或导出文件时遇到问题
  • 导入问题。
  • 我建议您使用 ExcelReader dll 导入 excel 文件。 exceldatareader.codeplex.com
  • 是的,这是另一种解决方法......但首先我想问你为什么会这样??
  • 这可能是因为您尝试打开的 xls 包含不相关格式的列。例如,包含数字的列可能被格式化为日期时间格式或一般格式

标签: c# excel import datatable oledbdataadapter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2020-07-10
    • 1970-01-01
    • 2019-07-11
    相关资源
    最近更新 更多