Skip to content

Commit f6b5a1d

Browse files
author
JanKallman
committed
Basic VBA support.
Workbook.CreateVBAProject method to create a new project. Workbook.CodeModule and Worksheet.CodeModule to access document code modules. Workbook.VBAProject to access project properties. Workbook.VBAProject.Modules.AddModule to add a new code Module. Workbook.VBAProject.Modules.AddClass to add a new Class. ...and a lot more. Working but need some polish. --HG-- branch : VBA rename : EPPlus/EncryptedPackageHandler.cs => EPPlus/Utils/EncryptedPackageHandler.cs rename : EPPlus/ExcelVBAProject.cs => EPPlus/VBA/ExcelVBAProject.cs
1 parent d59aada commit f6b5a1d

28 files changed

+2462
-510
lines changed

.hgignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
syntax: glob
2+
*.obj
3+
*.pdb
4+
*.user
5+
*.aps
6+
*.pch
7+
*.vspscc
8+
*.vssscc
9+
*.ncb
10+
*.suo
11+
*.tlb
12+
*.tlh
13+
*.bak
14+
*.cache
15+
*.ilk
16+
*.log
17+
*.lib
18+
*.sbr
19+
*.scc
20+
[Bb]in
21+
[Dd]ebug*/
22+
obj/
23+
[Rr]elease*/
24+
[Tt]humbs.db
25+
[Tt]est[Rr]esult*
26+
[Bb]uild[Ll]og.*
27+
*.[Pp]ublish.xml

EPPlus/Drawing/ExcelDrawings.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public ExcelChart AddChart(string Name, eChartType ChartType, ExcelPivotTable Pi
239239
{
240240
if(_drawingNames.ContainsKey(Name.ToLower()))
241241
{
242-
throw new Exception("Name already exist in the drawings collection");
242+
throw new Exception("Name already exists in the drawings collection");
243243
}
244244

245245
if (ChartType == eChartType.Bubble ||
@@ -289,7 +289,7 @@ public ExcelPicture AddPicture(string Name, Image image)
289289
{
290290
if (_drawingNames.ContainsKey(Name.ToLower()))
291291
{
292-
throw new Exception("Name already exist in the drawings collection");
292+
throw new Exception("Name already exists in the drawings collection");
293293
}
294294
XmlElement drawNode = CreateDrawingXml();
295295
drawNode.SetAttribute("editAs", "oneCell");
@@ -313,7 +313,7 @@ public ExcelPicture AddPicture(string Name, FileInfo ImageFile)
313313
{
314314
if (_drawingNames.ContainsKey(Name.ToLower()))
315315
{
316-
throw new Exception("Name already exist in the drawings collection");
316+
throw new Exception("Name already exists in the drawings collection");
317317
}
318318
XmlElement drawNode = CreateDrawingXml();
319319
drawNode.SetAttribute("editAs", "oneCell");
@@ -336,7 +336,7 @@ public ExcelShape AddShape(string Name, eShapeStyle Style)
336336
{
337337
if (_drawingNames.ContainsKey(Name.ToLower()))
338338
{
339-
throw new Exception("Name already exist in the drawings collection");
339+
throw new Exception("Name already exists in the drawings collection");
340340
}
341341
XmlElement drawNode = CreateDrawingXml();
342342
ExcelShape shape = new ExcelShape(this, drawNode, Style);

EPPlus/EPPlus.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,12 @@
145145
<Compile Include="ExcelStyles.cs" />
146146
<Compile Include="ExcelProtection.cs" />
147147
<Compile Include="ExcelTextFormat.cs" />
148-
<Compile Include="ExcelVBAProject.cs" />
148+
<Compile Include="VBA\ExcelVbaModule.cs" />
149+
<Compile Include="VBA\ExcelVbaModuleCollection.cs" />
150+
<Compile Include="VBA\ExcelVbaProject.cs" />
149151
<Compile Include="ExcelWorkbookView.cs" />
150152
<Compile Include="ExcelWorksheetView.cs" />
151-
<Compile Include="EncryptedPackageHandler.cs" />
153+
<Compile Include="Utils\EncryptedPackageHandler.cs" />
152154
<Compile Include="Style\ExcelGradientFill.cs" />
153155
<Compile Include="Style\XmlAccess\ExcelGradientFillXml.cs" />
154156
<Compile Include="Table\PivotTable\ExcelPivotTableFieldGroup.cs" />
@@ -186,6 +188,9 @@
186188
<Compile Include="Utils\IArgument.cs" />
187189
<Compile Include="Utils\Require.cs" />
188190
<Compile Include="Utils\SqRefUtility.cs" />
191+
<Compile Include="VBA\ExcelVbaProtection.cs" />
192+
<Compile Include="VBA\ExcelVbaReference.cs" />
193+
<Compile Include="VBA\ExcelVbaSignature.cs" />
189194
<Compile Include="XmlHelper.cs" />
190195
<Compile Include="XmlHelperFactory.cs" />
191196
</ItemGroup>
@@ -261,7 +266,6 @@
261266
<Install>true</Install>
262267
</BootstrapperPackage>
263268
</ItemGroup>
264-
<ItemGroup />
265269
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
266270
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
267271
Other similar extension points exist, see Microsoft.Common.targets.

EPPlus/ExcelPackage.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
using System.Collections.Generic;
4040
using System.Security.Cryptography;
4141
using OfficeOpenXml.Drawing;
42+
using OfficeOpenXml.Utils;
4243
namespace OfficeOpenXml
4344
{
4445
/// <summary>
@@ -191,6 +192,8 @@ internal class ImageInfo
191192
internal const string schemaVBA = @"application/vnd.ms-office.vbaProject";
192193
internal const string schemaVBASignature = @"application/vnd.ms-office.vbaProjectSignature";
193194

195+
internal const string contentTypeWorkbookDefault = @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
196+
internal const string contentTypeWorkbookMacroEnabled = "application/vnd.ms-excel.sheet.macroEnabled.main+xml";
194197
//Package reference
195198
private Package _package;
196199
private ExcelWorkbook _workbook;
@@ -611,9 +614,41 @@ private XmlNamespaceManager CreateDefaultNSM()
611614
/// <param name="xmlDoc">The XmlDocument to save</param>
612615
internal void SavePart(Uri uri, XmlDocument xmlDoc)
613616
{
614-
PackagePart part = _package.GetPart(uri);
617+
PackagePart part = _package.GetPart(uri);
615618
xmlDoc.Save(part.GetStream(FileMode.Create, FileAccess.Write));
616619
}
620+
/// <summary>
621+
/// Saves the XmlDocument into the package at the specified Uri.
622+
/// </summary>
623+
/// <param name="uri">The Uri of the component</param>
624+
/// <param name="xmlDoc">The XmlDocument to save</param>
625+
internal void SaveWorkbook(Uri uri, XmlDocument xmlDoc)
626+
{
627+
PackagePart part = _package.GetPart(uri);
628+
if(Workbook.VbaProject==null)
629+
{
630+
if (part.ContentType != contentTypeWorkbookDefault)
631+
{
632+
part = _package.CreatePart(uri, contentTypeWorkbookDefault, Compression);
633+
}
634+
}
635+
else
636+
{
637+
if (part.ContentType != contentTypeWorkbookMacroEnabled)
638+
{
639+
var rels = part.GetRelationships();
640+
_package.DeletePart(uri);
641+
part = Package.CreatePart(uri, contentTypeWorkbookMacroEnabled);
642+
foreach (var rel in rels)
643+
{
644+
Package.DeleteRelationship(rel.Id);
645+
part.CreateRelationship(rel.TargetUri, rel.TargetMode, rel.RelationshipType);
646+
}
647+
}
648+
}
649+
xmlDoc.Save(part.GetStream(FileMode.Create, FileAccess.Write));
650+
}
651+
617652
#endregion
618653

619654
#region Dispose

EPPlus/ExcelProtection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
using System.Collections.Generic;
3434
using System.Text;
3535
using System.Xml;
36+
using OfficeOpenXml.Utils;
3637
namespace OfficeOpenXml
3738
{
3839
/// <summary>

EPPlus/ExcelRangeBase.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,19 +1604,26 @@ public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool Prin
16041604
foreach (var item in Collection)
16051605
{
16061606
col = _fromCol;
1607-
foreach (var t in Members)
1607+
if (item is string || item is decimal || item is DateTime || item.GetType().IsPrimitive)
16081608
{
1609-
if (t is PropertyInfo)
1610-
{
1611-
_worksheet.Cells[row, col++].Value = ((PropertyInfo)t).GetValue(item, null);
1612-
}
1613-
else if (t is FieldInfo)
1614-
{
1615-
_worksheet.Cells[row, col++].Value = ((FieldInfo)t).GetValue(item);
1616-
}
1617-
else if (t is MethodInfo)
1609+
_worksheet.Cells[row, col++].Value = item;
1610+
}
1611+
else
1612+
{
1613+
foreach (var t in Members)
16181614
{
1619-
_worksheet.Cells[row, col++].Value = ((MethodInfo)t).Invoke(item, null);
1615+
if (t is PropertyInfo)
1616+
{
1617+
_worksheet.Cells[row, col++].Value = ((PropertyInfo)t).GetValue(item, null);
1618+
}
1619+
else if (t is FieldInfo)
1620+
{
1621+
_worksheet.Cells[row, col++].Value = ((FieldInfo)t).GetValue(item);
1622+
}
1623+
else if (t is MethodInfo)
1624+
{
1625+
_worksheet.Cells[row, col++].Value = ((MethodInfo)t).Invoke(item, null);
1626+
}
16201627
}
16211628
}
16221629
row++;

EPPlus/ExcelSheetProtection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
using System.Text;
3535
using System.Xml;
3636
using System.Security.Cryptography;
37+
using OfficeOpenXml.Utils;
3738

3839
namespace OfficeOpenXml
3940
{

EPPlus/ExcelStyles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public ExcelNamedStyleXml CreateNamedStyle(string name, ExcelStyle Template)
378378
{
379379
if (_wb.Styles.NamedStyles.ExistsKey(name))
380380
{
381-
throw new Exception(string.Format("Key {0} already exist in collection", name));
381+
throw new Exception(string.Format("Key {0} already exists in collection", name));
382382
}
383383

384384
ExcelNamedStyleXml style;

0 commit comments

Comments
 (0)