[英]Data not fills correctly in DataTable using OleDbDataAdapter
When I try to import excel sheet, it does not fetches some integer values. 当我尝试导入Excel工作表时,它不会获取某些整数值。 I am using DevExpress GridControl for exporting data from Grid . 我正在使用DevExpress GridControl从Grid导出数据 。 After export I change value of some cell's(which have blank value) to integer let's say 123 then on import it does not fetches that integer value in DataTable. 导出后,我将某些单元格(具有空白值)的值更改为整数,假设是123,然后在导入时,它不会在DataTable中获取该整数值。
I have posted same issue on DevExpress support center " Export values in Improper Cell ". 我在DevExpress支持中心“ 在不正确的单元格中导出值 ”上发布了相同的问题。 They said the issue is from MSDN not by their control's. 他们说,问题出在MSDN上,不受他们的控制。 Please download the sample from given DevExpress link & also watch the video attached for more detailed information. 请从给定的DevExpress链接下载示例,并观看随附的视频以获取更多详细信息。
I have used following code for import. 我已使用以下代码进行导入。
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);
}
}
}
So, Can any one please help me to solve this issue? 那么,有人可以帮我解决这个问题吗?
Adding the IMEX = 1 property to my connection string, solved my issue. 将IMEX = 1属性添加到我的连接字符串中,解决了我的问题。
string connectionStringV4 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source={0};Extended Properties=Excel 8.0;IMEX=1;";
There is also similar behavior reported in the below thread 下面的线程中报告了类似的行为
Not able to read integer in excel file consistantly 无法持续读取Excel文件中的整数
For more detailed answer please go to following link : 有关更详细的答案,请转到以下链接:
Data not fills correctly in DataTable using OleDbDataAdapter 使用OleDbDataAdapter数据无法正确填充到DataTable中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.