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 ;
2
30
using System . Collections . Generic ;
3
31
using System . Linq ;
4
32
using System . Text ;
@@ -14,8 +42,9 @@ class Sample15
14
42
{
15
43
public static void VBASample ( DirectoryInfo outputDir )
16
44
{
17
- //Create a macroenabled workbook from scratch.
45
+ //Create a macro-enabled workbook from scratch.
18
46
VBASample1 ( outputDir ) ;
47
+
19
48
//Open Sample 1 and add code to change the chart to a bubble chart.
20
49
VBASample2 ( outputDir ) ;
21
50
@@ -38,21 +67,10 @@ private static void VBASample1(DirectoryInfo outputDir)
38
67
var sb = new StringBuilder ( ) ;
39
68
40
69
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!\" " ) ;
42
71
sb . AppendLine ( "End Sub" ) ;
43
72
pck . Workbook . CodeModule . Code = sb . ToString ( ) ;
44
73
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
-
56
74
//And Save as xlsm
57
75
pck . SaveAs ( new FileInfo ( outputDir . FullName + @"\sample15-1.xlsm" ) ) ;
58
76
}
@@ -94,6 +112,9 @@ private static void VBASample2(DirectoryInfo outputDir)
94
112
95
113
private static void VBASample3 ( DirectoryInfo outputDir )
96
114
{
115
+ //Now, lets do something a little bit more fun.
116
+ //We are going to create a simple battleships game from scratch.
117
+
97
118
ExcelPackage pck = new ExcelPackage ( ) ;
98
119
99
120
//Add a worksheet.
@@ -107,6 +128,7 @@ private static void VBASample3(DirectoryInfo outputDir)
107
128
108
129
int gridSize = 10 ;
109
130
131
+ //Create the boards
110
132
var board1 = ws . Cells [ 2 , 2 , 2 + gridSize - 1 , 2 + gridSize - 1 ] ;
111
133
var board2 = ws . Cells [ 2 , 4 + gridSize - 1 , 2 + gridSize - 1 , 4 + ( gridSize - 1 ) * 2 ] ;
112
134
CreateBoard ( board1 ) ;
@@ -121,14 +143,15 @@ private static void VBASample3(DirectoryInfo outputDir)
121
143
//Password protect your code
122
144
pck . Workbook . VbaProject . Protection . SetPassword ( "EPPlus" ) ;
123
145
146
+ //Add all the code from the textfiles in the Vba-Code sub-folder.
124
147
pck . Workbook . CodeModule . Code = File . ReadAllText ( "..\\ ..\\ VBA-Code\\ ThisWorkbook.txt" ) ;
125
148
126
149
//Add the sheet code
127
150
ws . CodeModule . Code = File . ReadAllText ( "..\\ ..\\ VBA-Code\\ BattleshipSheet.txt" ) ;
128
151
var m1 = pck . Workbook . VbaProject . Modules . AddModule ( "Code" ) ;
129
152
string code = File . ReadAllText ( "..\\ ..\\ VBA-Code\\ CodeModule.txt" ) ;
130
153
131
- //Insert your ships on the right board
154
+ //Insert your ships on the right board. you can changes these, but don't cheat ;)
132
155
var ships = new string [ ] {
133
156
"N3:N7" ,
134
157
"P2:S2" ,
@@ -138,6 +161,7 @@ private static void VBASample3(DirectoryInfo outputDir)
138
161
139
162
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
140
163
m1 . Code = code ;
164
+
141
165
//Ships are displayed with a black background
142
166
string shipsaddress = string . Join ( "," , ships ) ;
143
167
ws . Cells [ shipsaddress ] . Style . Fill . PatternType = OfficeOpenXml . Style . ExcelFillStyle . Solid ;
@@ -149,17 +173,32 @@ private static void VBASample3(DirectoryInfo outputDir)
149
173
var c1 = pck . Workbook . VbaProject . Modules . AddClass ( "Ship" , false ) ;
150
174
c1 . Code = File . ReadAllText ( "..\\ ..\\ VBA-Code\\ ShipClass.txt" ) ;
151
175
176
+ //Add the info text shape.
152
177
var tb = ws . Drawings . AddShape ( "txtInfo" , eShapeStyle . Rect ) ;
153
178
tb . SetPosition ( 1 , 0 , 27 , 0 ) ;
154
179
tb . Fill . Color = Color . LightSlateGray ;
155
180
var rt1 = tb . RichText . Add ( "Battleships" ) ;
156
181
rt1 . Bold = true ;
157
182
tb . RichText . Add ( "\r \n Double-click on the left board to make your move. Find and sink all ships to Win!" ) ;
158
183
184
+ //Set the headers.
159
185
ws . SetValue ( "B1" , "Computer Grid" ) ;
160
186
ws . SetValue ( "M1" , "Your Grid" ) ;
161
187
ws . Row ( 1 ) . Style . Font . Size = 18 ;
162
188
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
+
163
202
pck . SaveAs ( new FileInfo ( outputDir . FullName + @"\sample15-3.xlsm" ) ) ;
164
203
}
165
204
0 commit comments