Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
EPPlus/SampleApp.Core/Sample13.cs at master · Zadjil-AR/EPPlus · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Zadjil-AR
EPPlus
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
EPPlus
/
SampleApp.Core
/
Sample13.cs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
176 lines (156 loc) · 7.87 KB
master
Breadcrumbs
EPPlus
/
SampleApp.Core
/
Sample13.cs
Copy path
Top
File metadata and controls
Code
Blame
176 lines (156 loc) · 7.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
;
}
}
}
You can’t perform that action at this time.