Skip to content

Commit fa33849

Browse files
committed
EPPlus 5-Sample project for .NET Core
0 parents  commit fa33849

File tree

73 files changed

+5677
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+5677
-0
lines changed

.gitignore

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#OS junk files
2+
[Tt]humbs.db
3+
*.DS_Store
4+
5+
#Visual Studio files
6+
*.[Oo]bj
7+
*.user
8+
*.aps
9+
*.pch
10+
*.vspscc
11+
*.vssscc
12+
*_i.c
13+
*_p.c
14+
*.ncb
15+
*.suo
16+
*.tlb
17+
*.tlh
18+
*.bak
19+
*.[Cc]ache
20+
*.ilk
21+
*.log
22+
*.lib
23+
*.sbr
24+
*.sdf
25+
*.opensdf
26+
*.unsuccessfulbuild
27+
ipch/
28+
obj/
29+
[Bb]in
30+
[Dd]ebug*/
31+
[Rr]elease*/
32+
[Hh]elp
33+
[Tt]est[Rr]esults
34+
Ankh.NoLoad
35+
*.[Pp]ublish.xml
36+
.vs/
37+
.nuget/nuget.exe
38+
*.orig
39+
40+
#Tooling
41+
_ReSharper*/
42+
*.resharper
43+
[Tt]est[Rr]esult*
44+
45+
#Project files
46+
[Bb]uild/
47+
48+
#Subversion files
49+
.svn
50+
51+
# Office Temp Files
52+
~$*
53+
54+
#NuGet
55+
packages/
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*******************************************************************************
2+
* You may amend and distribute as you like, but don't remove this header!
3+
*
4+
* All rights reserved.
5+
*
6+
* EPPlus is an Open Source project provided under the
7+
* GNU General Public License (GPL) as published by the
8+
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9+
*
10+
* EPPlus provides server-side generation of Excel 2007 spreadsheets.
11+
* See https://github.com/JanKallman/EPPlus for details.
12+
*
13+
* The GNU General Public License can be viewed at http://www.opensource.org/licenses/gpl-license.php
14+
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
15+
*
16+
* The code for this project may be used and redistributed by any means PROVIDING it is
17+
* not sold for profit without the author's written consent, and providing that this notice
18+
* and the author's name and all copyright notices remain intact.
19+
*
20+
* All code and executables are provided "as is" with no warranty either express or implied.
21+
* The author accepts no liability for any damage or loss of business that this product may cause.
22+
*
23+
*
24+
* Code change notes:
25+
*
26+
* Author Change Date
27+
*******************************************************************************
28+
* Jan Källman Added 21 Mar 2010
29+
*******************************************************************************/
30+
using OfficeOpenXml;
31+
using System.Drawing;
32+
using OfficeOpenXml.Style;
33+
namespace EPPlusSamples
34+
{
35+
class GettingStartedSample
36+
{
37+
/// <summary>
38+
/// Sample 1 - Simply creates a new workbook from scratch.
39+
/// The workbook contains one worksheet with a simple invertory list
40+
/// Data is loaded manually via the Cells property of the Worksheet.
41+
/// </summary>
42+
public static string Run()
43+
{
44+
using (var package = new ExcelPackage())
45+
{
46+
//Add a new worksheet to the empty workbook
47+
var worksheet = package.Workbook.Worksheets.Add("Inventory");
48+
//Add the headers
49+
worksheet.Cells[1, 1].Value = "ID";
50+
worksheet.Cells[1, 2].Value = "Product";
51+
worksheet.Cells[1, 3].Value = "Quantity";
52+
worksheet.Cells[1, 4].Value = "Price";
53+
worksheet.Cells[1, 5].Value = "Value";
54+
55+
//Add some items...
56+
worksheet.Cells["A2"].Value = 12001;
57+
worksheet.Cells["B2"].Value = "Nails";
58+
worksheet.Cells["C2"].Value = 37;
59+
worksheet.Cells["D2"].Value = 3.99;
60+
61+
worksheet.Cells["A3"].Value = 12002;
62+
worksheet.Cells["B3"].Value = "Hammer";
63+
worksheet.Cells["C3"].Value = 5;
64+
worksheet.Cells["D3"].Value = 12.10;
65+
66+
worksheet.Cells["A4"].Value = 12003;
67+
worksheet.Cells["B4"].Value = "Saw";
68+
worksheet.Cells["C4"].Value = 12;
69+
worksheet.Cells["D4"].Value = 15.37;
70+
71+
//Add a formula for the value-column
72+
worksheet.Cells["E2:E4"].Formula = "C2*D2";
73+
74+
//Ok now format the values;
75+
using (var range = worksheet.Cells[1, 1, 1, 5])
76+
{
77+
range.Style.Font.Bold = true;
78+
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
79+
range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
80+
range.Style.Font.Color.SetColor(Color.White);
81+
}
82+
83+
worksheet.Cells["A5:E5"].Style.Border.Top.Style = ExcelBorderStyle.Thin;
84+
worksheet.Cells["A5:E5"].Style.Font.Bold = true;
85+
86+
worksheet.Cells[5, 3, 5, 5].Formula = string.Format("SUBTOTAL(9,{0})", new ExcelAddress(2,3,4,3).Address);
87+
worksheet.Cells["C2:C5"].Style.Numberformat.Format = "#,##0";
88+
worksheet.Cells["D2:E5"].Style.Numberformat.Format = "#,##0.00";
89+
90+
//Create an autofilter for the range
91+
worksheet.Cells["A1:E4"].AutoFilter = true;
92+
93+
worksheet.Cells["A2:A4"].Style.Numberformat.Format = "@"; //Format as text
94+
95+
//There is actually no need to calculate, Excel will do it for you, but in some cases it might be useful.
96+
//For example if you link to this workbook from another workbook or you will open the workbook in a program that hasn't a calculation engine or
97+
//you want to use the result of a formula in your program.
98+
worksheet.Calculate();
99+
100+
worksheet.Cells.AutoFitColumns(0); //Autofit columns for all cells
101+
102+
// Lets set the header text
103+
worksheet.HeaderFooter.OddHeader.CenteredText = "&24&U&\"Arial,Regular Bold\" Inventory";
104+
// Add the page number to the footer plus the total number of pages
105+
worksheet.HeaderFooter.OddFooter.RightAlignedText =
106+
string.Format("Page {0} of {1}", ExcelHeaderFooter.PageNumber, ExcelHeaderFooter.NumberOfPages);
107+
// Add the sheet name to the footer
108+
worksheet.HeaderFooter.OddFooter.CenteredText = ExcelHeaderFooter.SheetName;
109+
// Add the file path to the footer
110+
worksheet.HeaderFooter.OddFooter.LeftAlignedText = ExcelHeaderFooter.FilePath + ExcelHeaderFooter.FileName;
111+
112+
worksheet.PrinterSettings.RepeatRows = worksheet.Cells["1:2"];
113+
worksheet.PrinterSettings.RepeatColumns = worksheet.Cells["A:G"];
114+
115+
// Change the sheet view to show it in page layout mode
116+
worksheet.View.PageLayoutView = true;
117+
118+
// Set some document properties
119+
package.Workbook.Properties.Title = "Invertory";
120+
package.Workbook.Properties.Author = "Jan Källman";
121+
package.Workbook.Properties.Comments = "This sample demonstrates how to create an Excel workbook using EPPlus";
122+
123+
// Set some extended property values
124+
package.Workbook.Properties.Company = "EPPlus Software AB";
125+
126+
// Set some custom property values
127+
package.Workbook.Properties.SetCustomPropertyValue("Checked by", "Jan Källman");
128+
package.Workbook.Properties.SetCustomPropertyValue("AssemblyName", "EPPlus");
129+
130+
var xlFile = FileOutputUtil.GetFileInfo("01-GettingStarted.xlsx");
131+
132+
// Save our new workbook in the output directory and we are done!
133+
package.SaveAs(xlFile);
134+
return xlFile.FullName;
135+
}
136+
}
137+
}
138+
}

01-GettingStarted/Readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#01 - Getting started
2+
These samples demonstrates the formula calculation capabilities of EPPlus.
3+
4+
### [GettingStartedSample.cs](GettingStartedSample.cs)
5+
Creates a workbook from scratch. The workbook contains one worksheet with a simple invertory list.
6+
Data is loaded manually via the Cells property of the Worksheet.
7+
8+
---
9+
[Back to overview](/SampleApp.Core/Readme.md)

02-ReadWorkbook/ReadWorkbook.xlsx

4.47 KB
Binary file not shown.

02-ReadWorkbook/ReadWorkbookSample.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*******************************************************************************
2+
* You may amend and distribute as you like, but don't remove this header!
3+
*
4+
* All rights reserved.
5+
*
6+
* EPPlus is an Open Source project provided under the
7+
* GNU General Public License (GPL) as published by the
8+
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9+
*
10+
* EPPlus provides server-side generation of Excel 2007 spreadsheets.
11+
* See https://github.com/JanKallman/EPPlus for details.
12+
*
13+
*
14+
*
15+
* The GNU General Public License can be viewed at http://www.opensource.org/licenses/gpl-license.php
16+
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
17+
*
18+
* The code for this project may be used and redistributed by any means PROVIDING it is
19+
* not sold for profit without the author's written consent, and providing that this notice
20+
* and the author's name and all copyright notices remain intact.
21+
*
22+
* All code and executables are provided "as is" with no warranty either express or implied.
23+
* The author accepts no liability for any damage or loss of business that this product may cause.
24+
*
25+
*
26+
* Code change notes:
27+
*
28+
* Author Change Date
29+
*******************************************************************************
30+
* Jan Källman Added 10-SEP-2009
31+
*******************************************************************************/
32+
using System;
33+
using System.IO;
34+
using OfficeOpenXml;
35+
36+
namespace EPPlusSamples
37+
{
38+
/// <summary>
39+
/// Simply opens an existing file and reads some values and properties
40+
/// </summary>
41+
class ReadWorkbookSample
42+
{
43+
public static void Run()
44+
{
45+
var filePath = FileInputUtil.GetFileInfo("02-ReadWorkbook", "ReadWorkbook.xlsx").FullName;
46+
Console.WriteLine("Reading column 2 of {0}", filePath);
47+
Console.WriteLine();
48+
49+
FileInfo existingFile = new FileInfo(filePath);
50+
using (ExcelPackage package = new ExcelPackage(existingFile))
51+
{
52+
//Get the first worksheet in the workbook
53+
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
54+
55+
int col = 2; //Column 2 is the item description
56+
for (int row = 2; row < 5; row++)
57+
Console.WriteLine("\tCell({0},{1}).Value={2}", row, col, worksheet.Cells[row, col].Value);
58+
59+
//Output the formula from row 3 in A1 and R1C1 format
60+
Console.WriteLine("\tCell({0},{1}).Formula={2}", 3, 5, worksheet.Cells[3, 5].Formula);
61+
Console.WriteLine("\tCell({0},{1}).FormulaR1C1={2}", 3, 5, worksheet.Cells[3, 5].FormulaR1C1);
62+
63+
//Output the formula from row 5 in A1 and R1C1 format
64+
Console.WriteLine("\tCell({0},{1}).Formula={2}", 5, 3, worksheet.Cells[5, 3].Formula);
65+
Console.WriteLine("\tCell({0},{1}).FormulaR1C1={2}", 5, 3, worksheet.Cells[5, 3].FormulaR1C1);
66+
67+
} // the using statement automatically calls Dispose() which closes the package.
68+
69+
Console.WriteLine();
70+
Console.WriteLine("Read workbook sample complete");
71+
Console.WriteLine();
72+
}
73+
}
74+
}

02-ReadWorkbook/Readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#02 - Read workbook
2+
Read an existing workbook with EPPlus.
3+
4+
### [ReadWorkbookSample.cs](ReadWorkbookSample.cs)
5+
Creates a workbook from scratch. The workbook contains one worksheet with a simple invertory list.
6+
7+
---
8+
[Back to overview](/SampleApp.Core/Readme.md)

03-UsingAsyncAwait/Importfile.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ID Product Items In Stock Purchase Price Price
2+
12001 Nails 37 1,3 3,99
3+
12002 Hammer 5 5,33 12,10
4+
12003 Saw 12 8,99 15,37
5+
12010 Drill 20 4,3 8,00
6+
12011 Crowbar 7 13,77 23,48

03-UsingAsyncAwait/Readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#03 - Using async / await to load and save data
2+
This sample demonstrates how to load and save data using the async await pattern.
3+
4+
### [UsingAsyncAwait.cs](UsingAsyncAwaitSample.cs)
5+
6+
---
7+
[Back to overview](/SampleApp.Core/Readme.md)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************************
2+
* You may amend and distribute as you like, but don't remove this header!
3+
*
4+
* All rights reserved.
5+
*
6+
* EPPlus is an Open Source project provided under the
7+
* GNU General Public License (GPL) as published by the
8+
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9+
*
10+
* EPPlus provides server-side generation of Excel 2007 spreadsheets.
11+
* See https://github.com/JanKallman/EPPlus for details.
12+
*
13+
*
14+
*
15+
* The GNU General Public License can be viewed at http://www.opensource.org/licenses/gpl-license.php
16+
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
17+
*
18+
* The code for this project may be used and redistributed by any means PROVIDING it is
19+
* not sold for profit without the author's written consent, and providing that this notice
20+
* and the author's name and all copyright notices remain intact.
21+
*
22+
* All code and executables are provided "as is" with no warranty either express or implied.
23+
* The author accepts no liability for any damage or loss of business that this product may cause.
24+
*
25+
*
26+
* Code change notes:
27+
*
28+
* Author Change Date
29+
*******************************************************************************
30+
* Jan Källman Added 10-SEP-2009
31+
*******************************************************************************/
32+
33+
using System;
34+
using OfficeOpenXml;
35+
using System.Data.SqlClient;
36+
using System.Drawing;
37+
using OfficeOpenXml.Style;
38+
using System.Data.SQLite;
39+
using System.Threading.Tasks;
40+
using System.Threading;
41+
using OfficeOpenXml.Table;
42+
43+
namespace EPPlusSamples.SalesReport
44+
{
45+
class UsingAsyncAwaitSample
46+
{
47+
/// <summary>
48+
/// Shows a few different ways to load / save asynchronous
49+
/// </summary>
50+
/// <param name="connectionString">The connection string to the SQLite database</param>
51+
public static async Task RunAsync(string connectionString)
52+
{
53+
var file = FileOutputUtil.GetFileInfo("03-AsyncAwait.xlsx");
54+
using (ExcelPackage package = new ExcelPackage(file))
55+
{
56+
var ws = package.Workbook.Worksheets.Add("Sheet1");
57+
58+
using (var sqlConn = new SQLiteConnection(connectionString))
59+
{
60+
sqlConn.Open();
61+
using (var sqlCmd = new SQLiteCommand("select CompanyName, [Name], Email, Country, o.OrderId, orderdate, ordervalue, currency from Customer c inner join Orders o on c.CustomerId=o.CustomerId inner join SalesPerson s on o.salesPersonId = s.salesPersonId ORDER BY 1,2 desc", sqlConn))
62+
{
63+
var range = await ws.Cells["B2"].LoadFromDataReaderAsync(sqlCmd.ExecuteReader(), true, "Table1", TableStyles.Medium10);
64+
range.AutoFitColumns();
65+
}
66+
}
67+
68+
await package.SaveAsync();
69+
}
70+
71+
//Load the package async again.
72+
using (var package = new ExcelPackage())
73+
{
74+
await package.LoadAsync(file);
75+
76+
var newWs = package.Workbook.Worksheets.Add("AddedSheet2");
77+
var range = await newWs.Cells["A1"].LoadFromTextAsync(FileInputUtil.GetFileInfo("03-UsingAsyncAwait", "Importfile.txt"), new ExcelTextFormat { Delimiter='\t' });
78+
range.AutoFitColumns();
79+
80+
await package.SaveAsAsync(FileOutputUtil.GetFileInfo("03-AsyncAwait-LoadedAndModified.xlsx"));
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)