Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathLoadingDataWithDynamicObjects.cs
More file actions
Latest commit
92 lines (87 loc) · 3.98 KB
/
Copy pathLoadingDataWithDynamicObjects.cs
File metadata and controls
92 lines (87 loc) · 3.98 KB
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
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
07/22/2020 EPPlus Software AB EPPlus 5.2.1
*************************************************************************************************/
usingNewtonsoft.Json;
usingNewtonsoft.Json.Linq;
usingOfficeOpenXml;
usingOfficeOpenXml.LoadFunctions.Params;
usingOfficeOpenXml.Table;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Dynamic;
usingSystem.IO;
usingSystem.Text;
namespaceEPPlusSamples.LoadingData
{
publicstaticclassLoadingDataWithDynamicObjects
{
publicstaticvoidRun()
{
// Create a list of dynamic objects
dynamicp1=newExpandoObject();
p1.Id=1;
p1.FirstName="Ivan";
p1.LastName="Horvat";
p1.Age=21;
dynamicp2=newExpandoObject();
p2.Id=2;
p2.FirstName="John";
p2.LastName="Doe";
p2.Age=45;
dynamicp3=newExpandoObject();
p3.Id=3;
p3.FirstName="Sven";
p3.LastName="Svensson";
p3.Age=68;
List<ExpandoObject>items=newList<ExpandoObject>()
{
p1,
p2,
p3
};
// Create a workbook with a worksheet and load the data into a table
using(varpackage=newExcelPackage(FileUtil.GetCleanFileInfo("04-LoadDynamicObjects.xlsx")))
{
varsheet=package.Workbook.Worksheets.Add("Dynamic");
sheet.Cells["A1"].LoadFromDictionaries(items, c =>
{
// Print headers using the property names
c.PrintHeaders=true;
// insert a space before each capital letter in the header
c.HeaderParsingType=HeaderParsingTypes.CamelCaseToSpace;
// when TableStyle is not TableStyles.None the data will be loaded into a table with the
// selected style.
c.TableStyle=TableStyles.Medium1;
});
package.Save();
}
// Load data from json (in this case a file)
varjsonItems=JsonConvert.DeserializeObject<IEnumerable<ExpandoObject>>(File.ReadAllText(FileUtil.GetFileInfo("04-LoadingData","testdata.json").FullName));
using(varpackage=newExcelPackage(FileUtil.GetCleanFileInfo("04-LoadJsonFromFile.xlsx")))
{
varsheet=package.Workbook.Worksheets.Add("Dynamic");
sheet.Cells["A1"].LoadFromDictionaries(jsonItems, c =>
{
// Print headers using the property names
c.PrintHeaders=true;
// insert a space before each capital letter in the header
c.HeaderParsingType=HeaderParsingTypes.CamelCaseToSpace;
// when TableStyle is not TableStyles.None the data will be loaded into a table with the
// selected style.
c.TableStyle=TableStyles.Medium1;
});
sheet.Cells["D:D"].Style.Numberformat.Format="yyyy-mm-dd";
sheet.Cells[1,1,sheet.Dimension.End.Row,sheet.Dimension.End.Column].AutoFitColumns();
package.Save();
}
}
}
}