I'm using the following code for get a DataTable variable with the information of a Sheet from a Excel file:
// Just a few examples about connectionString and Excel's file path:
string pathFile = @"C:\Windows\MyFolder\myExcelSample.xlsx";
string excelConnString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathFile + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';";
using (OleDbConnection objConn = new OleDbConnection(cadenaConexion))
{
objConn.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = string.Empty;
if (dt != null)
{
var tempDataTable = (from dataRow in dt.AsEnumerable()
where !dataRow["TABLE_NAME"].ToString().Contains("FilterDatabase")
select dataRow).CopyToDataTable();
dt = tempDataTable;
sheetName = dt.Rows[TABLE_ROW]["TABLE_NAME"].ToString();
}
cmd.Connection = objConn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds, "Fact_TEMP");
tbl_temporal = ds.Tables["Fact_TEMP"];
objConn.Close();
}
The Excel file has a column called "Document No#" which this code says is float type, but, this column has values that are not float.
Here are a few examples:
444036
CO27_009734
CO31_050656
444041
444041
CO24_102377
CO64_000021
444043
CO24_102378
444044
444044
CO24_102380
CO24_102381
444046
444046444049
444050
CO24_102384
And the values that are not float-type are removed in the tbl_temporal
variable.
Which other ways are for solve this situation that doesn't envolve user update the type of the column (which by default is General
) in the Excel file?
A few information I have to share:
- Excel file has a ".xlsx" extension and has 40340 rows.
- Excel file cannot be modified. This Excel file is supplied by a user which just upload his/her Excel file to our System and our System has to solve the issues and it shouldn't modify the schema or its data.