-
-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathExcelExporter_Tests.cs
1098 lines (912 loc) · 43.2 KB
/
ExcelExporter_Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ======================================================================
//
// filename : ExcelExporter_Tests.cs
// description :
//
// created by 雪雁 at 2019-09-11 13:51
// 文档官网:https://docs.xin-lai.com
// 公众号教程:麦扣聊技术
// QQ群:85318032(编程交流)
// Blog:http://www.cnblogs.com/codelove/
//
// ======================================================================
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Extension;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Tests.Extensions;
using Magicodes.ExporterAndImporter.Tests.Models.Export;
using Magicodes.ExporterAndImporter.Tests.Models.Export.ExportByTemplate_Test1;
using Magicodes.IE.Core;
using Newtonsoft.Json.Linq;
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Style;
using Shouldly;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Magicodes.ExporterAndImporter.Tests
{
public class ExcelExporter_Tests : TestBase
{
/// <summary>
/// 将entities直接转成DataTable
/// </summary>
/// <typeparam name="T">Entity type</typeparam>
/// <param name="entities">entity集合</param>
/// <returns>将Entity的值转为DataTable</returns>
private static DataTable EntityToDataTable<T>(DataTable dt, IEnumerable<T> entities)
{
if (!entities.Any()) return dt;
var properties = typeof(T).GetProperties();
foreach (var entity in entities)
{
var dr = dt.NewRow();
foreach (var property in properties)
if (dt.Columns.Contains(property.Name))
dr[property.Name] = property.GetValue(entity, null);
dt.Rows.Add(dr);
}
return dt;
}
[Fact(DisplayName = "DTO特性导出(测试格式化以及列头索引)")]
public async Task AttrsExport_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(AttrsExport_Test)}.xlsx");
DeleteFile(filePath);
var data = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>(100);
foreach (var item in data)
{
item.LongNo = 458752665;
item.Text = "测试长度超出单元格的字符串";
}
var result = await exporter.Export(filePath, data);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells[sheet.Dimension.Address].Rows.ShouldBe(101);
sheet.Cells["A2"].Text.ShouldBe(data[0].Text2);
//[ExporterHeader(DisplayName = "日期1", Format = "yyyy-MM-dd")]
sheet.Cells["E2"].Text.Equals(DateTime.Parse(sheet.Cells["E2"].Text).ToString("yyyy-MM-dd"));
//[ExporterHeader(DisplayName = "日期2", Format = "yyyy-MM-dd HH:mm:ss")]
sheet.Cells["F2"].Text.Equals(DateTime.Parse(sheet.Cells["F2"].Text).ToString("yyyy-MM-dd HH:mm:ss"));
//默认DateTime
sheet.Cells["G2"].Text.Equals(DateTime.Parse(sheet.Cells["G2"].Text).ToString("yyyy-MM-dd"));
//单元格宽度测试
sheet.Column(sheet.Cells.First(p => p.Text == "Time3").End.Column).Width.ShouldBe(100);
sheet.Cells["I1"].Style.Font.Size.ShouldBe(18);
sheet.Cells["I2"].Style.Font.Size.ShouldBe(12);
sheet.Cells["A2"].Style.Font.Size.ShouldBe(12);
sheet.Cells["A1"].Style.Font.Size.ShouldBe(11);
sheet.Tables.Count.ShouldBe(1);
var tb = sheet.Tables.First();
tb.Columns.Count.ShouldBe(9);
tb.Columns.First().Name.ShouldBe("普通文本");
tb.Columns[tb.Columns.Count - 1].Name.ShouldBe("加粗文本");
}
}
[Fact(DisplayName = "导出字段顺序测试")]
public async Task ExportByColumnIndex_Test()
{
var exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportByColumnIndex_Test)}.xlsx");
DeleteFile(filePath);
var data = GenFu.GenFu.ListOf<Issue179>(100);
var result = await exporter.Export(filePath, data);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var sheet = pck.Workbook.Worksheets.First();
sheet.Tables.Count.ShouldBe(1);
var tb = sheet.Tables.First();
tb.Columns.Count.ShouldBe(typeof(Issue179).GetProperties().Where(p => !p.GetAttribute<ExporterHeaderAttribute>().IsIgnore).Count());
tb.Columns.First().Name.ShouldBe("员工姓名");
tb.Columns[1].Name.ShouldBe("料号");
}
}
[Fact(DisplayName = "空数据导出")]
public async Task AttrsExportWithNoData_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(AttrsExportWithNoData_Test)}.xlsx");
DeleteFile(filePath);
var data = new List<ExportTestDataWithAttrs>();
var result = await exporter.Export(filePath, data);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
pck.Workbook.Worksheets.First().Cells[pck.Workbook.Worksheets.First().Dimension.Address].Rows
.ShouldBe(1);
}
}
[Fact(DisplayName = "全局居中数据导出测试")]
public async Task AttrExportWithAutoCenterData_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(AttrExportWithAutoCenterData_Test)}.xlsx");
DeleteFile(filePath);
var result = await exporter.Export(filePath, GenFu.GenFu.ListOf<ExportTestDataWithAutoCenter>());
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
pck.Workbook.Worksheets.First().Cells[pck.Workbook.Worksheets.First().Dimension.Address].Rows
.ShouldBe(26);
pck.Workbook.Worksheets.First()
.Cells[1, 1, 10, 2].Style.HorizontalAlignment.ShouldBe(ExcelHorizontalAlignment.Center);
pck.Workbook.Worksheets.First()
.Cells[2, 2, 10, 2].Style.HorizontalAlignment.ShouldBe(ExcelHorizontalAlignment.Center);
}
}
[Fact(DisplayName = "居中数据导出测试")]
public async Task AttrExportWithColAutoCenterData_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(AttrExportWithAutoCenterData_Test)}.xlsx");
DeleteFile(filePath);
var result = await exporter.Export(filePath, GenFu.GenFu.ListOf<ExportTestDataWithColAutoCenter>());
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
pck.Workbook.Worksheets.First().Cells[pck.Workbook.Worksheets.First().Dimension.Address].Rows
.ShouldBe(26);
pck.Workbook.Worksheets.First()
.Cells[1, 1, 10, 2].Style.HorizontalAlignment.ShouldBe(ExcelHorizontalAlignment.Center);
}
}
[Fact(DisplayName = "数据拆分多Sheet导出")]
public async Task SplitData_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(SplitData_Test)}-1.xlsx");
DeleteFile(filePath);
var result = await exporter.Export(filePath,
GenFu.GenFu.ListOf<ExportTestDataWithSplitSheet>(300));
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//验证Sheet数是否为3
pck.Workbook.Worksheets.Count.ShouldBe(3);
//检查忽略列
pck.Workbook.Worksheets.First().Cells["C1"].Value.ShouldBe("数值");
pck.Workbook.Worksheets.First().Cells[pck.Workbook.Worksheets.First().Dimension.Address].Rows
.ShouldBe(101);
}
filePath = GetTestFilePath($"{nameof(SplitData_Test)}-2.xlsx");
DeleteFile(filePath);
result = await exporter.Export(filePath,
GenFu.GenFu.ListOf<ExportTestDataWithSplitSheet>(299));
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//验证Sheet数是否为3
pck.Workbook.Worksheets.Count.ShouldBe(3);
//请不要使用索引(NET461和.NET Core的Sheet索引值不一致)
var lastSheet = pck.Workbook.Worksheets.Last();
lastSheet.Cells[lastSheet.Dimension.Address].Rows.ShouldBe(100);
}
filePath = GetTestFilePath($"{nameof(SplitData_Test)}-3.xlsx");
DeleteFile(filePath);
result = await exporter.Export(filePath,
GenFu.GenFu.ListOf<ExportTestDataWithSplitSheet>(302));
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//验证Sheet数是否为4
pck.Workbook.Worksheets.Count.ShouldBe(4);
//请不要使用索引(NET461和.NET Core的Sheet索引值不一致)
var lastSheet = pck.Workbook.Worksheets.Last();
lastSheet.Cells[lastSheet.Dimension.Address].Rows.ShouldBe(3);
}
}
[Fact(DisplayName = "头部筛选器测试")]
public async Task ExporterHeaderFilter_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"{nameof(ExporterHeaderFilter_Test)}.xlsx");
#region 通过筛选器修改列名
if (File.Exists(filePath)) File.Delete(filePath);
var data1 = GenFu.GenFu.ListOf<ExporterHeaderFilterTestData1>();
var result = await exporter.Export(filePath, data1);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells["D1"].Value.ShouldBe("name");
sheet.Dimension.Columns.ShouldBe(4);
}
#endregion 通过筛选器修改列名
#region 通过筛选器修改忽略列
if (File.Exists(filePath)) File.Delete(filePath);
var data2 = GenFu.GenFu.ListOf<ExporterHeaderFilterTestData2>();
result = await exporter.Export(filePath, data2);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Dimension.Columns.ShouldBe(5);
}
#endregion 通过筛选器修改忽略列
}
[Fact(DisplayName = "DataTable结合DTO导出Excel")]
public async Task DynamicExport_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(DynamicExport_Test) + ".xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
var exportDatas = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>(1000);
var dt = exportDatas.ToDataTable();
var result = await exporter.Export<ExportTestDataWithAttrs>(filePath, dt);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Dimension.Columns.ShouldBe(9);
}
}
[Fact(DisplayName = "DataTable结合DTO自定义行开始位置导出Excel")]
public async Task DynamicExportCustomRowStartIndex_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(DynamicExportCustomRowStartIndex_Test) + ".xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
var exportDatas = GenFu.GenFu.ListOf<ExportTestDataWithAttrsCustomRowStartIndex>(1000);
var dt = exportDatas.ToDataTable();
var result = await exporter.Export<ExportTestDataWithAttrsCustomRowStartIndex>(filePath, dt);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Dimension.Columns.ShouldBe(9);
sheet.Dimension.Rows.ShouldBe(1005);
}
}
[Fact(DisplayName = "DataTable结合DTO类型导出ByteArray Excel")]
public async Task DynamicExport_ByteArray_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(DynamicExport_Test) + ".xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
var exportDatas = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>(1000);
var dt = exportDatas.ToDataTable();
var result = await exporter.ExportAsByteArray<ExportTestDataWithAttrs>(dt);
result.ShouldNotBeNull();
using (var file = File.OpenWrite(filePath))
{
file.Write(result, 0, result.Length);
}
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Dimension.Columns.ShouldBe(9);
}
}
[Fact(DisplayName = "DataTable结合Type类型导出ByteArray Excel")]
public async Task DynamicExportByType_ByteArray_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(DynamicExport_Test) + ".xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
var exportDatas = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>(1000);
var dt = exportDatas.ToDataTable();
var result = await exporter.ExportAsByteArray(dt, typeof(ExportTestDataWithAttrs));
result.ShouldNotBeNull();
using (var file = File.OpenWrite(filePath))
{
file.Write(result, 0, result.Length);
}
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Dimension.Columns.ShouldBe(9);
}
}
[Fact(DisplayName = "DataTable导出Excel(无需定义类,支持列筛选器和表拆分)")]
public async Task DynamicDataTableExport_Test()
{
IExcelExporter exporter = new ExcelExporter();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(DynamicDataTableExport_Test) + ".xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
var exportDatas = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>(50);
var dt = new DataTable();
//创建带列名和类型名的列
dt.Columns.Add("Text", Type.GetType("System.String"));
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Number", Type.GetType("System.Decimal"));
dt = EntityToDataTable(dt, exportDatas);
//加个筛选器导出
var result = await exporter.Export(filePath, dt, new DataTableTestExporterHeaderFilter(), 10);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//判断Sheet拆分
pck.Workbook.Worksheets.Count.ShouldBe(5);
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells["C1"].Value.ShouldBe("数值");
sheet.Dimension.Columns.ShouldBe(3);
}
}
#if DEBUG
[Fact(DisplayName = "大量数据导出Excel", Skip = "本地Debug模式下跳过,太费时")]
#else
[Fact(DisplayName = "大量数据导出Excel")]
#endif
public async Task Export100000Data_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(Export100000Data_Test) + ".xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
var result = await exporter.Export(filePath, GenFu.GenFu.ListOf<ExportTestData>(100000));
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
}
[Fact(DisplayName = "DTO导出")]
public async Task ExportAsByteArray_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportAsByteArray_Test)}.xlsx");
DeleteFile(filePath);
var result = await exporter.ExportAsByteArray(GenFu.GenFu.ListOf<ExportTestDataWithAttrs>());
result.ShouldNotBeNull();
result.Length.ShouldBeGreaterThan(0);
File.WriteAllBytes(filePath, result);
File.Exists(filePath).ShouldBeTrue();
}
[Fact(DisplayName = "DTO导出支持动态类型")]
public async Task ExportAsByteArraySupportDynamicType_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportAsByteArraySupportDynamicType_Test)}.xlsx");
DeleteFile(filePath);
var source = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>();
string fields = "text,number,name";
var shapedData = source.ShapeData(fields) as ICollection<ExpandoObject>;
var result = await exporter.ExportAsByteArray<ExpandoObject>(shapedData);
result.ShouldNotBeNull();
result.Length.ShouldBeGreaterThan(0);
File.WriteAllBytes(filePath, result);
File.Exists(filePath).ShouldBeTrue();
}
[Fact(DisplayName = "导出分割当前Sheet追加Column")]
public async Task ExprotSeparateByColumn_Test()
{
var exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExprotSeparateByColumn_Test)}.xlsx");
DeleteFile(filePath);
var list1 = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>();
var list2 = GenFu.GenFu.ListOf<ExportTestDataWithSplitSheet>(30);
var result = await exporter.Append(list1).SeparateByColumn().Append(list2)
.SeparateByColumn()
.Append(list2).ExportAppendData(filePath);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
}
}
[Fact(DisplayName = "多个sheet导出")]
public async Task ExportMutiCollection_Test()
{
var exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportMutiCollection_Test)}.xlsx");
DeleteFile(filePath);
var list1 = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>();
var list2 = GenFu.GenFu.ListOf<ExportTestDataWithSplitSheet>(30);
var result = exporter.Append(list1, "sheet1").SeparateBySheet().Append(list2).ExportAppendData(filePath);
await result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(2);
pck.Workbook.Worksheets.First().Name
.ShouldBe("sheet1");
pck.Workbook.Worksheets.Last().Name
.ShouldBe(typeof(ExportTestDataWithSplitSheet).GetAttribute<ExcelExporterAttribute>().Name);
}
}
[Fact(DisplayName = "多个sheet导出(空数据)")]
public async Task ExportMutiCollectionWithEmpty_Test()
{
var exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportMutiCollectionWithEmpty_Test)}.xlsx");
DeleteFile(filePath);
var list1 = new List<ExportTestDataWithAttrs>();
var list2 = new List<ExportTestDataWithSplitSheet>();
var result = exporter.Append(list1).SeparateBySheet().Append(list2).ExportAppendData(filePath);
await result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(2);
}
//#299 https://github.com/dotnetcore/Magicodes.IE/issues/299
DeleteFile(filePath);
list1 = GenFu.A.ListOf<ExportTestDataWithAttrs>();
list2 = new List<ExportTestDataWithSplitSheet>();
result = exporter.Append(list1).SeparateBySheet().Append(list2).ExportAppendData(filePath);
await result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(2);
}
}
[Fact(DisplayName = "通过Dto导出表头")]
public async Task ExportHeaderAsByteArray_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportHeaderAsByteArray_Test)}.xlsx");
DeleteFile(filePath);
var result = await exporter.ExportHeaderAsByteArray(GenFu.GenFu.New<ExportTestDataWithAttrs>());
result.ShouldNotBeNull();
result.Length.ShouldBeGreaterThan(0);
result.ToExcelExportFileInfo(filePath);
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Name.ShouldBe("测试");
sheet.Dimension.Columns.ShouldBe(9);
}
}
[Fact(DisplayName = "通过动态传值导出表头")]
public async Task ExportHeaderAsByteArrayWithItems_Test()
{
IExcelExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportHeaderAsByteArrayWithItems_Test)}.xlsx");
DeleteFile(filePath);
var arr = new[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6" };
var sheetName = "Test";
var result = await exporter.ExportHeaderAsByteArray(arr, sheetName);
result.ShouldNotBeNull();
result.Length.ShouldBeGreaterThan(0);
result.ToExcelExportFileInfo(filePath);
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
sheet.Name.ShouldBe(sheetName);
sheet.Dimension.Columns.ShouldBe(arr.Length);
}
}
#if DEBUG
[Fact(DisplayName = "大数据动态列导出Excel", Skip = "本地Debug模式下跳过,太费时")]
#else
[Fact(DisplayName = "大数据动态列导出Excel")]
#endif
public async Task LargeDataDynamicExport_Test()
{
IExcelExporter exporter = new ExcelExporter();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(LargeDataDynamicExport_Test) + ".xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
var exportDatas = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>(1200000);
var dt = new DataTable();
//创建带列名和类型名的列
dt.Columns.Add("Text", Type.GetType("System.String"));
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Number", Type.GetType("System.Decimal"));
dt = EntityToDataTable(dt, exportDatas);
var result = await exporter.Export(filePath, dt, maxRowNumberOnASheet: 100000);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//判断Sheet拆分
pck.Workbook.Worksheets.Count.ShouldBe(12);
}
}
[Fact(DisplayName = "无特性定义导出测试")]
public async Task ExportTestDataWithoutExcelExporter_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportTestDataWithoutExcelExporter_Test)}.xlsx");
DeleteFile(filePath);
var result = await exporter.Export(filePath,
GenFu.GenFu.ListOf<ExportTestDataWithoutExcelExporter>());
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
}
#region 图片导出
[Fact(DisplayName = "Excel导出图片测试")]
public async Task ExportPicture_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportPicture_Test)}.xlsx");
DeleteFile(filePath);
var data = GenFu.GenFu.ListOf<ExportTestDataWithPicture>(5);
var url = Path.Combine("TestFiles", "ExporterTest.png");
for (var i = 0; i < data.Count; i++)
{
var item = data[i];
item.Img1 = url;
if (i == 4)
item.Img = null;
else
item.Img = "https://docs.microsoft.com/en-us/media/microsoft-logo-dark.png";
}
var result = await exporter.Export(filePath, data);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
//验证Alt
sheet.Cells["G6"].Value.ShouldBe("404");
//验证图片
sheet.Drawings.Count.ShouldBe(9);
foreach (ExcelPicture item in sheet.Drawings)
{
//检查图片位置
new[] { 2, 6 }.ShouldContain(item.From.Column);
item.ShouldNotBeNull();
}
//sheet.Tables.Count.ShouldBe(1);
}
}
[Fact(DisplayName = "Excel导出图片测试自定义开始行位置")]
public async Task ExportPictureCustomRowStartIndex_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportPictureCustomRowStartIndex_Test)}.xlsx");
DeleteFile(filePath);
var data = GenFu.GenFu.ListOf<ExportTestDataWithPictureCustomRowStatIndex>(5);
var url = Path.Combine("TestFiles", "ExporterTest.png");
for (var i = 0; i < data.Count; i++)
{
var item = data[i];
item.Img1 = url;
if (i == 4)
item.Img = null;
else
item.Img = "https://docs.microsoft.com/en-us/media/microsoft-logo-dark.png";
}
var result = await exporter.Export(filePath, data);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
//检查转换结果
var sheet = pck.Workbook.Worksheets.First();
//验证Alt
sheet.Cells["G9"].Value.ShouldBe("404");
//验证图片
sheet.Drawings.Count.ShouldBe(9);
foreach (ExcelPicture item in sheet.Drawings)
{
//检查图片位置
new[] { 2, 6 }.ShouldContain(item.From.Column);
item.ShouldNotBeNull();
}
sheet.Dimension.Start.Row.ShouldBe(4);
sheet.Dimension.Rows.ShouldBe(6);
//sheet.Tables.Count.ShouldBe(1);
}
}
#endregion 图片导出
[Fact(DisplayName = "数据注解导出测试")]
public async Task ExportTestDataAnnotations_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExportTestDataAnnotations_Test)}.xlsx");
DeleteFile(filePath);
var data = GenFu.GenFu.ListOf<ExportTestDataAnnotations>();
data[0].Number = null;
var result = await exporter.Export(filePath,
data);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells["C2"].Text.Equals(DateTime.Parse(sheet.Cells["C2"].Text).ToString("yyyy-MM-dd"));
sheet.Cells["D2"].Text.Equals(DateTime.Parse(sheet.Cells["D2"].Text).ToString("yyyy-MM-dd"));
new List<string> { "是", "否" }.ShouldContain(sheet.Cells["G2"].Text);
sheet.Tables.Count.ShouldBe(1);
var tb = sheet.Tables.First();
var enums = typeof(MyEmum).GetEnumDefinitionList();
var list = new List<string>();
foreach (var (item1, item2, item3, item4) in enums)
{
list.Add(item1);
list.Add(item2.ToString());
list.Add(item3);
list.Add(item4);
}
list.Add("A Test");
list.Add("B Test");
list.ShouldContain(sheet.Cells["E2"].Text);
tb.Columns[0].Name.ShouldBe("Custom列1");
tb.Columns[1].Name.ShouldBe("列2");
tb.Columns.Count.ShouldBe(9);
}
}
[Fact(DisplayName = "样式错误测试")]
public async Task TenExport_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(TenExport_Test)}.xlsx");
DeleteFile(filePath);
var data = GenFu.GenFu.ListOf<GalleryLineExportModel>(100);
var result = await exporter.Export(filePath, data);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
}
[Fact(DisplayName = "导出分割当前Sheet追加Rows")]
public async Task ExprotSeparateByRows_Test()
{
var exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExprotSeparateByRows_Test)}.xlsx");
DeleteFile(filePath);
var list1 = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>();
var list2 = GenFu.GenFu.ListOf<ExportTestDataWithSplitSheet>(30);
var result = await exporter.Append(list1).SeparateByRow().Append(list2)
.ExportAppendData(filePath);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var ec = pck.Workbook.Worksheets.First();
ec.Dimension.Rows.ShouldBe(57);
}
}
[Fact(DisplayName = "导出分割当前Sheet追加Rows和headers")]
public async Task ExprotSeparateByRowsAndHeaders_Test()
{
var exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ExprotSeparateByRowsAndHeaders_Test)}.xlsx");
DeleteFile(filePath);
var list1 = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>();
var list2 = GenFu.GenFu.ListOf<ExportTestDataWithSplitSheet>(30);
var result = await exporter.Append(list1).SeparateByRow().AppendHeaders().Append(list2)
.ExportAppendData(filePath);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var ec = pck.Workbook.Worksheets.First();
ec.Dimension.Rows.ShouldBe(58);
}
}
/// <summary>
/// #140 https://github.com/dotnetcore/Magicodes.IE/issues/140
/// 身份证导出文本格式测试
/// </summary>
/// <returns></returns>
[Fact(DisplayName = "身份证导出文本格式测试")]
public async Task Issue140_IdCardExport_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(Issue140_IdCardExport_Test)}.xlsx");
DeleteFile(filePath);
var list = new List<Issue140_IdCardExportDto>()
{
new Issue140_IdCardExportDto()
{
IdCard = "430626111111111111"
},
new Issue140_IdCardExportDto()
{
IdCard = "430626111111111111"
},
new Issue140_IdCardExportDto()
{
IdCard = "430626111111111111"
},
};
var result = await exporter.Export(filePath, list);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
pck.Workbook.Worksheets.First().Cells[pck.Workbook.Worksheets.First().Dimension.Address].Rows
.ShouldBe(list.Count + 1);
pck.Workbook.Worksheets.First().Cells["A2"].Text.ShouldBe(list[0].IdCard);
}
}
[Fact(DisplayName = "忽略所有列进行导出测试")]
public async Task ItThrowsIfIgnoresAllColumnsExport_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ItThrowsIfIgnoresAllColumnsExport_Test)}.xlsx");
DeleteFile(filePath);
Func<Task> f = async () => await exporter.ExportAsByteArray(GenFu.GenFu.ListOf<ExportTestIgnoreAllColumns>());
var exception = await Assert.ThrowsAsync<ArgumentException>(f);
exception.Message.ShouldBe(Resource.DoNotIgnoreAllTheHeader);
}
[Fact(DisplayName = "ValueMapping测试#337")]
public async Task ValueMapping_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ValueMapping_Test)}.xlsx");
DeleteFile(filePath);
var list = new List<Issue337>()
{
new Issue337()
{
Gender ="男",
IsAlumni = true,
Name ="张三",
IsAlumni2 = true,
},
new Issue337()
{
Gender ="男",
IsAlumni = false,
Name ="张三",
IsAlumni2 = true,
},
new Issue337()
{
Gender ="男",
IsAlumni = null,
Name ="张三",
IsAlumni2 = false,
},
};
var result = await exporter.Export(filePath, list);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells["D2"].Text.ShouldBe("是");
sheet.Cells["D3"].Text.ShouldBe("是");
sheet.Cells["D4"].Text.ShouldBe("否");
sheet.Cells["C2"].Text.ShouldBe("是");
sheet.Cells["C3"].Text.ShouldBe("否");
sheet.Cells["C4"].Text.ShouldBe("");
}
}
[Fact(DisplayName = "ValueMappingsBase测试#544")]
public async Task ValueMappingsBase_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ValueMappingsBase_Test)}.xlsx");
DeleteFile(filePath);
var list = new List<Issue544>()
{
new Issue544()
{
Gender ="男",
IsAlumni = true,
Name ="张三",
IsAlumni2 = true,
},
new Issue544()
{
Gender ="男",
IsAlumni = false,
Name ="张三",
IsAlumni2 = true,
},
new Issue544()
{
Gender ="男",
IsAlumni = null,
Name ="张三",
IsAlumni2 = false,
},
};
var result = await exporter.Export(filePath, list);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))