Skip to content
This repository has been archived by the owner on Feb 11, 2019. It is now read-only.

Commit

Permalink
Fixed #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstKosyanov committed Jun 8, 2016
1 parent 2ddd1c9 commit 324adb4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
16 changes: 8 additions & 8 deletions UnitTests/xlClassImportTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class xlClassImportTester

TestExcelClass[] data = new TestExcelClass[]
{
new TestExcelClass() { intProperty1 = 1 , SomeDate = DateTime.Now, SomeString = "asdasd"},
new TestExcelClass() { intProperty1 = 2 , SomeDate = DateTime.Now, SomeString = "aafgf"},
new TestExcelClass() { intProperty1 = 3 , SomeDate = DateTime.Now, SomeString = "xdfe"},
new TestExcelClass() { intProperty1 = 4 , SomeDate = DateTime.Now, SomeString = "dfdr"},
new TestExcelClass() { intProperty1 = 5 , SomeDate = DateTime.Now, SomeString = "ghdg"},
new TestExcelClass() { intProperty1 = 7 , SomeDate = DateTime.Now, SomeString = "dfg"},
new TestExcelClass() { intProperty1 = 9 , SomeDate = DateTime.Now, SomeString = "dfgag"},
new TestExcelClass() { intProperty1 = 10, SomeDate = DateTime.Now, SomeString = "sdfsw"},
new TestExcelClass() { intProperty1 = 1 , intProperty3 = 1 , SomeDate = DateTime.Now, SomeString = "asdasd"},
new TestExcelClass() { intProperty1 = 2 , intProperty3 = 2 , SomeDate = DateTime.Now, SomeString = "aafgf"},
new TestExcelClass() { intProperty1 = 3 , intProperty3 = 3 , SomeDate = DateTime.Now, SomeString = "xdfe"},
new TestExcelClass() { intProperty1 = 4 , intProperty3 = 4 , SomeDate = DateTime.Now, SomeString = "dfdr"},
new TestExcelClass() { intProperty1 = 5 , intProperty3 = 5 , SomeDate = DateTime.Now, SomeString = "ghdg"},
new TestExcelClass() { intProperty1 = 7 , intProperty3 = 7 , SomeDate = DateTime.Now, SomeString = "dfg"},
new TestExcelClass() { intProperty1 = 9 , intProperty3 = 9 , SomeDate = DateTime.Now, SomeString = "dfgag"},
new TestExcelClass() { intProperty1 = 10, intProperty3 = 10, SomeDate = DateTime.Now, SomeString = "sdfsw"},
};

[ClassInitialize]
Expand Down
4 changes: 2 additions & 2 deletions XLOC/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// Можно задать все значения или принять номера сборки и редакции по умолчанию
// используя "*", как показано ниже:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.2.5")]
[assembly: AssemblyFileVersion("1.0.2.5")]
[assembly: AssemblyVersion("1.0.2.6")]
[assembly: AssemblyFileVersion("1.0.2.6")]
71 changes: 37 additions & 34 deletions XLOC/Reader/xlArrayReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,56 @@ object getValue(Cell cell, Type type)
case CellValues.Date:
throw new NotImplementedException($"Преобразование для типа {type} не реализовано");
case CellValues.Number:
return Convert.ChangeType(cell.CellValue, type);
return ConvertToTypeWitNullableCheck(cell.CellValue, type);
case CellValues.SharedString:
var RefId = int.Parse(cell.CellValue.Text);
return TypeDescriptor.GetConverter(type).ConvertFromString(docProvider.sharedStrings[RefId].HasValue() ? docProvider.sharedStrings[RefId] : string.Empty);
case CellValues.String:
case CellValues.InlineString:
return Convert.ChangeType(cell.CellValue?.Text, type);
return ConvertToTypeWitNullableCheck(cell.CellValue?.Text, type);
default:
return Convert.ChangeType(ConvertTypelessCell(cell), type);
return ConvertToTypeWitNullableCheck(ConvertTypelessCell(cell), type);
}
}
catch (Exception ex)
{
throw new InvalidOperationException($"Ошибка преобразования ячеек, адрес ссылки [{cell.CellReference}], искходное значение [{cell.CellValue?.Text}], исходный тип [{cell.DataType?.Value}], стиль [{cell.StyleIndex?.Value}]", ex);
}
}

static object ConvertToTypeWitNullableCheck(object value, Type type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) ? ConvertNullable(value, type) : Convert.ChangeType(value, type);
static object ConvertNullable(object value, Type type) => value != null ? Convert.ChangeType(value, type.GetGenericArguments().First()) : null;

int getSkip() => _config.SkipMode == SkipModeEnum.None ? 0 : _config.SkipCount ?? 0;

Map<T> GetMap<T>(WorksheetPart sheet) where T : IxlCompatible, new()
{
switch (_config.SkipMode)
{
case SkipModeEnum.None: return new Map<T>(sheet.GetCaptionCells().ToDictionary(x => x.CellReference.Value, x => getValue(x, typeof(string)).ToString()));
case SkipModeEnum.Manual: return new Map<T>(sheet.GetCaptionCells(_config.SkipCount.Value).ToDictionary(x => x.CellReference.Value, x => getValue(x, typeof(string)).ToString()));
case SkipModeEnum.Auto: return AutoMap<T>(sheet);
default: throw new NotImplementedException("Default switch case not implemented");
}
}

Map<T> AutoMap<T>(WorksheetPart sheet)
{
Map<T> result = null;
var enumerator = sheet.Worksheet.GetFirstChild<SheetData>().Descendants<Row>().GetEnumerator();
_config.SkipCount = 0;
while (!(!enumerator.MoveNext() || (result?.IsValid ?? false)))
{
result = new Map<T>(ToDictionary(enumerator.Current));
_config.SkipCount++;
}
return result;
}

Dictionary<string, string> ToDictionary(Row row)
{
return row.Descendants<Cell>().Where(x => x.CellValue != null).ToDictionary(x => x.CellReference.Value, x => getValue(x, typeof(string)).ToString());
}
//=================================================
#endregion

Expand Down Expand Up @@ -168,37 +202,6 @@ object getValue(Cell cell, Type type)
}
}
}

int getSkip() => _config.SkipMode == SkipModeEnum.None ? 0 : _config.SkipCount ?? 0;

private Map<T> GetMap<T>(WorksheetPart sheet) where T : IxlCompatible, new()
{
switch (_config.SkipMode)
{
case SkipModeEnum.None: return new Map<T>(sheet.GetCaptionCells().ToDictionary(x => x.CellReference.Value, x => getValue(x, typeof(string)).ToString()));
case SkipModeEnum.Manual: return new Map<T>(sheet.GetCaptionCells(_config.SkipCount.Value).ToDictionary(x => x.CellReference.Value, x => getValue(x, typeof(string)).ToString()));
case SkipModeEnum.Auto: return AutoMap<T>(sheet);
default: throw new NotImplementedException("Default switch case not implemented");
}
}

private Map<T> AutoMap<T>(WorksheetPart sheet)
{
Map<T> result = null;
var enumerator = sheet.Worksheet.GetFirstChild<SheetData>().Descendants<Row>().GetEnumerator();
_config.SkipCount = 0;
while (!(!enumerator.MoveNext() || (result?.IsValid ?? false)))
{
result = new Map<T>(ToDictionary(enumerator.Current));
_config.SkipCount++;
}
return result;
}

private Dictionary<string, string> ToDictionary(Row row)
{
return row.Descendants<Cell>().Where(x => x.CellValue != null).ToDictionary(x => x.CellReference.Value, x => getValue(x, typeof(string)).ToString());
}
//=================================================
#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion XLOC/XLOC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\..\..\Documents\NuGet\</OutputPath>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand Down

0 comments on commit 324adb4

Please sign in to comment.