Skip to content

Commit

Permalink
Added support for dates from csv files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecole committed May 19, 2017
1 parent 513e81a commit 539172d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/EPPlus.Extensions.Tests/EPPlus.Extensions.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Marvel.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Marvel.xlsx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
31 changes: 30 additions & 1 deletion src/EPPlus.Extensions.Tests/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,43 @@ public void ToDataSet_ShouldAddRows_ForEachSourceRow()
result.Tables[0].Rows[49][0].ToString().ShouldBe("Wyoming");
}

public void ToDataSet_WhenCsv_AllowsDateFormat()
{
var package = GetMarvelCsvPackage();

var result = package.ToDataSet(true);

var cellValue = result.Tables[0].Rows[0][3];

DateTime temp;
DateTime.TryParse(cellValue.ToString(), out temp).ShouldBeTrue($"Input was {cellValue}");

Convert.ToDateTime(cellValue).ToString("o").ShouldBe(DateTime.Parse("04/22/1950 08:41 PM").ToString("o"));
}

private static ExcelPackage GetMarvelPackage()
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Marvel.xlsx");
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Marvel.xlsx");
var file = new FileInfo(path);
var package = new ExcelPackage(file);
return package;
}

private static ExcelPackage GetMarvelCsvPackage()
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Marvel.csv");
var file = new FileInfo(path);
var package = new ExcelPackage();

var textFormat = new ExcelTextFormat();
textFormat.TextQualifier = '"';

var sheet = package.Workbook.Worksheets.Add("Marvel");
sheet.Cells.LoadFromText(File.ReadAllText(file.FullName), textFormat);

return package;
}

private static ExcelPackage GetStatesPackage()
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"States.xlsx");
Expand Down
11 changes: 11 additions & 0 deletions src/EPPlus.Extensions.Tests/Marvel.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
First Name,Last Name,Alter Ego,Birth Date
Tony,Stark,Iron Man,04/22/1950 08:41 PM
Peter,Parker,Spider-man,04/22/2017 08:41 PM
Bruce,Banner,Hulk,04/22/2017 08:41 PM
Clint,Barton,Hawkeye,04/22/2017 08:41 PM
James,Rhodes,War Machine,04/22/2017 08:41 PM
Steve,Rogers,Captain America,04/22/2017 08:41 PM
James,Barnes,The Winter Solider,04/22/2017 08:41 PM
Sam,Wilson,Falcon,04/22/2017 08:41 PM
Wanda,Maximoff,Scarlet Witch,04/22/2017 08:41 PM
Natasha,Romanoff,Black WIdow,04/22/2017 08:41 PM
4 changes: 2 additions & 2 deletions src/EPPlus.Extensions/EPPlusExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static DataSet ToDataSet(this ExcelPackage package, int headerRow = 0)
sheetStartRow = headerRow;
}
var columns = from firstRowCell in sheet.Cells[sheetStartRow, 1, sheetStartRow, sheet.Dimension.End.Column]
select new DataColumn(headerRow > 0 ? firstRowCell.Text : $"Column {firstRowCell.Start.Column}");
select new DataColumn(headerRow > 0 ? firstRowCell.Value.ToString() : $"Column {firstRowCell.Start.Column}");

table.Columns.AddRange(columns.ToArray());

Expand All @@ -58,7 +58,7 @@ public static DataSet ToDataSet(this ExcelPackage package, int headerRow = 0)
var row = table.Rows.Add();
foreach (var cell in inputRow)
{
row[cell.Start.Column - 1] = cell.Text;
row[cell.Start.Column - 1] = cell.Value;
}
}

Expand Down

0 comments on commit 539172d

Please sign in to comment.