forked from JanKallman/EPPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample13.cs
176 lines (156 loc) · 8.04 KB
/
Sample13.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
/*
* You may amend and distribute as you like, but don't remove this header!
*
* EPPlus provides server-side generation of Excel 2007 spreadsheets.
* See https://github.com/JanKallman/EPPlus for details.
*
* All rights reserved.
*
* EPPlus is an Open Source project provided under the
* GNU General Public License (GPL) as published by the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The GNU General Public License can be viewed at http://www.opensource.org/licenses/gpl-license.php
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
*
* The code for this project may be used and redistributed by any means PROVIDING it is
* not sold for profit without the author's written consent, and providing that this notice
* and the author's name and all copyright notices remain intact.
*
* All code and executables are provided "as is" with no warranty either express or implied.
* The author accepts no liability for any damage or loss of business that this product may cause.
*
* Code change notes:
*
* Author Change Date
* ******************************************************************************
* Jan Källman Added 2011-05-03
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OfficeOpenXml;
using System.Data;
using OfficeOpenXml.Table;
using System.Reflection;
namespace EPPlusSamples
{
/// <summary>
/// This class shows how to load data in a few ways
/// </summary>
public static class Sample13
{
public class FileDTO
{
public string Name { get; set; }
public long Size { get; set; }
public DateTime Created { get; set; }
public DateTime LastModified { get; set; }
public bool IsDirectory = false; //This is a field variable
public override string ToString()
{
if (IsDirectory)
{
return Name + "\t<Dir>";
}
else
{
return Name + "\t" + Size.ToString("#,##0");
}
}
}
public static void RunSample13()
{
var pck = new ExcelPackage();
//Create a datatable with the directories and files from the root directory...
DataTable dt = GetDataTable(Utils.GetDirectoryInfo("."));
var wsDt = 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...
var collection = (from row in dt.Select() select new { Name = row["Name"], Created_time = (DateTime)row["Created"] });
var wsEnum = 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...
var wsList = pck.Workbook.Worksheets.Add("FromList");
List<FileDTO> list = (from row in dt.Select()
select new FileDTO
{
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(from file in list
orderby file.Size descending
where file.IsDirectory == false
select file, 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(from file in list
orderby file.Name ascending
where file.IsDirectory == true
select new
{
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.
var rng = wsList.Cells["J1"].LoadFromCollection(list,
true,
TableStyles.Medium10,
BindingFlags.Instance | BindingFlags.Public,
new MemberInfo[] {
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
var fi = Utils.GetFileInfo("sample13.xlsx");
pck.SaveAs(fi);
}
private static DataTable GetDataTable(DirectoryInfo dir)
{
DataTable dt = new DataTable("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 (var item in dir.GetDirectories())
{
var row = dt.NewRow();
row["Name"] = item.Name;
row["Created"] = item.CreationTime;
row["Modified"] = item.LastWriteTime;
dt.Rows.Add(row);
}
foreach (var item in dir.GetFiles())
{
var row = dt.NewRow();
row["Name"] = item.Name;
row["Size"] = item.Length;
row["Created"] = item.CreationTime;
row["Modified"] = item.LastWriteTime;
dt.Rows.Add(row);
}
return dt;
}
}
}