Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
vvenegasv committed Mar 11, 2017
2 parents 422cea6 + 3536ac0 commit 5f37025
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 40 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
<data name="CannotParseNumber" xml:space="preserve">
<value>El valor "{0}" no puede ser convertido a un número válido</value>
</data>
<data name="CannotParseString" xml:space="preserve">
<value>No se pudo obtener el texto de la celda</value>
</data>
<data name="EngineHasBusinessExceptions" xml:space="preserve">
<value>Se han presentado {0} errores al validar los datos antes de exportarlos. Para más informacion, revise la propiedad "Data"</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,7 @@ public void SetDocument(string path)
}
_file.Position = 0;
}

public IList<T> GetList<T>(string key) where T : class
{
if (!_wasReaded)
Expand Down Expand Up @@ -126,60 +127,42 @@ public IList<T> GetList<T>(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);
}

//Set date value
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);
}

//Set boolean value
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++;
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<iconUrl>https://es.gravatar.com/userimage/101034715/cc029f93b4ab6e5c7e263d8db6b35b3a.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>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</releaseNotes>
<releaseNotes>Se corrige error que no casteaba correctamente a los tipos de dato al importar celdas con numeros</releaseNotes>
<copyright>Copyright © 2016 - 2017 Infodinamica Limitada</copyright>
<tags>EXCEL EXCEL-2003 EXCEL-2007 EXPORT-EXCEL IMPORT-EXCEL NPOI NPOI-WRAPPER</tags>
</metadata>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Binary file not shown.

0 comments on commit 5f37025

Please sign in to comment.