This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Sample9.cs
195 lines (170 loc) · 8.87 KB
/
Sample9.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*******************************************************************************
* You may amend and distribute as you like, but don't remove this header!
*
* 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
*
* EPPlus provides server-side generation of Excel 2007 spreadsheets.
* See https://github.com/JanKallman/EPPlus for details.
*
*
*
* 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 28 Oct 2010
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OfficeOpenXml;
using System.IO;
using OfficeOpenXml.Table;
using OfficeOpenXml.Drawing.Chart;
using System.Globalization;
namespace EPPlusSamples
{
/// <summary>
/// This sample shows how to load CSV files using the LoadFromText method, how to use tables and
/// how to use charts with more than one charttype and secondary axis
/// </summary>
public static class Sample9
{
/// <summary>
/// Loads two CSV files into tables and adds a chart to each sheet.
/// </summary>
/// <param name="outputDir"></param>
/// <returns></returns>
public static string RunSample9()
{
FileInfo newFile = Utils.GetFileInfo(@"\sample9.xlsx");
using (ExcelPackage package = new ExcelPackage())
{
LoadFile1(package);
LoadFile2(package);
package.SaveAs(newFile);
}
return newFile.FullName;
}
private static void LoadFile1(ExcelPackage package)
{
//Create the Worksheet
var sheet = package.Workbook.Worksheets.Add("Csv1");
//Create the format object to describe the text file
var format = new ExcelTextFormat();
format.TextQualifier = '"';
format.SkipLinesBeginning = 2;
format.SkipLinesEnd = 1;
var csvDir= new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "csv");
//Now read the file into the sheet. Start from cell A1. Create a table with style 27. First row contains the header.
Console.WriteLine("Load the text file...");
var range = sheet.Cells["A1"].LoadFromText(Utils.GetFileInfo(csvDir,"Sample9-1.txt",false), format, TableStyles.Medium27, true);
Console.WriteLine("Format the table...");
//Tables don't support custom styling at this stage(you can of course format the cells), but we can create a Namedstyle for a column...
var dateStyle = package.Workbook.Styles.CreateNamedStyle("TableDate");
dateStyle.Style.Numberformat.Format = "YYYY-MM";
var numStyle = package.Workbook.Styles.CreateNamedStyle("TableNumber");
numStyle.Style.Numberformat.Format = "#,##0.0";
//Now format the table...
var tbl = sheet.Tables[0];
tbl.ShowTotal = true;
tbl.Columns[0].TotalsRowLabel = "Total";
tbl.Columns[0].DataCellStyleName = "TableDate";
tbl.Columns[1].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[1].DataCellStyleName = "TableNumber";
tbl.Columns[2].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[2].DataCellStyleName = "TableNumber";
tbl.Columns[3].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[3].DataCellStyleName = "TableNumber";
tbl.Columns[4].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[4].DataCellStyleName = "TableNumber";
tbl.Columns[5].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[5].DataCellStyleName = "TableNumber";
tbl.Columns[6].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[6].DataCellStyleName = "TableNumber";
Console.WriteLine("Create the chart...");
//Now add a stacked areachart...
var chart = sheet.Drawings.AddChart("chart1", eChartType.AreaStacked);
chart.SetPosition(0, 630);
chart.SetSize(800, 600);
//Create one series for each column...
for (int col = 1; col < 7; col++)
{
var ser = chart.Series.Add(range.Offset(1, col, range.End.Row - 1, 1), range.Offset(1, 0, range.End.Row - 1, 1));
ser.HeaderAddress = range.Offset(0, col, 1, 1);
}
//Set the style to 27.
chart.Style = eChartStyle.Style27;
sheet.View.ShowGridLines = false;
sheet.Calculate();
sheet.Cells[sheet.Dimension.Address].AutoFitColumns();
}
private static void LoadFile2(ExcelPackage package)
{
//Create the Worksheet
var sheet = package.Workbook.Worksheets.Add("Csv2");
//Create the format object to describe the text file
var format = new ExcelTextFormat();
format.Delimiter='\t'; //Tab
format.SkipLinesBeginning = 1;
CultureInfo ci = new CultureInfo("sv-SE"); //Use your choice of Culture
ci.NumberFormat.NumberDecimalSeparator = ","; //Decimal is comma
format.Culture = ci;
//Now read the file into the sheet.
Console.WriteLine("Load the text file...");
var csvDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "csv");
var range = sheet.Cells["A1"].LoadFromText(Utils.GetFileInfo(csvDir, "Sample9-2.txt", false), format);
//Add a formula
range.Offset(1, range.End.Column, range.End.Row - range.Start.Row, 1).FormulaR1C1 = "RC[-1]-RC[-2]";
//Add a table...
var tbl = sheet.Tables.Add(range.Offset(0,0,range.End.Row-range.Start.Row+1, range.End.Column-range.Start.Column+2),"Table");
tbl.ShowTotal = true;
tbl.Columns[0].TotalsRowLabel = "Total";
tbl.Columns[1].TotalsRowFormula = "COUNT(3,Table[Product])"; //Add a custom formula
tbl.Columns[2].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[3].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[4].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[5].TotalsRowFunction = RowFunctions.Sum;
tbl.Columns[5].Name = "Profit";
tbl.TableStyle = TableStyles.Medium10;
sheet.Cells[sheet.Dimension.Address].AutoFitColumns();
//Add a chart with two charttypes (Column and Line) and a secondary axis...
var chart = sheet.Drawings.AddChart("chart2", eChartType.ColumnStacked);
chart.SetPosition(0, 540);
chart.SetSize(800, 600);
var serie1= chart.Series.Add(range.Offset(1, 3, range.End.Row - 1, 1), range.Offset(1, 1, range.End.Row - 1, 1));
serie1.Header = "Purchase Price";
var serie2 = chart.Series.Add(range.Offset(1, 5, range.End.Row - 1, 1), range.Offset(1, 1, range.End.Row - 1, 1));
serie2.Header = "Profit";
//Add a Line series
var chartType2 = chart.PlotArea.ChartTypes.Add(eChartType.LineStacked);
chartType2.UseSecondaryAxis = true;
var serie3 = chartType2.Series.Add(range.Offset(1, 2, range.End.Row - 1, 1), range.Offset(1, 0, range.End.Row - 1, 1));
serie3.Header = "Items in stock";
//By default the secondary XAxis is not visible, but we want to show it...
chartType2.XAxis.Deleted = false;
chartType2.XAxis.TickLabelPosition = eTickLabelPosition.High;
//Set the max value for the Y axis...
chartType2.YAxis.MaxValue = 50;
chart.Style = eChartStyle.Style26;
sheet.View.ShowGridLines = false;
sheet.Calculate();
}
}
}