2021-04-18 11:59:34 +08:00
using Dapper ;
using MiniExcelLibs ;
2021-04-14 15:02:43 +08:00
using MiniExcelLibs.Tests.Utils ;
2021-04-13 15:19:39 +08:00
using System ;
using System.Collections.Generic ;
2021-04-18 12:39:40 +08:00
using System.Data ;
2021-04-13 15:19:39 +08:00
using System.IO ;
using System.Linq ;
2021-06-21 04:58:06 +08:00
using System.Threading.Tasks ;
2021-04-13 15:19:39 +08:00
using Xunit ;
namespace MiniExcelTests
{
public class MiniExcelTemplateTests
{
2021-04-18 12:39:40 +08:00
[Fact]
public void DatatableTemptyRowTest ( )
{
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateComplex.xlsx" ;
2021-04-18 12:39:40 +08:00
var managers = new DataTable ( ) ;
{
managers . Columns . Add ( "name" ) ;
managers . Columns . Add ( "department" ) ;
}
var employees = new DataTable ( ) ;
{
employees . Columns . Add ( "name" ) ;
employees . Columns . Add ( "department" ) ;
}
var value = new Dictionary < string , object > ( )
{
["title"] = "FooCompany" ,
["managers"] = managers ,
["employees"] = employees
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
{
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C5" , demension ) ;
}
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateComplex.xlsx" ;
2021-04-18 12:39:40 +08:00
var managers = new DataTable ( ) ;
{
managers . Columns . Add ( "name" ) ;
managers . Columns . Add ( "department" ) ;
managers . Rows . Add ( "Jack" , "HR" ) ;
}
var employees = new DataTable ( ) ;
{
employees . Columns . Add ( "name" ) ;
employees . Columns . Add ( "department" ) ;
employees . Rows . Add ( "Wade" , "HR" ) ;
}
var value = new Dictionary < string , object > ( )
{
["title"] = "FooCompany" ,
["managers"] = managers ,
["employees"] = employees
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
{
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C5" , demension ) ;
}
}
}
[Fact]
public void DatatableTest ( )
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateComplex.xlsx" ;
2021-04-18 12:39:40 +08:00
var managers = new DataTable ( ) ;
{
managers . Columns . Add ( "name" ) ;
managers . Columns . Add ( "department" ) ;
managers . Rows . Add ( "Jack" , "HR" ) ;
managers . Rows . Add ( "Loan" , "IT" ) ;
}
var employees = new DataTable ( ) ;
{
employees . Columns . Add ( "name" ) ;
employees . Columns . Add ( "department" ) ;
employees . Rows . Add ( "Wade" , "HR" ) ;
employees . Rows . Add ( "Felix" , "HR" ) ;
employees . Rows . Add ( "Eric" , "IT" ) ;
employees . Rows . Add ( "Keaton" , "IT" ) ;
}
var value = new Dictionary < string , object > ( )
{
["title"] = "FooCompany" ,
["managers"] = managers ,
["employees"] = employees
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
{
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
Assert . Equal ( 9 , rows . Count ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
}
{
var rows = MiniExcel . Query ( path , sheetName : "Sheet2" ) . ToList ( ) ;
Assert . Equal ( 9 , rows . Count ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
}
}
2021-04-18 11:59:34 +08:00
[Fact]
public void DapperTemplateTest ( )
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateComplex.xlsx" ;
2021-04-18 11:59:34 +08:00
var connection = Db . GetConnection ( "Data Source=:memory:" ) ;
var value = new Dictionary < string , object > ( )
{
["title"] = "FooCompany" ,
["managers"] = connection . Query ( "select 'Jack' name,'HR' department union all select 'Loan','IT'" ) ,
["employees"] = connection . Query ( @"select 'Wade' name,'HR' department union all select 'Felix','HR' union all select 'Eric','IT' union all select 'Keaton','IT'" )
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
{
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( 9 , rows . Count ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
}
{
var rows = MiniExcel . Query ( path , sheetName : "Sheet2" ) . ToList ( ) ;
Assert . Equal ( 9 , rows . Count ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
}
}
[Fact]
public void DictionaryTemplateTest ( )
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateComplex.xlsx" ;
2021-04-18 11:59:34 +08:00
var value = new Dictionary < string , object > ( )
{
["title"] = "FooCompany" ,
["managers"] = new [ ] {
new Dictionary < string , object > { [ "name" ] = "Jack" , [ "department" ] = "HR" } ,
new Dictionary < string , object > { [ "name" ] = "Loan" , [ "department" ] = "IT" }
} ,
["employees"] = new [ ] {
new Dictionary < string , object > { [ "name" ] = "Wade" , [ "department" ] = "HR" } ,
new Dictionary < string , object > { [ "name" ] = "Felix" , [ "department" ] = "HR" } ,
new Dictionary < string , object > { [ "name" ] = "Eric" , [ "department" ] = "IT" } ,
new Dictionary < string , object > { [ "name" ] = "Keaton" , [ "department" ] = "IT" }
}
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
{
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( 9 , rows . Count ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
}
{
var rows = MiniExcel . Query ( path , sheetName : "Sheet2" ) . ToList ( ) ;
Assert . Equal ( 9 , rows . Count ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
}
}
2021-04-15 10:36:52 +08:00
[Fact]
public void TestGithubProject ( )
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateGithubProjects.xlsx" ;
2021-04-15 10:36:52 +08:00
var projects = new [ ]
{
new { Name = "MiniExcel" , Link = "https://github.com/shps951023/MiniExcel" , Star = 146 , CreateTime = new DateTime ( 2021 , 03 , 01 ) } ,
new { Name = "HtmlTableHelper" , Link = "https://github.com/shps951023/HtmlTableHelper" , Star = 16 , CreateTime = new DateTime ( 2020 , 02 , 01 ) } ,
new { Name = "PocoClassGenerator" , Link = "https://github.com/shps951023/PocoClassGenerator" , Star = 16 , CreateTime = new DateTime ( 2019 , 03 , 17 ) }
} ;
var value = new
{
User = "ITWeiHan" ,
Projects = projects ,
TotalStar = projects . Sum ( s = > s . Star )
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
2021-04-15 11:00:53 +08:00
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "ITWeiHan Github Projects" , rows [ 0 ] . B ) ;
Assert . Equal ( "Total Star : 178" , rows [ 8 ] . C ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:D9" , demension ) ;
2021-04-15 10:36:52 +08:00
}
2021-04-14 22:28:43 +08:00
2021-04-15 10:36:52 +08:00
public class TestIEnumerableTypePoco
2021-04-14 22:28:43 +08:00
{
public string @string { get ; set ; }
public int? @int { get ; set ; }
public decimal? @decimal { get ; set ; }
public double? @double { get ; set ; }
public DateTime ? datetime { get ; set ; }
public bool? @bool { get ; set ; }
public Guid ? Guid { get ; set ; }
}
[Fact]
public void TestIEnumerableType ( )
{
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestIEnumerableType.xlsx" ;
2021-04-14 22:28:43 +08:00
//1. By POCO
2021-04-15 10:36:52 +08:00
var poco = new TestIEnumerableTypePoco { @string = "string" , @int = 123 , @decimal = decimal . Parse ( "123.45" ) , @double = ( double ) 123.33 , @datetime = new DateTime ( 2021 , 4 , 1 ) , @bool = true , @Guid = Guid . NewGuid ( ) } ;
2021-04-14 22:28:43 +08:00
var value = new
{
Ts = new [ ] {
2021-04-15 10:36:52 +08:00
poco ,
new TestIEnumerableTypePoco { } ,
2021-04-14 22:42:25 +08:00
null ,
2021-04-15 10:36:52 +08:00
new TestIEnumerableTypePoco { } ,
poco
2021-04-14 22:28:43 +08:00
}
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
2021-04-15 10:36:52 +08:00
var rows = MiniExcel . Query < TestIEnumerableTypePoco > ( path ) . ToList ( ) ;
Assert . Equal ( poco . @string , rows [ 0 ] . @string ) ;
Assert . Equal ( poco . @int , rows [ 0 ] . @int ) ;
Assert . Equal ( poco . @double , rows [ 0 ] . @double ) ;
Assert . Equal ( poco . @decimal , rows [ 0 ] . @decimal ) ;
Assert . Equal ( poco . @bool , rows [ 0 ] . @bool ) ;
Assert . Equal ( poco . datetime , rows [ 0 ] . datetime ) ;
Assert . Equal ( poco . Guid , rows [ 0 ] . Guid ) ;
2021-04-14 22:28:43 +08:00
Assert . Null ( rows [ 1 ] . @string ) ;
Assert . Null ( rows [ 1 ] . @int ) ;
Assert . Null ( rows [ 1 ] . @double ) ;
Assert . Null ( rows [ 1 ] . @decimal ) ;
Assert . Null ( rows [ 1 ] . @bool ) ;
Assert . Null ( rows [ 1 ] . datetime ) ;
Assert . Null ( rows [ 1 ] . Guid ) ;
2021-04-14 22:42:25 +08:00
// special input null but query is empty vo
Assert . Null ( rows [ 2 ] . @string ) ;
Assert . Null ( rows [ 2 ] . @int ) ;
Assert . Null ( rows [ 2 ] . @double ) ;
Assert . Null ( rows [ 2 ] . @decimal ) ;
Assert . Null ( rows [ 2 ] . @bool ) ;
Assert . Null ( rows [ 2 ] . datetime ) ;
Assert . Null ( rows [ 2 ] . Guid ) ;
2021-04-14 22:28:43 +08:00
2021-04-14 22:42:25 +08:00
Assert . Null ( rows [ 3 ] . @string ) ;
Assert . Null ( rows [ 3 ] . @int ) ;
Assert . Null ( rows [ 3 ] . @double ) ;
Assert . Null ( rows [ 3 ] . @decimal ) ;
Assert . Null ( rows [ 3 ] . @bool ) ;
Assert . Null ( rows [ 3 ] . datetime ) ;
Assert . Null ( rows [ 3 ] . Guid ) ;
2021-04-14 22:28:43 +08:00
2021-04-14 22:42:25 +08:00
2021-04-15 10:36:52 +08:00
Assert . Equal ( poco . @string , rows [ 4 ] . @string ) ;
Assert . Equal ( poco . @int , rows [ 4 ] . @int ) ;
Assert . Equal ( poco . @double , rows [ 4 ] . @double ) ;
Assert . Equal ( poco . @decimal , rows [ 4 ] . @decimal ) ;
Assert . Equal ( poco . @bool , rows [ 4 ] . @bool ) ;
Assert . Equal ( poco . datetime , rows [ 4 ] . datetime ) ;
Assert . Equal ( poco . Guid , rows [ 4 ] . Guid ) ;
2021-04-14 22:28:43 +08:00
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
2021-04-14 22:42:25 +08:00
Assert . Equal ( "A1:G6" , demension ) ;
2021-04-14 22:28:43 +08:00
}
}
2021-04-15 10:36:52 +08:00
[Fact]
public void TestTemplateTypeMapping ( )
{
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestITemplateTypeAutoMapping.xlsx" ;
2021-04-15 10:36:52 +08:00
//1. By POCO
var value = new TestIEnumerableTypePoco { @string = "string" , @int = 123 , @decimal = decimal . Parse ( "123.45" ) , @double = ( double ) 123.33 , @datetime = new DateTime ( 2021 , 4 , 1 ) , @bool = true , @Guid = Guid . NewGuid ( ) } ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
var rows = MiniExcel . Query < TestIEnumerableTypePoco > ( path ) . ToList ( ) ;
Assert . Equal ( value . @string , rows [ 0 ] . @string ) ;
Assert . Equal ( value . @int , rows [ 0 ] . @int ) ;
Assert . Equal ( value . @double , rows [ 0 ] . @double ) ;
Assert . Equal ( value . @decimal , rows [ 0 ] . @decimal ) ;
Assert . Equal ( value . @bool , rows [ 0 ] . @bool ) ;
Assert . Equal ( value . datetime , rows [ 0 ] . datetime ) ;
Assert . Equal ( value . Guid , rows [ 0 ] . Guid ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:G2" , demension ) ;
}
}
2021-04-14 22:28:43 +08:00
[Fact]
public void TemplateCenterEmptyTest ( )
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateCenterEmpty.xlsx" ;
2021-04-14 22:28:43 +08:00
var value = new
{
2021-04-15 10:36:52 +08:00
Tests = Enumerable . Range ( 1 , 5 ) . Select ( ( s , i ) = > new { test1 = i , test2 = i } )
2021-04-14 22:28:43 +08:00
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
}
2021-04-13 18:40:04 +08:00
[Fact]
public void TemplateBasiTest ( )
{
{
2021-04-13 23:33:01 +08:00
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateEasyFill.xlsx" ;
2021-04-13 18:40:04 +08:00
// 1. By POCO
var value = new
{
Name = "Jack" ,
CreateDate = new DateTime ( 2021 , 01 , 01 ) ,
VIP = true ,
Points = 123
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "Jack" , rows [ 1 ] . A ) ;
Assert . Equal ( "2021-01-01 00:00:00" , rows [ 1 ] . B ) ;
Assert . Equal ( true , rows [ 1 ] . C ) ;
Assert . Equal ( 123 , rows [ 1 ] . D ) ;
Assert . Equal ( "Jack has 123 points" , rows [ 1 ] . E ) ;
2021-04-18 00:20:45 +08:00
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:E2" , demension ) ;
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateEasyFill.xlsx" ;
2021-04-18 00:20:45 +08:00
var templateBytes = File . ReadAllBytes ( templatePath ) ;
// 1. By POCO
var value = new
{
Name = "Jack" ,
CreateDate = new DateTime ( 2021 , 01 , 01 ) ,
VIP = true ,
Points = 123
} ;
MiniExcel . SaveAsByTemplate ( path , templateBytes , value ) ;
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "Jack" , rows [ 1 ] . A ) ;
Assert . Equal ( "2021-01-01 00:00:00" , rows [ 1 ] . B ) ;
Assert . Equal ( true , rows [ 1 ] . C ) ;
Assert . Equal ( 123 , rows [ 1 ] . D ) ;
Assert . Equal ( "Jack has 123 points" , rows [ 1 ] . E ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:E2" , demension ) ;
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateEasyFill.xlsx" ;
2021-04-18 00:20:45 +08:00
var templateBytes = File . ReadAllBytes ( templatePath ) ;
// 1. By POCO
var value = new
{
Name = "Jack" ,
CreateDate = new DateTime ( 2021 , 01 , 01 ) ,
VIP = true ,
Points = 123
} ;
using ( var stream = File . Create ( path ) )
{
stream . SaveAsByTemplate ( templateBytes , value ) ;
}
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "Jack" , rows [ 1 ] . A ) ;
Assert . Equal ( "2021-01-01 00:00:00" , rows [ 1 ] . B ) ;
Assert . Equal ( true , rows [ 1 ] . C ) ;
Assert . Equal ( 123 , rows [ 1 ] . D ) ;
Assert . Equal ( "Jack has 123 points" , rows [ 1 ] . E ) ;
2021-04-14 15:02:43 +08:00
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
2021-06-21 04:58:06 +08:00
Assert . Equal ( "A1:E2" , demension ) ;
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
var templatePath = @"../../../../../samples/xlsx/TestTemplateEasyFill.xlsx" ;
// 2. By Dictionary
var value = new Dictionary < string , object > ( )
{
["Name"] = "Jack" ,
["CreateDate"] = new DateTime ( 2021 , 01 , 01 ) ,
["VIP"] = true ,
["Points"] = 123
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "Jack" , rows [ 1 ] . A ) ;
Assert . Equal ( "2021-01-01 00:00:00" , rows [ 1 ] . B ) ;
Assert . Equal ( true , rows [ 1 ] . C ) ;
Assert . Equal ( 123 , rows [ 1 ] . D ) ;
Assert . Equal ( "Jack has 123 points" , rows [ 1 ] . E ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:E2" , demension ) ;
}
}
[Fact]
public async Task TemplateAsyncBasiTest ( )
{
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
var templatePath = @"../../../../../samples/xlsx/TestTemplateEasyFill.xlsx" ;
// 1. By POCO
var value = new
{
Name = "Jack" ,
CreateDate = new DateTime ( 2021 , 01 , 01 ) ,
VIP = true ,
Points = 123
} ;
await MiniExcel . SaveAsByTemplateAsync ( path , templatePath , value ) ;
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "Jack" , rows [ 1 ] . A ) ;
Assert . Equal ( "2021-01-01 00:00:00" , rows [ 1 ] . B ) ;
Assert . Equal ( true , rows [ 1 ] . C ) ;
Assert . Equal ( 123 , rows [ 1 ] . D ) ;
Assert . Equal ( "Jack has 123 points" , rows [ 1 ] . E ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:E2" , demension ) ;
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
var templatePath = @"../../../../../samples/xlsx/TestTemplateEasyFill.xlsx" ;
var templateBytes = File . ReadAllBytes ( templatePath ) ;
// 1. By POCO
var value = new
{
Name = "Jack" ,
CreateDate = new DateTime ( 2021 , 01 , 01 ) ,
VIP = true ,
Points = 123
} ;
await MiniExcel . SaveAsByTemplateAsync ( path , templateBytes , value ) ;
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "Jack" , rows [ 1 ] . A ) ;
Assert . Equal ( "2021-01-01 00:00:00" , rows [ 1 ] . B ) ;
Assert . Equal ( true , rows [ 1 ] . C ) ;
Assert . Equal ( 123 , rows [ 1 ] . D ) ;
Assert . Equal ( "Jack has 123 points" , rows [ 1 ] . E ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:E2" , demension ) ;
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
var templatePath = @"../../../../../samples/xlsx/TestTemplateEasyFill.xlsx" ;
var templateBytes = File . ReadAllBytes ( templatePath ) ;
// 1. By POCO
var value = new
{
Name = "Jack" ,
CreateDate = new DateTime ( 2021 , 01 , 01 ) ,
VIP = true ,
Points = 123
} ;
using ( var stream = File . Create ( path ) )
{
await stream . SaveAsByTemplateAsync ( templateBytes , value ) ;
}
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "Jack" , rows [ 1 ] . A ) ;
Assert . Equal ( "2021-01-01 00:00:00" , rows [ 1 ] . B ) ;
Assert . Equal ( true , rows [ 1 ] . C ) ;
Assert . Equal ( 123 , rows [ 1 ] . D ) ;
Assert . Equal ( "Jack has 123 points" , rows [ 1 ] . E ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
2021-04-14 15:02:43 +08:00
Assert . Equal ( "A1:E2" , demension ) ;
2021-04-13 18:40:04 +08:00
}
{
2021-04-13 23:33:01 +08:00
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateEasyFill.xlsx" ;
2021-04-13 18:40:04 +08:00
// 2. By Dictionary
var value = new Dictionary < string , object > ( )
{
["Name"] = "Jack" ,
["CreateDate"] = new DateTime ( 2021 , 01 , 01 ) ,
["VIP"] = true ,
["Points"] = 123
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "Jack" , rows [ 1 ] . A ) ;
Assert . Equal ( "2021-01-01 00:00:00" , rows [ 1 ] . B ) ;
Assert . Equal ( true , rows [ 1 ] . C ) ;
Assert . Equal ( 123 , rows [ 1 ] . D ) ;
Assert . Equal ( "Jack has 123 points" , rows [ 1 ] . E ) ;
2021-04-14 15:02:43 +08:00
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:E2" , demension ) ;
2021-04-13 18:40:04 +08:00
}
}
2021-04-13 23:33:01 +08:00
[Fact]
public void TestIEnumerable ( )
{
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateBasicIEmumerableFill.xlsx" ;
2021-04-13 23:33:01 +08:00
//1. By POCO
var value = new
{
employees = new [ ] {
new { name = "Jack" , department = "HR" } ,
new { name = "Lisa" , department = "HR" } ,
new { name = "John" , department = "HR" } ,
new { name = "Mike" , department = "IT" } ,
new { name = "Neo" , department = "IT" } ,
new { name = "Loan" , department = "IT" }
}
2021-04-18 22:51:19 +08:00
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:B7" , demension ) ;
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateBasicIEmumerableFill.xlsx" ;
2021-04-18 22:51:19 +08:00
//2. By Dictionary
var value = new Dictionary < string , object > ( )
{
["employees"] = new [ ] {
new { name = "Jack" , department = "HR" } ,
new { name = "Lisa" , department = "HR" } ,
new { name = "John" , department = "HR" } ,
new { name = "Mike" , department = "IT" } ,
new { name = "Neo" , department = "IT" } ,
new { name = "Loan" , department = "IT" }
}
2021-04-13 23:33:01 +08:00
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
2021-04-14 15:02:43 +08:00
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:B7" , demension ) ;
2021-04-13 23:33:01 +08:00
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateBasicIEmumerableFill.xlsx" ;
2021-04-13 23:33:01 +08:00
2021-04-19 02:49:16 +08:00
//3. By DataTable
var dt = new DataTable ( ) ;
{
dt . Columns . Add ( "name" ) ;
dt . Columns . Add ( "department" ) ;
dt . Rows . Add ( "Jack" , "HR" ) ;
dt . Rows . Add ( "Lisa" , "HR" ) ;
dt . Rows . Add ( "John" , "HR" ) ;
dt . Rows . Add ( "Mike" , "IT" ) ;
dt . Rows . Add ( "Neo" , "IT" ) ;
dt . Rows . Add ( "Loan" , "IT" ) ;
}
2021-04-13 23:33:01 +08:00
var value = new Dictionary < string , object > ( )
{
2021-04-19 02:49:16 +08:00
["employees"] = dt
2021-04-13 23:33:01 +08:00
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
2021-04-14 15:02:43 +08:00
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:B7" , demension ) ;
2021-04-13 23:33:01 +08:00
}
}
2021-04-13 18:40:04 +08:00
2021-04-13 15:19:39 +08:00
[Fact]
public void TemplateTest ( )
{
{
2021-04-13 23:33:01 +08:00
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateComplex.xlsx" ;
2021-04-13 23:33:01 +08:00
// 1. By Class
var value = new
{
title = "FooCompany" ,
managers = new [ ] {
new { name = "Jack" , department = "HR" } ,
new { name = "Loan" , department = "IT" }
} ,
employees = new [ ] {
new { name = "Wade" , department = "HR" } ,
new { name = "Felix" , department = "HR" } ,
new { name = "Eric" , department = "IT" } ,
new { name = "Keaton" , department = "IT" }
}
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
2021-04-16 17:31:39 +08:00
{
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
2021-04-14 15:02:43 +08:00
2021-04-16 17:31:39 +08:00
Assert . Equal ( 9 , rows . Count ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
}
{
2021-04-18 10:57:50 +08:00
var rows = MiniExcel . Query ( path , sheetName : "Sheet2" ) . ToList ( ) ;
2021-04-16 17:31:39 +08:00
Assert . Equal ( 9 , rows . Count ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
}
2021-04-13 23:33:01 +08:00
}
{
var path = Path . Combine ( Path . GetTempPath ( ) , $"{Guid.NewGuid().ToString()}.xlsx" ) ;
2021-04-21 19:23:48 +08:00
var templatePath = @"../../../../../samples/xlsx/TestTemplateComplex.xlsx" ;
2021-04-13 23:33:01 +08:00
// 2. By Dictionary
var value = new Dictionary < string , object > ( )
{
["title"] = "FooCompany" ,
["managers"] = new [ ] {
2021-04-18 00:13:41 +08:00
new { name = "Jack" , department = "HR" } ,
new { name = "Loan" , department = "IT" }
} ,
2021-04-13 23:33:01 +08:00
["employees"] = new [ ] {
2021-04-18 00:13:41 +08:00
new { name = "Wade" , department = "HR" } ,
new { name = "Felix" , department = "HR" } ,
new { name = "Eric" , department = "IT" } ,
new { name = "Keaton" , department = "IT" }
}
2021-04-13 23:33:01 +08:00
} ;
MiniExcel . SaveAsByTemplate ( path , templatePath , value ) ;
var rows = MiniExcel . Query ( path ) . ToList ( ) ;
Assert . Equal ( "FooCompany" , rows [ 0 ] . A ) ;
Assert . Equal ( "Jack" , rows [ 2 ] . B ) ;
Assert . Equal ( "HR" , rows [ 2 ] . C ) ;
Assert . Equal ( "Loan" , rows [ 3 ] . B ) ;
Assert . Equal ( "IT" , rows [ 3 ] . C ) ;
Assert . Equal ( "Wade" , rows [ 5 ] . B ) ;
Assert . Equal ( "HR" , rows [ 5 ] . C ) ;
Assert . Equal ( "Felix" , rows [ 6 ] . B ) ;
Assert . Equal ( "HR" , rows [ 6 ] . C ) ;
Assert . Equal ( "Eric" , rows [ 7 ] . B ) ;
Assert . Equal ( "IT" , rows [ 7 ] . C ) ;
Assert . Equal ( "Keaton" , rows [ 8 ] . B ) ;
Assert . Equal ( "IT" , rows [ 8 ] . C ) ;
2021-04-14 15:02:43 +08:00
var demension = Helpers . GetFirstSheetDimensionRefValue ( path ) ;
Assert . Equal ( "A1:C9" , demension ) ;
2021-04-13 23:33:01 +08:00
}
2021-04-13 15:19:39 +08:00
}
}
}