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 pathLoadingDataWithTablesSample.cs
More file actions
Latest commit
167 lines (143 loc) · 7.45 KB
/
Copy pathLoadingDataWithTablesSample.cs
File metadata and controls
167 lines (143 loc) · 7.45 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
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
/*************************************************************************************************
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
*************************************************************************************************
01/27/2020 EPPlus Software AB Initial release EPPlus 5
*************************************************************************************************/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingOfficeOpenXml;
usingSystem.Data;
usingOfficeOpenXml.Table;
usingSystem.Reflection;
usingSystem.ComponentModel;
namespaceEPPlusSamples.LoadingData
{
/// <summary>
/// This class shows how to load data in a few ways
/// </summary>
publicstaticclassLoadingDataWithTablesSample
{
publicstaticvoidRun()
{
varpck=newExcelPackage();
//Create a datatable with the directories and files from the current directory...
DataTabledt=GetDataTable(FileUtil.GetDirectoryInfo("."));
varwsDt=pck.Workbook.Worksheets.Add("FromDataTable");
//Load the datatable and set the number formats...
wsDt.Cells["A1"].LoadFromDataTable(dt,true,TableStyles.Medium9);
wsDt.Cells[2,2,dt.Rows.Count+1,2].Style.Numberformat.Format="#,##0";
wsDt.Cells[2,3,dt.Rows.Count+1,4].Style.Numberformat.Format="mm-dd-yy";
wsDt.Cells[wsDt.Dimension.Address].AutoFitColumns();
//Select Name and Created-time...
varcollection=(fromrowindt.Select()selectnew{Name=row["Name"],Created_time=(DateTime)row["Created"]});
varwsEnum=pck.Workbook.Worksheets.Add("FromAnonymous");
//Load the collection starting from cell A1...
wsEnum.Cells["A1"].LoadFromCollection(collection,true,TableStyles.Medium9);
//Add some formating...
wsEnum.Cells[2,2,dt.Rows.Count-1,2].Style.Numberformat.Format="mm-dd-yy";
wsEnum.Cells[wsEnum.Dimension.Address].AutoFitColumns();
//Load a list of FileDTO objects from the datatable...
varwsList=pck.Workbook.Worksheets.Add("FromList");
List<FileDTO>list=(fromrowindt.Select()
selectnewFileDTO
{
Name=row["Name"].ToString(),
Size=row["Size"].GetType()==typeof(long)?(long)row["Size"]:0,
Created=(DateTime)row["Created"],
LastModified=(DateTime)row["Modified"],
IsDirectory=(row["Size"]==DBNull.Value)
}).ToList<FileDTO>();
//Load files ordered by size...
wsList.Cells["A1"].LoadFromCollection(fromfileinlist
orderby file.Size descending
wherefile.IsDirectory==false
selectfile,true,TableStyles.Medium9);
wsList.Cells[2,2,dt.Rows.Count+1,2].Style.Numberformat.Format="#,##0";
wsList.Cells[2,3,dt.Rows.Count+1,4].Style.Numberformat.Format="mm-dd-yy";
//Load directories ordered by Name...
wsList.Cells["F1"].LoadFromCollection(fromfileinlist
orderby file.Name ascending
wherefile.IsDirectory==true
selectnew
{
Name=file.Name,
Created=file.Created,
Last_modified=file.LastModified
},//Use an underscore in the property name to get a space in the title.
true,TableStyles.Medium11);
wsList.Cells[2,7,dt.Rows.Count+1,8].Style.Numberformat.Format="mm-dd-yy";
//Load the list using a specified array of MemberInfo objects. Properties, fields and methods are supported.
varrng=wsList.Cells["J1"].LoadFromCollection(list,
true,
TableStyles.Medium10,
BindingFlags.Instance|BindingFlags.Public,
newMemberInfo[]{
typeof(FileDTO).GetProperty("Name"),
typeof(FileDTO).GetField("IsDirectory"),
typeof(FileDTO).GetMethod("ToString")}
);
wsList.Tables.GetFromRange(rng).Columns[2].Name="Description";
wsList.Cells[wsList.Dimension.Address].AutoFitColumns();
//...and save
varfi=FileUtil.GetCleanFileInfo("04-LoadingData.xlsx");
pck.SaveAs(fi);
pck.Dispose();
}
privatestaticDataTableGetDataTable(DirectoryInfodir)
{
DataTabledt=newDataTable("RootDir");
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Size",typeof(long));
dt.Columns.Add("Created",typeof(DateTime));
dt.Columns.Add("Modified",typeof(DateTime));
foreach(varitemindir.GetDirectories())
{
varrow=dt.NewRow();
row["Name"]=item.Name;
row["Created"]=item.CreationTime;
row["Modified"]=item.LastWriteTime;
dt.Rows.Add(row);
}
foreach(varitemindir.GetFiles())
{
varrow=dt.NewRow();
row["Name"]=item.Name;
row["Size"]=item.Length;
row["Created"]=item.CreationTime;
row["Modified"]=item.LastWriteTime;
dt.Rows.Add(row);
}
returndt;
}
}
publicclassFileDTO
{
publicstringName{get;set;}
publiclongSize{get;set;}
publicDateTimeCreated{get;set;}
[Description("Last Modified")]
publicDateTimeLastModified{get;set;}
[Description("Is a Directory")]
publicboolIsDirectory=false;//This is a field variable
publicoverridestringToString()
{
if(IsDirectory)
{
returnName+"\t<Dir>";
}
else
{
returnName+"\t"+Size.ToString("#,##0");
}
}
}
}