Skip to content

Commit

Permalink
Started Work on samples for EPPlus 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKallman committed Jun 9, 2020
1 parent b808704 commit 27b67ab
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 97 deletions.
2 changes: 1 addition & 1 deletion 03-UsingAsyncAwait/UsingAsyncAwaitSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static async Task RunAsync(string connectionString)
using (var sqlConn = new SQLiteConnection(connectionString))
{
sqlConn.Open();
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))
using (var sqlCmd = new SQLiteCommand("select CompanyName, [Name], Email, c.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))
{
var range = await ws.Cells["B2"].LoadFromDataReaderAsync(sqlCmd.ExecuteReader(), true, "Table1", TableStyles.Medium10);
range.AutoFitColumns();
Expand Down
2 changes: 1 addition & 1 deletion 08-SalesReport/SalesReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static string Run(string connectionString)
using (var sqlConn = new SQLiteConnection(connectionString))
{
sqlConn.Open();
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))
using (var sqlCmd = new SQLiteCommand("select CompanyName, [Name], Email, c.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))
{
using (var sqlReader = sqlCmd.ExecuteReader())
{
Expand Down
2 changes: 1 addition & 1 deletion 13-Filter/FilterSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static async Task<ExcelRangeBase> LoadFromDatabase(string connectionStri
using (var sqlConn = new SQLiteConnection(connectionString))
{
sqlConn.Open();
using (var sqlCmd = new SQLiteCommand("select companyName as CompanyName, [name] as Name, email as Email, country as Country, o.OrderId as OrderId, orderdate as OrderDate, ordervalue as OrderValue, currency 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))
using (var sqlCmd = new SQLiteCommand("select companyName as CompanyName, [name] as Name, email as Email, c.country as Country, o.OrderId as OrderId, orderdate as OrderDate, ordervalue as OrderValue, currency 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))
{
using (var sqlReader = sqlCmd.ExecuteReader())
{
Expand Down
174 changes: 173 additions & 1 deletion 15-ChartsAndThemes/ChartsAndThemes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Date Author Change
using OfficeOpenXml.Table;
using System.Data;
using System.IO;
using OfficeOpenXml.Drawing.Chart.ChartEx;

namespace EPPlusSamples
{
Expand Down Expand Up @@ -78,7 +79,16 @@ public static async Task<string> RunAsync(string connectionString, FileInfo xlFi

//Adds a radar chart
AddRadarChart(package);


//Adds a sunburst and a treemap chart
await AddSunburstAndTreemapChart(connectionString, package);

//Add a box & whisker and a histogram chart
AddBoxWhiskerAndParetoHistogramChart(package);

// Add a waterfall chart
AddWaterfallChart(package);

//Add an area chart using a chart template (chrx file)
await AddAreaFromChartTemplate(connectionString, package);

Expand All @@ -88,6 +98,148 @@ public static async Task<string> RunAsync(string connectionString, FileInfo xlFi
}
}

private static async Task AddSunburstAndTreemapChart(string connectionString, ExcelPackage package)
{
var ws = package.Workbook.Worksheets.Add("Sunburst & Treemap Chart");
var range = await LoadSalesFromDatabase(connectionString, ws);

var sunburstChart = ws.Drawings.AddSunburstChart("SunburstChart1");
var sbSerie = sunburstChart.Series.Add(ws.Cells[2, 1, range.Rows, 3], ws.Cells[2, 4, range.Rows, 4]);
sbSerie.HeaderAddress = ws.Cells["D1"];
sunburstChart.SetPosition(1, 0, 6, 0);
sunburstChart.SetSize(800, 800);
sunburstChart.Title.Text = "Sales";
sunburstChart.Legend.Add();
sunburstChart.Legend.Position = eLegendPosition.Bottom;
sbSerie.DataLabel.Add(true, true);
sunburstChart.StyleManager.SetChartStyle(ePresetChartStyle.SunburstChartStyle4);


var treemapChart = ws.Drawings.AddTreemapChart("TreemapChart1");
var tmSerie = treemapChart.Series.Add(ws.Cells[2, 1, range.Rows, 3], ws.Cells[2, 4, range.Rows, 4]);
treemapChart.Title.Font.Fill.Style = eFillStyle.SolidFill;
treemapChart.Title.Font.Fill.SolidFill.Color.SetSchemeColor(eSchemeColor.Background2);
tmSerie.HeaderAddress = ws.Cells["D1"];
treemapChart.SetPosition(1, 0, 19, 0);
treemapChart.SetSize(1000, 800);
treemapChart.Title.Text = "Sales";
treemapChart.Legend.Add();
treemapChart.Legend.Position = eLegendPosition.Right;
tmSerie.DataLabel.Add(true, true);
tmSerie.ParentLabelLayout = eParentLabelLayout.Banner;
treemapChart.StyleManager.SetChartStyle(ePresetChartStyle.TreemapChartStyle6);
}
private static void AddBoxWhiskerAndParetoHistogramChart(ExcelPackage package)
{
var ws = package.Workbook.Worksheets.Add("BoxAndWhiskerChart");
AddBoxWhiskerData(ws);

var boxWhiskerChart = ws.Drawings.AddBoxWhiskerChart("BoxAndWhisker1");
var bwSerie1 = boxWhiskerChart.Series.Add(ws.Cells[2, 1, 11, 1], null);
bwSerie1.HeaderAddress = ws.Cells["A1"];
var bwSerie2 = boxWhiskerChart.Series.Add(ws.Cells[2, 2, 11, 2], null);
bwSerie2.HeaderAddress = ws.Cells["B1"];
var bwSerie3 = boxWhiskerChart.Series.Add(ws.Cells[2, 3, 11, 3], null);
bwSerie3.HeaderAddress = ws.Cells["C1"];
boxWhiskerChart.SetPosition(1, 0, 6, 0);
boxWhiskerChart.SetSize(800, 800);
boxWhiskerChart.Title.Text = "Number series";
boxWhiskerChart.StyleManager.SetChartStyle(ePresetChartStyle.BoxWhiskerChartStyle4);

var histogramChart = ws.Drawings.AddHistogramChart("Pareto", true);
histogramChart.SetPosition(1, 0, 19, 0);
histogramChart.SetSize(800, 800);
var hgSerie = histogramChart.Series.Add(ws.Cells[2, 3, 15, 3], null);
hgSerie.HeaderAddress = ws.Cells["C1"];
hgSerie.Binning.Size = 4;
}

private static void AddWaterfallChart(ExcelPackage package)
{
var ws = package.Workbook.Worksheets.Add("WaterfallChart");

ws.SetValue("A1", "Description");
ws.SetValue("A2", "Initial Saldo");
ws.SetValue("A3", "Food");
ws.SetValue("A4", "Beer");
ws.SetValue("A5", "Transfer");
ws.SetValue("A6", "Electrical Bill");
ws.SetValue("A7", "Cell Phone");
ws.SetValue("A8", "Car Repair");

ws.SetValue("B1", "Saldo/transaction");
ws.SetValue("B2", 1000);
ws.SetValue("B3", -237.5);
ws.SetValue("B4", -33.75);
ws.SetValue("B5", 200);
ws.SetValue("B6", -153.4);
ws.SetValue("B7", -49);
ws.SetValue("B8", -258.47);
ws.Cells["B9"].Formula="SUM(B2:B8)";
ws.Calculate();

var waterfallChart = ws.Drawings.AddWaterfallChart("Waterfall1");
waterfallChart.SetPosition(1, 0, 6, 0);
waterfallChart.SetSize(800, 800);
var wfSerie1 = waterfallChart.Series.Add(ws.Cells[2, 2, 9, 2], ws.Cells[2, 1, 8, 1]);

var dp=wfSerie1.DataPoints.Add(0);
dp.SubTotal = true;
dp = wfSerie1.DataPoints.Add(7);
dp.SubTotal = true;
}
private static void AddBoxWhiskerData(ExcelWorksheet ws)
{
ws.Cells["A1"].Value = "Primes";
ws.Cells["A2"].Value = 2;
ws.Cells["A3"].Value = 3;
ws.Cells["A4"].Value = 5;
ws.Cells["A5"].Value = 7;
ws.Cells["A6"].Value = 11;
ws.Cells["A7"].Value = 13;
ws.Cells["A8"].Value = 17;
ws.Cells["A9"].Value = 19;
ws.Cells["A10"].Value = 23;
ws.Cells["A11"].Value = 29;
ws.Cells["A12"].Value = 31;
ws.Cells["A13"].Value = 37;
ws.Cells["A14"].Value = 41;
ws.Cells["A15"].Value = 43;

ws.Cells["B1"].Value = "Even";
ws.Cells["B2"].Value = 2;
ws.Cells["B3"].Value = 4;
ws.Cells["B4"].Value = 6;
ws.Cells["B5"].Value = 8;
ws.Cells["B6"].Value = 10;
ws.Cells["B7"].Value = 12;
ws.Cells["B8"].Value = 14;
ws.Cells["B9"].Value = 16;
ws.Cells["B10"].Value = 18;
ws.Cells["B11"].Value = 20;
ws.Cells["B12"].Value = 22;
ws.Cells["B13"].Value = 24;
ws.Cells["B14"].Value = 26;
ws.Cells["B15"].Value = 28;


ws.Cells["C1"].Value = "Random";
ws.Cells["C2"].Value = 2;
ws.Cells["C3"].Value = 3;
ws.Cells["C4"].Value = 7;
ws.Cells["C5"].Value = 12;
ws.Cells["C6"].Value = 15;
ws.Cells["C7"].Value = 18;
ws.Cells["C8"].Value = 19;
ws.Cells["C9"].Value = 23;
ws.Cells["C10"].Value = 25;
ws.Cells["C11"].Value = 30;
ws.Cells["C12"].Value = 35;
ws.Cells["C13"].Value = 37;
ws.Cells["C14"].Value = 40;
ws.Cells["C15"].Value = 42;
}

private static async Task AddAreaFromChartTemplate(string connectionString, ExcelPackage package)
{
var ws = package.Workbook.Worksheets.Add("Area chart from template");
Expand Down Expand Up @@ -334,6 +486,26 @@ private static async Task<ExcelRangeBase> LoadFromDatabase(string connectionStri
}
return range;
}
private static async Task<ExcelRangeBase> LoadSalesFromDatabase(string connectionString, ExcelWorksheet ws)
{
ExcelRangeBase range;
using (var sqlConn = new SQLiteConnection(connectionString))
{
sqlConn.Open();
using (var sqlCmd = new SQLiteCommand("select s.continent, s.country, s.city, SUM(OrderValue) As Sales from Customer c inner join Orders o on c.CustomerId=o.CustomerId inner join SalesPerson s on o.salesPersonId = s.salesPersonId Where Currency='USD' group by s.continent, s.country, s.city ORDER BY s.continent, s.country, s.city", sqlConn))
{
using (var sqlReader = sqlCmd.ExecuteReader())
{
range = await ws.Cells["A1"].LoadFromDataReaderAsync(sqlReader, true);
range.Offset(0, 0, 1, range.Columns).Style.Font.Bold = true;
range.Offset(0, 3, range.Rows, 3).Style.Numberformat.Format = "#,##0";
}
//Set the numberformat
}
}
return range;
}

private static void CreateIceCreamData(ExcelWorksheet ws)
{
ws.SetValue("A1", "Icecream Sales-2019");
Expand Down
2 changes: 1 addition & 1 deletion 18-PivotTables/PivotTablesSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static List<SalesDTO> GetDataFromSQL(string connectionStr)
using (var sqlConn = new SQLiteConnection(connectionStr))
{
sqlConn.Open();
using (var sqlCmd = new SQLiteCommand("select companyName as CompanyName, [name] as Name, email as Email, country as Country, o.OrderId as OrderId, orderdate as OrderDate, ordervalue as OrderValue, tax as Tax, freight as Freight, currency Currency from Customer c inner join Orders o on c.CustomerId=o.CustomerId inner join SalesPerson s on o.salesPersonId = s.salesPersonId ORDER BY OrderDate, OrderValue desc", sqlConn))
using (var sqlCmd = new SQLiteCommand("select companyName as CompanyName, [name] as Name, email as Email, c.country as Country, o.OrderId as OrderId, orderdate as OrderDate, ordervalue as OrderValue, tax as Tax, freight as Freight, currency Currency from Customer c inner join Orders o on c.CustomerId=o.CustomerId inner join SalesPerson s on o.salesPersonId = s.salesPersonId ORDER BY OrderDate, OrderValue desc", sqlConn))
{
using (var sqlReader = sqlCmd.ExecuteReader())
{
Expand Down
Binary file modified EPPlusSample.sqlite
Binary file not shown.
5 changes: 4 additions & 1 deletion EPPlusSampleApp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EPPlus" Version="5.1.1" />
<PackageReference Include="Faker.Net" Version="1.1.1" />
<PackageReference Include="System.Data.Common" Version="4.3.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
Expand All @@ -32,6 +31,10 @@
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EPPlus\src\EPPlus\EPPlus.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
6 changes: 6 additions & 0 deletions EPPlusSampleApp.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EPPlusSampleApp.Core", "EPPlusSampleApp.Core.csproj", "{68677B2E-EA58-4EA5-88CC-479E0B2B286C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EPPlus", "..\EPPlus\src\EPPlus\EPPlus.csproj", "{65DAE380-0E7C-43F0-8F7E-6662DF511985}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{68677B2E-EA58-4EA5-88CC-479E0B2B286C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68677B2E-EA58-4EA5-88CC-479E0B2B286C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68677B2E-EA58-4EA5-88CC-479E0B2B286C}.Release|Any CPU.Build.0 = Release|Any CPU
{65DAE380-0E7C-43F0-8F7E-6662DF511985}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65DAE380-0E7C-43F0-8F7E-6662DF511985}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65DAE380-0E7C-43F0-8F7E-6662DF511985}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65DAE380-0E7C-43F0-8F7E-6662DF511985}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit 27b67ab

Please sign in to comment.