Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions PlayGround/PlayGround.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>10.0</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>10.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Shane32.ExcelLinq.Tests\Shane32.ExcelLinq.Tests.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions PlayGround/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// See https://aka.ms/new-console-template for more information
using Shane32.ExcelLinq.Tests.Models;

Console.WriteLine("Hello, World!");



using var stream1 = new System.IO.FileStream("test.xlsx", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
var tt = new TestFileContext(stream1);
var pp = tt.GetSheet<Class1>();
var xl = new TestFileContext();
using var stream = new System.IO.FileStream("test.csv", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
var t = xl.ReadCsv<Class1>(stream);


var g = t;

4 changes: 4 additions & 0 deletions Shane32.ExcelLinq.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Global
{83E2AC89-22B4-438D-92CF-FDA280B63CFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83E2AC89-22B4-438D-92CF-FDA280B63CFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83E2AC89-22B4-438D-92CF-FDA280B63CFD}.Release|Any CPU.Build.0 = Release|Any CPU
{1F2D99BC-84B4-4704-B3AB-C9EB6A2091B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F2D99BC-84B4-4704-B3AB-C9EB6A2091B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F2D99BC-84B4-4704-B3AB-C9EB6A2091B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F2D99BC-84B4-4704-B3AB-C9EB6A2091B7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
15 changes: 14 additions & 1 deletion src/Shane32.ExcelLinq.Tests/General/EndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public void NullConstructorsThrow()
Assert.ThrowsException<ArgumentNullException>(() => new TestFileContext((ExcelPackage)null));
}


[TestMethod]
public void ReadSampleCsvFile()
{
var xl = new TestFileContext();
using var stream1 = new System.IO.FileStream("test1.csv", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
using var stream2 = new System.IO.FileStream("test2.csv", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
xl.ReadCsv<Class1>(stream1, "Sheet1");
xl.ReadCsv<Class1>(stream2, "Sheet2");
ReadSample1File_test(xl);
}

[TestMethod]
public void ReadSample1File()
{
Expand Down Expand Up @@ -55,7 +67,8 @@ public void ReadAndWrite()
Assert.AreEqual(xl.GetSheet<Class1>().Count, xl2.GetSheet<Class1>().Count);
}

public void ReadSample1File_test(TestFileContext xl) {
public void ReadSample1File_test(TestFileContext xl)
{
var sheet1 = xl.GetSheet<TestFileContext.Sheet1>();
var sheet2 = xl.GetSheet<Class1>();
Assert.AreEqual(2, sheet1.Count);
Expand Down
20 changes: 12 additions & 8 deletions src/Shane32.ExcelLinq.Tests/Models/TestFileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@ namespace Shane32.ExcelLinq.Tests.Models
{
public class TestFileContext : ExcelContext
{
public TestFileContext() : base() { }
public TestFileContext(System.IO.Stream stream) : base(stream) { }
public TestFileContext(string filename) : base(filename) { }
public TestFileContext(ExcelPackage excelPackage) : base(excelPackage) { }

protected override void OnModelCreating(ExcelModelBuilder builder)
{
Action<ExcelRange> headerFormatter = range => {

range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
};
builder.ReadCsv();

Action<ExcelRange> numberFormatter = range => range.Style.Numberformat.Format = "#,##0.00";
var sheet1 = builder.Sheet<Sheet1>();
sheet1.Column(x => x.Date)
sheet1.Column(x => x.Date).AlternateName("its a date")
.HeaderFormatter(headerFormatter)
.ColumnFormatter(range => range.Style.Numberformat.Format = "MM/dd/yyyy");
sheet1.Column(x => x.Quantity)
.HeaderFormatter(headerFormatter)
.ColumnFormatter(range => range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center);
sheet1.Column(x => x.Description)
.HeaderFormatter(headerFormatter);
sheet1.Column(x => x.Amount)
sheet1.Column(x => x.Amount).AlternateName("some amount")
.HeaderFormatter(headerFormatter)
.ColumnFormatter(numberFormatter);
sheet1.Column(x => x.Total)
Expand Down Expand Up @@ -62,19 +66,19 @@ protected override void OnModelCreating(ExcelModelBuilder builder)
sheet1.WriteRangeLocator(worksheet => worksheet.Cells[3, 1]);
sheet1.WritePolisher((worksheet, range) => {
worksheet.Calculate();
for (int col = 1; col <= worksheet.Dimension.End.Column; col++) {
var column = worksheet.Column(col);
column.AutoFit();
column.Width *= 1.2;
}
//for (int col = 1; col <= worksheet.Dimension.End.Column; col++) {
// var column = worksheet.Column(col);
// column.AutoFit();
// column.Width *= 1.2;
//}
worksheet.Cells[1, 1].Value = "This is a test header";
});

var sheet2 = builder.Sheet<Class1>("Sheet2");
sheet2.Column(x => x.IntColumn);
sheet2.Column(x => x.FloatColumn);
sheet2.Column(x => x.DoubleColumn);
sheet2.Column(x => x.StringColumn);
sheet2.Column(x => x.StringColumn).AlternateName("be a string column");
sheet2.Column(x => x.BooleanColumn);
sheet2.Column(x => x.DateTimeColumn)
.ColumnFormatter(range => range.Style.Numberformat.Format = "MM/dd/yyyy hh:mm AM/PM");
Expand Down
18 changes: 18 additions & 0 deletions src/Shane32.ExcelLinq.Tests/Shane32.ExcelLinq.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net462;net48</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<None Remove="test.csv" />
<None Remove="test1.csv" />
<None Remove="test2.csv" />
</ItemGroup>

<ItemGroup>
<Content Include="test.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="test1.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="test2.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
10 changes: 10 additions & 0 deletions src/Shane32.ExcelLinq.Tests/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
IntColumn,FloatColumn,DoubleColumn,StringColumn,BooleanColumn,DateTimeColumn,TimespanColumn,UriColumn,GuidColumn,NullableIntColumn
1,1,1,test,TRUE,2-Aug,02:00 PM,http://localhost/test,f1dc7e7d-d63e-4279-8dfd-cecb6e26cda8,3
1.1,1.1,1.1,test2,FALSE,8/1/2020,14:00,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,3.1
1,1.1,1.1,test2,true,8/3/20,14:00,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,3
1.1,1.1,1.1,test2,yes,8/1/20 2:30 PM,02:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,NO,8/1/20 2:30 PM,02:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,y,8/1/20 2:30 PM,02:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,N,8/1/20 2:30 PM,02:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,1,8/1/20 2:30 PM,02:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,0,8/1/20 2:30 PM,02:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
3 changes: 3 additions & 0 deletions src/Shane32.ExcelLinq.Tests/test1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Date,Quantity,Description,Amount, Total ,Notes
7/1/2020,52,Widgets,45.99 ," $2,391.48 ",
7/23/2020,22,Bolts,2.54 , $55.88 ,Each bolt is a set of two
10 changes: 10 additions & 0 deletions src/Shane32.ExcelLinq.Tests/test2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
IntColumn,FloatColumn,DoubleColumn,StringColumn,BooleanColumn,DateTimeColumn,TimespanColumn,UriColumn,GuidColumn,NullableIntColumn
1,1,1,test,TRUE,2-Aug,2:00 PM,http://localhost/test,f1dc7e7d-d63e-4279-8dfd-cecb6e26cda8,3
1.1,1.1,1.1,test2,FALSE,8/1/2020,14:00,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,3.1
1,1.1,1.1,test2,true,8/3/20,14:00,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,3
1.1,1.1,1.1,test2,yes,8/1/20 2:30 PM,2:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,NO,8/1/20 2:30 PM,2:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,y,8/1/20 2:30 PM,2:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,N,8/1/20 2:30 PM,2:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,1,8/1/20 2:30 PM,2:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
1.1,1.1,1.1,test2,0,8/1/20 2:30 PM,2:34,http://localhost/help,89892480-4179-42c7-9e2f-e0bb1094dd6b,
5 changes: 4 additions & 1 deletion src/Shane32.ExcelLinq/Builders/ExcelModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public class ExcelModelBuilder : IExcelModel, ISheetModelLookup
internal Dictionary<string, ISheetModel> _sheetDictionary = new Dictionary<string, ISheetModel>();
internal Dictionary<Type, ISheetModel> _typeDictionary = new Dictionary<Type, ISheetModel>();
private bool _ignoreSheetNames;
public bool _readCsv;

public SheetModelBuilder<T> Sheet<T>() where T : new()
{
return Sheet<T>(typeof(T).Name);
}

public SheetModelBuilder<T> Sheet<T>(string name) where T : new()
{
if (name == null)
Expand All @@ -34,6 +35,8 @@ public void IgnoreSheetNames()
{
_ignoreSheetNames = true;
}
bool IExcelModel.ReadCsv => _readCsv;
public void ReadCsv() => _readCsv = true;

IEnumerator<ISheetModel> IEnumerable<ISheetModel>.GetEnumerator() => _sheets.GetEnumerator();

Expand Down
1 change: 1 addition & 0 deletions src/Shane32.ExcelLinq/Builders/SheetModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public SheetModelBuilder(ExcelModelBuilder excelModelBuilder, string name)
excelModelBuilder._typeDictionary.Add(typeof(T), this);
}


public SheetModelBuilder<T> AlternateName(string name)
{
if (string.IsNullOrWhiteSpace(name))
Expand Down
Loading