簡體   English   中英

使用OleDbDataAdapter數據無法正確填充到DataTable中

[英]Data not fills correctly in DataTable using OleDbDataAdapter

當我嘗試導入Excel工作表時,它不會獲取某些整數值。 我正在使用DevExpress GridControl從Grid導出數據 導出后,我將某些單元格(具有空白值)的值更改為整數,假設是123,然后在導入時,它不會在DataTable中獲取該整數值。

我在DevExpress支持中心“ 在不正確的單元格中導出值 ”上發布了相同的問題。 他們說,問題出在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);
        }
    }
}

那么,有人可以幫我解決這個問題嗎?

IMEX = 1屬性添加到我的連接字符串中,解決了我的問題。

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

下面的線程中報告了類似的行為

無法持續讀取Excel文件中的整數

有關更詳細的答案,請轉到以下鏈接:

使用OleDbDataAdapter數據無法正確填充到DataTable中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM