diff --git a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/ErrorMessage.Designer.cs b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/ErrorMessage.Designer.cs index 36d1530..b522a41 100644 --- a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/ErrorMessage.Designer.cs +++ b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/ErrorMessage.Designer.cs @@ -87,6 +87,15 @@ public static string CannotParseNumber { } } + /// + /// Looks up a localized string similar to No se pudo obtener el texto de la celda. + /// + public static string CannotParseString { + get { + return ResourceManager.GetString("CannotParseString", resourceCulture); + } + } + /// /// Looks up a localized string similar to Se han presentado {0} errores al validar los datos antes de exportarlos. Para más informacion, revise la propiedad "Data". /// diff --git a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/ErrorMessage.resx b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/ErrorMessage.resx index 8e7f2d2..739ce5d 100644 --- a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/ErrorMessage.resx +++ b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/ErrorMessage.resx @@ -126,6 +126,9 @@ El valor "{0}" no puede ser convertido a un número válido + + No se pudo obtener el texto de la celda + Se han presentado {0} errores al validar los datos antes de exportarlos. Para más informacion, revise la propiedad "Data" diff --git a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/Properties/AssemblyInfo.cs b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/Properties/AssemblyInfo.cs index 368554e..c9b6f82 100644 --- a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/Properties/AssemblyInfo.cs +++ b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.Resources/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // Puede especificar todos los valores o establecer como predeterminados los números de compilación y de revisión // mediante el carácter '*', como se muestra a continuación: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.3.0")] -[assembly: AssemblyFileVersion("1.3.3.0")] +[assembly: AssemblyVersion("1.3.4.0")] +[assembly: AssemblyFileVersion("1.3.4.0")] diff --git a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Engines/Excel/ExcelImportEngine.cs b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Engines/Excel/ExcelImportEngine.cs index 737b066..d5420c2 100644 --- a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Engines/Excel/ExcelImportEngine.cs +++ b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Engines/Excel/ExcelImportEngine.cs @@ -9,6 +9,7 @@ using Infodinamica.Framework.Core.Extensions.Reflection; using Infodinamica.Framework.Exportable.Resources; using Infodinamica.Framework.Exportable.Tools; +using NPOI.OpenXmlFormats.Shared; using NPOI.SS.UserModel; namespace Infodinamica.Framework.Exportable.Engines.Excel @@ -62,7 +63,7 @@ public void SetDocument(string path) } _file.Position = 0; } - + public IList GetList(string key) where T : class { if (!_wasReaded) @@ -126,19 +127,9 @@ public IList GetList(string key) where T : class if (instanceProperty != null && instanceProperty.IsNumeric()) { double numberValue; - try - { - numberValue = cell.NumericCellValue; - } - catch - { - //Remove blanck spaces an try to cast again - if (!double.TryParse(cell.StringCellValue.Trim(), out numberValue)) - //If cannot parse, try to get default value configured at attribute column - if (!double.TryParse(instanceMetadata.DefaultForNullOrInvalidValues, out numberValue)) - //If there isn't a default value, throw an exception. Nothing we can do - throw new Exception(string.Format(ErrorMessage.CannotParseNumber, cell.StringCellValue)); - } + if (!TryGetNumber(cell, out numberValue)) + if (!double.TryParse(instanceMetadata.DefaultForNullOrInvalidValues, out numberValue)) + throw new Exception(string.Format(ErrorMessage.CannotParseNumber, GetText(cell))); instanceProperty.SetValue(t, Convert.ChangeType(numberValue, instanceProperty.PropertyType), null); } @@ -146,16 +137,9 @@ public IList GetList(string key) where T : class else if (instanceProperty != null && instanceProperty.IsDateOrTime()) { DateTime dateValue; - try - { - dateValue = cell.DateCellValue; - } - catch - { - if(!DateTime.TryParse(cell.StringCellValue.Trim(), out dateValue)) - if(!DateTime.TryParse(instanceMetadata.DefaultForNullOrInvalidValues, out dateValue)) - throw new Exception(string.Format(ErrorMessage.CannotParseDatetime, cell.StringCellValue)); - } + if (!TryGetDate(cell, out dateValue)) + if (!DateTime.TryParse(instanceMetadata.DefaultForNullOrInvalidValues, out dateValue)) + throw new Exception(string.Format(ErrorMessage.CannotParseDatetime, GetText(cell))); instanceProperty.SetValue(t, Convert.ChangeType(dateValue, instanceProperty.PropertyType), null); } @@ -163,23 +147,22 @@ public IList GetList(string key) where T : class else if (instanceProperty != null && instanceProperty.IsBoolean()) { bool boolValue; - try - { - boolValue = cell.BooleanCellValue; - } - catch - { - if (!bool.TryParse(cell.StringCellValue.Trim(), out boolValue)) - if (!bool.TryParse(instanceMetadata.DefaultForNullOrInvalidValues, out boolValue)) - throw new Exception(string.Format(ErrorMessage.CannotParseBoolean, cell.StringCellValue)); - } + if(!TryGetBool(cell, out boolValue)) + if (!bool.TryParse(instanceMetadata.DefaultForNullOrInvalidValues, out boolValue)) + throw new Exception(string.Format(ErrorMessage.CannotParseBoolean, GetText(cell))); instanceProperty.SetValue(t, Convert.ChangeType(boolValue, instanceProperty.PropertyType), null); } //Else is string else { - instanceProperty.SetValue(t, Convert.ChangeType(cell.StringCellValue, instanceProperty.PropertyType), null); + string value = string.Empty; + if (TryGetText(cell, out value)) + instanceProperty.SetValue(t, Convert.ChangeType(value, instanceProperty.PropertyType), null); + else if (instanceMetadata.DefaultForNullOrInvalidValues != null) + instanceProperty.SetValue(t, Convert.ChangeType(instanceMetadata.DefaultForNullOrInvalidValues, instanceProperty.PropertyType), null); + else + throw new Exception(ErrorMessage.CannotParseString); } colIndex++; @@ -201,5 +184,189 @@ public IExcelImportEngine AsExcel() { return this; } + + private bool TryGetNumber(ICell cell, out double returnValue) + { + returnValue = -1; + + try + { + returnValue = cell.NumericCellValue; + return true; + } catch { } + + try + { + switch (cell.CellType) + { + case CellType.Blank: + return false; + case CellType.Boolean: + returnValue = cell.BooleanCellValue ? 1 : 0; + return true; + case CellType.Error: + return false; + case CellType.Formula: + return false; + case CellType.Numeric: + returnValue = cell.NumericCellValue; + return true; + case CellType.String: + return double.TryParse(cell.StringCellValue, out returnValue); + case CellType.Unknown: + return double.TryParse(cell.StringCellValue, out returnValue); + default: + return false; + } + } + catch + { + return false; + } + } + + private bool TryGetText(ICell cell, out string returnValue) + { + returnValue = string.Empty; + + try + { + returnValue = cell.StringCellValue; + return true; + } catch { } + + try + { + switch (cell.CellType) + { + case CellType.Blank: + returnValue = string.Empty; + return true; + case CellType.Boolean: + returnValue = cell.BooleanCellValue.ToString(); + return true; + case CellType.Error: + return false; + case CellType.Formula: + return false; + case CellType.Numeric: + returnValue = cell.NumericCellValue.ToString(); + return true; + case CellType.String: + returnValue = cell.StringCellValue; + return true; + case CellType.Unknown: + returnValue = cell.StringCellValue; + return true; + default: + return false; + } + } + catch + { + return false; + } + } + + private bool TryGetBool(ICell cell, out bool returnValue) + { + returnValue = false; + + try + { + returnValue = cell.BooleanCellValue; + return true; + } catch { } + + + try + { + switch (cell.CellType) + { + case CellType.Blank: + returnValue = false; + return true; + case CellType.Boolean: + returnValue = cell.BooleanCellValue; + return true; + case CellType.Error: + return false; + case CellType.Formula: + return false; + case CellType.Numeric: + if (cell.NumericCellValue == 0) + { + returnValue = false; + return true; + } else if (cell.NumericCellValue == 1) + { + returnValue = true; + return true; + } + else + return false; + case CellType.String: + return bool.TryParse(cell.StringCellValue, out returnValue); + case CellType.Unknown: + return bool.TryParse(cell.StringCellValue, out returnValue); + default: + return false; + } + } + catch + { + return false; + } + } + + private bool TryGetDate(ICell cell, out DateTime returnValue) + { + returnValue = new DateTime(); + + //First try to get the value from date cell + try + { + returnValue = cell.DateCellValue; + return true; + } + catch { } + + + try + { + switch (cell.CellType) + { + case CellType.Blank: + return false; + case CellType.Boolean: + return false; + case CellType.Error: + return false; + case CellType.Formula: + return false; + case CellType.Numeric: + return DateTime.TryParse(cell.NumericCellValue.ToString(), out returnValue); + case CellType.String: + return DateTime.TryParse(cell.StringCellValue.ToString(), out returnValue); + case CellType.Unknown: + return DateTime.TryParse(cell.StringCellValue.ToString(), out returnValue); + default: + return false; + } + } + catch + { + return false; + } + } + + private string GetText(ICell cell) + { + var value = string.Empty; + if (TryGetText(cell, out value)) + return value; + else + return String.Empty; + } } } diff --git a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.nuspec b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.nuspec index 150aef7..65984c0 100644 --- a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.nuspec +++ b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable.nuspec @@ -11,7 +11,7 @@ https://es.gravatar.com/userimage/101034715/cc029f93b4ab6e5c7e263d8db6b35b3a.png false $description$ - Se corrige un error que impedía utilizar el valor por defecto cuando la celda estaba vacía o tenía un valor inválido + Se corrige error que no casteaba correctamente a los tipos de dato al importar celdas con numeros Copyright © 2016 - 2017 Infodinamica Limitada EXCEL EXCEL-2003 EXCEL-2007 EXPORT-EXCEL IMPORT-EXCEL NPOI NPOI-WRAPPER diff --git a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Properties/AssemblyInfo.cs b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Properties/AssemblyInfo.cs index a12fbae..132519b 100644 --- a/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Properties/AssemblyInfo.cs +++ b/src/Infodinamica.Framework.Exportable/Infodinamica.Framework.Exportable/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // Puede especificar todos los valores o establecer como predeterminados los números de compilación y de revisión // mediante el carácter '*', como se muestra a continuación: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.3.*")] -[assembly: AssemblyFileVersion("1.3.3.0")] +[assembly: AssemblyVersion("1.3.4.*")] +[assembly: AssemblyFileVersion("1.3.4.0")] diff --git a/src/Infodinamica.Framework.Exportable/NugetReleases/Infodinamica.Framework.Exportable.1.3.4.23437.nupkg b/src/Infodinamica.Framework.Exportable/NugetReleases/Infodinamica.Framework.Exportable.1.3.4.23437.nupkg new file mode 100644 index 0000000..e54d7c1 Binary files /dev/null and b/src/Infodinamica.Framework.Exportable/NugetReleases/Infodinamica.Framework.Exportable.1.3.4.23437.nupkg differ