Skip to content

Commit 8986acd

Browse files
author
JanKallman
committed
Fixed a few merge problems.
Added web sample 4 Removed DxfId property
1 parent f9c5eca commit 8986acd

13 files changed

+132
-34
lines changed

EPPlus/ConditionalFormatting/Contracts/IExcelConditionalFormattingRule.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public interface IExcelConditionalFormattingRule
7878
/// <para>The possible values for this attribute are defined by the ST_DxfId simple type
7979
/// (§18.18.25).</para>
8080
/// </summary>
81-
int DxfId { get; set; }
81+
// int DxfId { get; set; }
8282
/// <summary>
8383
/// Gives access to the differencial styling (DXF) for the rule.
8484
/// </summary>

EPPlus/ConditionalFormatting/ExcelConditionalFormattingCollection.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public IExcelConditionalFormattingRule RulesByPriority(
377377
/// </summary>
378378
/// <param name="type"></param>
379379
/// <param name="address"></param>
380-
/// <returns></returns>
380+
/// <returns></returns>F
381381
internal IExcelConditionalFormattingRule AddRule(
382382
eExcelConditionalFormattingRuleType type,
383383
ExcelAddress address)

EPPlus/ConditionalFormatting/Rules/ExcelConditionalFormattingRule.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public bool StopIfTrue
332332
/// <summary>
333333
/// DxfId Style Attribute
334334
/// </summary>
335-
public int DxfId
335+
internal int DxfId
336336
{
337337
get
338338
{

EPPlus/ExcelStyles.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
using OfficeOpenXml.Style;
3737
using OfficeOpenXml.Style.XmlAccess;
3838
using OfficeOpenXml.Style.Dxf;
39+
using OfficeOpenXml.ConditionalFormatting;
3940
namespace OfficeOpenXml
4041
{
4142
/// <summary>
@@ -608,15 +609,15 @@ public void UpdateXml()
608609
int ix = Dxfs.FindIndexByID(cf.Style.Id);
609610
if (ix < 0)
610611
{
611-
cf.DxfId = Dxfs.Count;
612+
((ExcelConditionalFormattingRule)cf).DxfId = Dxfs.Count;
612613
Dxfs.Add(cf.Style.Id, cf.Style);
613614
var elem = ((XmlDocument)TopNode).CreateElement("d", "dxf", ExcelPackage.schemaMain);
614615
cf.Style.CreateNodes(new XmlHelperInstance(NameSpaceManager, elem), "");
615616
dxfsNode.AppendChild(elem);
616617
}
617618
else
618619
{
619-
cf.DxfId = ix;
620+
((ExcelConditionalFormattingRule)cf).DxfId = ix;
620621
}
621622
}
622623
}

EPPlus/ExcelWorksheet.cs

+18
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
using OfficeOpenXml.Table.PivotTable;
5252
using System.ComponentModel;
5353
using System.Drawing;
54+
using OfficeOpenXml.ConditionalFormatting;
5455

5556
namespace OfficeOpenXml
5657
{
@@ -2637,6 +2638,23 @@ public ExcelPivotTableCollection PivotTables
26372638
return _pivotTables;
26382639
}
26392640
}
2641+
private ExcelConditionalFormattingCollection _conditionalFormatting = null;
2642+
/// <summary>
2643+
/// ConditionalFormatting defined in the worksheet. Use the Add methods to create ConditionalFormatting and add them to the worksheet. Then
2644+
/// set the properties on the instance returned.
2645+
/// </summary>
2646+
/// <seealso cref="ExcelConditionalFormattingCollection"/>
2647+
public ExcelConditionalFormattingCollection ConditionalFormatting
2648+
{
2649+
get
2650+
{
2651+
if (_conditionalFormatting == null)
2652+
{
2653+
_conditionalFormatting = new ExcelConditionalFormattingCollection(this);
2654+
}
2655+
return _conditionalFormatting;
2656+
}
2657+
}
26402658
private ExcelDataValidationCollection _dataValidation = null;
26412659
/// <summary>
26422660
/// DataValidation defined in the worksheet. Use the Add methods to create DataValidations and add them to the worksheet. Then

EPPlus/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@
6060
//
6161
// You can specify all the values or you can default the Revision and Build Numbers
6262
// by using the '*' as shown below:
63-
[assembly: AssemblyVersion("3.0.0.2")]
64-
[assembly: AssemblyFileVersion("3.0.0.2")]
63+
[assembly: AssemblyVersion("3.1.0.0")]
64+
[assembly: AssemblyFileVersion("3.1.0.0")]
6565
[assembly: AllowPartiallyTrustedCallers]

EPPlus/XmlHelper.cs

-7
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,6 @@ internal int GetXmlNodeInt(string path)
682682
return null;
683683
}
684684
}
685-
}
686-
else
687-
{
688-
return null;
689-
}
690-
}
691-
}
692685

693686
internal decimal GetXmlNodeDecimal(string path)
694687
{

EPPlusWebSample/Default.aspx

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ A:hover{color:#CC6699}
5454
This sample demonstrates how to use a template stored in the Application cashe.
5555
</td>
5656
</tr>
57+
<tr>
58+
<td>
59+
<asp:HyperLink ID="sample4" runat="server" NavigateUrl="~/GetSample.aspx?Sample=4">Sample 4</asp:HyperLink>
60+
</td>
61+
<td>
62+
This sample demonstrates how to use a macro-enabled spreadsheet.
63+
</td>
64+
</tr>
5765
</table>
5866
</form>
5967
</body>

EPPlusWebSample/Default.aspx.designer.cs

+10-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EPPlusWebSample/GetSample.aspx.cs

+29
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
using System.IO;
3535
using OfficeOpenXml.Style;
3636
using System.Drawing;
37+
using System.Text;
3738
namespace EPPlusWebSample
3839
{
3940
public partial class GetSample : System.Web.UI.Page
@@ -51,6 +52,9 @@ protected void Page_Load(object sender, EventArgs e)
5152
case "3":
5253
Sample3();
5354
break;
55+
case "4":
56+
Sample4();
57+
break;
5458
default:
5559
Response.Write("<script>javascript:alert('Invalid querystring');</script>");
5660
break;
@@ -135,5 +139,30 @@ private void Sample3()
135139
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
136140
Response.AddHeader("content-disposition", "attachment; filename=Sample3.xlsx");
137141
}
142+
private void Sample4()
143+
{
144+
ExcelPackage pck = new ExcelPackage();
145+
146+
//Add a worksheet.
147+
var ws=pck.Workbook.Worksheets.Add("VBA Sample");
148+
ws.Drawings.AddShape("VBASampleRect", eShapeStyle.RoundRect);
149+
150+
//Create a vba project
151+
pck.Workbook.CreateVBAProject();
152+
153+
//Now add some code that creates a bubble chart...
154+
var sb = new StringBuilder();
155+
156+
sb.AppendLine("Private Sub Workbook_Open()");
157+
sb.AppendLine(" [VBA Sample].Shapes(\"VBASampleRect\").TextEffect.Text = \"This text is set from VBA!\"");
158+
sb.AppendLine("End Sub");
159+
pck.Workbook.CodeModule.Code = sb.ToString();
160+
161+
Response.BinaryWrite(pck.GetAsByteArray());
162+
Response.ContentType = "application/vnd.ms-excel.sheet.macroEnabled.12"; //.xlsm files uses a different contenttype than .xlsx
163+
Response.AddHeader("content-disposition", "attachment; filename=Sample4.xlsm");
164+
165+
}
166+
138167
}
139168
}

SampleApp/Sample14.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public static string RunSample14(DirectoryInfo outputDir)
8888
cfRule1.LowValue.Color = ColorTranslator.FromHtml("#FFFFEB84");
8989
cfRule1.HighValue.Type = eExcelConditionalFormattingValueObjectType.Formula;
9090
cfRule1.HighValue.Formula = "IF($G$1=\"A</x:&'cfRule>\",1,5)";
91-
cfRule1.DxfId = 34;
9291
cfRule1.StopIfTrue = true;
92+
cfRule1.Style.Font.Bold = true;
9393

9494
// But others you can't (readonly)
9595
// cfRule1.Type = eExcelConditionalFormattingRuleType.ThreeColorScale;
@@ -160,8 +160,7 @@ public static string RunSample14(DirectoryInfo outputDir)
160160
// -------------------------------------------------------------------
161161
var cfRule9 = worksheet.ConditionalFormatting.AddAboveStdDev(
162162
new ExcelAddress("B11:B20"));
163-
164-
cfRule9.StdDev = 0;
163+
cfRule9.StdDev = 0;
165164

166165
// -------------------------------------------------------------------
167166
// Create a Below StdDev rule

SampleApp/Sample15.cs

+54-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
using System;
1+
/*
2+
* You may amend and distribute as you like, but don't remove this header!
3+
*
4+
* EPPlus provides server-side generation of Excel 2007 spreadsheets.
5+
* See http://www.codeplex.com/EPPlus for details.
6+
*
7+
* All rights reserved.
8+
*
9+
* EPPlus is an Open Source project provided under the
10+
* GNU General Public License (GPL) as published by the
11+
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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+
* Code change notes:
24+
*
25+
* Author Change Date
26+
* ******************************************************************************
27+
* Jan Källman Added 2012-05-01
28+
*******************************************************************************/
29+
using System;
230
using System.Collections.Generic;
331
using System.Linq;
432
using System.Text;
@@ -14,8 +42,9 @@ class Sample15
1442
{
1543
public static void VBASample(DirectoryInfo outputDir)
1644
{
17-
//Create a macroenabled workbook from scratch.
45+
//Create a macro-enabled workbook from scratch.
1846
VBASample1(outputDir);
47+
1948
//Open Sample 1 and add code to change the chart to a bubble chart.
2049
VBASample2(outputDir);
2150

@@ -38,21 +67,10 @@ private static void VBASample1(DirectoryInfo outputDir)
3867
var sb = new StringBuilder();
3968

4069
sb.AppendLine("Private Sub Workbook_Open()");
41-
sb.AppendLine(" [VBA Sample].Shapes(\"VBASampleRect\").TextEffect.Text = \"This test is set from VBA!\"");
70+
sb.AppendLine(" [VBA Sample].Shapes(\"VBASampleRect\").TextEffect.Text = \"This text is set from VBA!\"");
4271
sb.AppendLine("End Sub");
4372
pck.Workbook.CodeModule.Code = sb.ToString();
4473

45-
/*** Try to find a cert valid for signing... ***/
46-
X509Store store = new X509Store(StoreLocation.CurrentUser);
47-
store.Open(OpenFlags.ReadOnly);
48-
foreach (var cert in store.Certificates)
49-
{
50-
if(cert.HasPrivateKey && cert.NotBefore <= DateTime.Today && cert.NotAfter >= DateTime.Today)
51-
{
52-
//pck.Workbook.VbaProject.Signature.Certificate = cert;
53-
}
54-
}
55-
5674
//And Save as xlsm
5775
pck.SaveAs(new FileInfo(outputDir.FullName + @"\sample15-1.xlsm"));
5876
}
@@ -94,6 +112,9 @@ private static void VBASample2(DirectoryInfo outputDir)
94112

95113
private static void VBASample3(DirectoryInfo outputDir)
96114
{
115+
//Now, lets do something a little bit more fun.
116+
//We are going to create a simple battleships game from scratch.
117+
97118
ExcelPackage pck = new ExcelPackage();
98119

99120
//Add a worksheet.
@@ -107,6 +128,7 @@ private static void VBASample3(DirectoryInfo outputDir)
107128

108129
int gridSize=10;
109130

131+
//Create the boards
110132
var board1 = ws.Cells[2, 2, 2 + gridSize - 1, 2 + gridSize - 1];
111133
var board2 = ws.Cells[2, 4+gridSize-1, 2 + gridSize-1, 4 + (gridSize-1)*2];
112134
CreateBoard(board1);
@@ -121,14 +143,15 @@ private static void VBASample3(DirectoryInfo outputDir)
121143
//Password protect your code
122144
pck.Workbook.VbaProject.Protection.SetPassword("EPPlus");
123145

146+
//Add all the code from the textfiles in the Vba-Code sub-folder.
124147
pck.Workbook.CodeModule.Code = File.ReadAllText("..\\..\\VBA-Code\\ThisWorkbook.txt");
125148

126149
//Add the sheet code
127150
ws.CodeModule.Code = File.ReadAllText("..\\..\\VBA-Code\\BattleshipSheet.txt");
128151
var m1=pck.Workbook.VbaProject.Modules.AddModule("Code");
129152
string code = File.ReadAllText("..\\..\\VBA-Code\\CodeModule.txt");
130153

131-
//Insert your ships on the right board
154+
//Insert your ships on the right board. you can changes these, but don't cheat ;)
132155
var ships = new string[]{
133156
"N3:N7",
134157
"P2:S2",
@@ -138,6 +161,7 @@ private static void VBASample3(DirectoryInfo outputDir)
138161

139162
code = string.Format(code, ships[0],ships[1],ships[2],ships[3],ships[4], board1.Address, board2.Address); //Ships are injected into the constants in the module
140163
m1.Code = code;
164+
141165
//Ships are displayed with a black background
142166
string shipsaddress = string.Join(",", ships);
143167
ws.Cells[shipsaddress].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
@@ -149,17 +173,32 @@ private static void VBASample3(DirectoryInfo outputDir)
149173
var c1 = pck.Workbook.VbaProject.Modules.AddClass("Ship",false);
150174
c1.Code = File.ReadAllText("..\\..\\VBA-Code\\ShipClass.txt");
151175

176+
//Add the info text shape.
152177
var tb = ws.Drawings.AddShape("txtInfo", eShapeStyle.Rect);
153178
tb.SetPosition(1, 0, 27, 0);
154179
tb.Fill.Color = Color.LightSlateGray;
155180
var rt1 = tb.RichText.Add("Battleships");
156181
rt1.Bold = true;
157182
tb.RichText.Add("\r\nDouble-click on the left board to make your move. Find and sink all ships to Win!");
158183

184+
//Set the headers.
159185
ws.SetValue("B1", "Computer Grid");
160186
ws.SetValue("M1", "Your Grid");
161187
ws.Row(1).Style.Font.Size = 18;
162188

189+
//If you have a valid certificate for code signing you can use this code to set it.
190+
///*** Try to find a cert valid for signing... ***/
191+
//X509Store store = new X509Store(StoreLocation.CurrentUser);
192+
//store.Open(OpenFlags.ReadOnly);
193+
//foreach (var cert in store.Certificates)
194+
//{
195+
// if (cert.HasPrivateKey && cert.NotBefore <= DateTime.Today && cert.NotAfter >= DateTime.Today)
196+
// {
197+
// pck.Workbook.VbaProject.Signature.Certificate = cert;
198+
// break;
199+
// }
200+
//}
201+
163202
pck.SaveAs(new FileInfo(outputDir.FullName + @"\sample15-3.xlsm"));
164203
}
165204

SampleApp/VBA-Code/ShipClass.txt

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Public Property Set Position(ByVal value As Range)
1111
End Property
1212

1313
Public Function IsHit(cell As Range) As Boolean
14+
Attribute IsHit.VB_Description = "Checks if the current move is a hit"
15+
Attribute IsHit.VB_HelpID = 1
16+
'Sample of attribute use on function level (show up in the objectbrowser(F2) --> Method --> Properties
1417
If Code.Collide(cell, Position) Then
1518
cell.Worksheet.Unprotect
1619
cell.Interior.Color = vbRed

0 commit comments

Comments
 (0)