Skip to content

Commit 1595d6f

Browse files
Provide support for ExcelLite implementation for .xls documents (Excel Office 97 to 2003).
1 parent 3193814 commit 1595d6f

File tree

6 files changed

+98
-12
lines changed

6 files changed

+98
-12
lines changed

dotnet/src/dotnetcore/GxExcel/GxExcel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<ItemGroup>
1919
<PackageReference Include="EPPlus" Version="4.5.3.2" />
20+
<PackageReference Include="GemBox.Spreadsheet" Version="49.0.1454" PrivateAssets="All"/>
2021
<PackageReference Include="log4net" Version="2.0.15" />
2122
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="6.0.3" />
2223
</ItemGroup>

dotnet/src/dotnetframework/GxExcel/GxExcelI.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public bool checkExcelDocument()
5252
{
5353
if (Document == null)
5454
{
55-
#if !NETCORE
5655
if (this.fileName.EndsWith(Constants.EXCEL2003Extension) || this.template.EndsWith(Constants.EXCEL2003ExtensionTemplate))
5756
{
5857
if (document == null || document.ErrCode == 99)
@@ -63,18 +62,19 @@ public bool checkExcelDocument()
6362
document.ReadOnly = ReadOnly;
6463
document.Init(initErrDesc);
6564
}
65+
#if !NETCORE
6666
if (document == null || document.ErrCode != 0) //Automation
6767
{
6868
GXLogging.Debug(log,"Interop.GXOFFICE2Lib.ExcelDocumentClass");
6969
document = new IExcelDocumentWrapper(new Interop.GXOFFICE2Lib.ExcelDocumentClass());
7070
document.ReadOnly = ReadOnly;
7171
document.Init("");
7272
}
73-
}
74-
else
7573
#endif
76-
{
77-
document = new GeneXus.Office.ExcelGXEPPlus.ExcelDocument();
74+
}
75+
else
76+
{
77+
document = new GeneXus.Office.ExcelGXEPPlus.ExcelDocument();
7878
document.ReadOnly = ReadOnly;
7979
}
8080

@@ -437,9 +437,14 @@ public static object GetEnumValue(Assembly ass, string enumSuperClass, string en
437437
return fi.GetValue(null);
438438

439439
}
440+
internal static object GetEnumValue(Assembly ass, string enumNameClass, string enumField)
441+
{
442+
Type prn1 = ass.GetType(enumNameClass);
443+
return prn1.GetProperty(enumField).GetValue(null);
444+
}
440445

441-
}
442-
public interface IGxError
446+
}
447+
public interface IGxError
443448
{
444449
void setErrCod(short errCod);
445450
void setErrDes(String errDes);

dotnet/src/dotnetframework/GxExcel/GxExcelLite.cs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using log4net;
77
using GeneXus.Application;
88
using GeneXus.Office.Excel;
9+
using GxClasses.Helpers;
10+
911

1012
namespace GeneXus.Office.ExcelLite
1113
{
@@ -368,7 +370,16 @@ public class ExcelDocument : IGxError, IExcelDocument
368370
{
369371
static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
370372
public static string nmspace;
373+
#if NETCORE
374+
public static string license = "FREE-LIMITED-KEY";
375+
const string LoadMethod = "Load";
376+
const string SaveMethod = "Save";
377+
#else
371378
public static string license;
379+
const string LoadMethod = "LoadXls";
380+
const string SaveMethod = "SaveXls";
381+
#endif
382+
372383
public static Assembly ass;
373384

374385
public short Init(string previousMsgError)
@@ -381,17 +392,25 @@ public short Init(string previousMsgError)
381392
{
382393
if (nmspace==null)
383394
{
395+
#if NETCORE
396+
ass = AssemblyLoader.LoadAssembly(new AssemblyName("GemBox.Spreadsheet"));
397+
#else
398+
384399
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "GemBox.Spreadsheet.dll"));
385400
if (ass==null)
386401
{
387402
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", "GemBox.Spreadsheet.dll"));
388403
}
404+
#endif
389405
if (ass!=null)
390406
{
391407
nmspace="GemBox.Spreadsheet";
392408
}
393409
else
394410
{
411+
#if NETCORE
412+
ass = AssemblyLoader.LoadAssembly(new AssemblyName("GemBox.ExcelLite"));
413+
#else
395414
if (ass==null)
396415
{
397416
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), @"GemBox.ExcelLite.dll"));
@@ -400,6 +419,7 @@ public short Init(string previousMsgError)
400419
{
401420
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", "GemBox.ExcelLite.dll"));
402421
}
422+
#endif
403423
if (ass!=null)
404424
{
405425
nmspace="GemBox.ExcelLite";
@@ -409,11 +429,15 @@ public short Init(string previousMsgError)
409429
}
410430
else
411431
{
432+
#if NETCORE
433+
ass = AssemblyLoader.LoadAssembly(new AssemblyName(nmspace));
434+
#else
412435
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), nmspace + ".dll"));
413436
if (ass==null)
414437
{
415438
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", nmspace + ".dll"));
416439
}
440+
#endif
417441
}
418442
GXLogging.Debug(log, "nmspace:" + nmspace);
419443

@@ -434,6 +458,7 @@ public short Init(string previousMsgError)
434458
GXLogging.Error(log, @"Error setting license.", e);
435459
}
436460
}
461+
437462
}
438463
return 0;
439464
}
@@ -458,7 +483,6 @@ private Assembly loadAssembly(string fileName)
458483
{
459484
return null;
460485
}
461-
462486
}
463487
public short Open(String fileName)
464488
{
@@ -479,7 +503,11 @@ public short Open(String fileName)
479503
if (stream != null)
480504
{
481505
stream.Position = 0;
482-
GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream });
506+
#if NETCORE
507+
GxExcelUtils.InvokeStatic(ass, classType.FullName, LoadMethod, new object[] { stream });
508+
#else
509+
GxExcelUtils.Invoke(ef, LoadMethod, new object[] { stream });
510+
#endif
483511
}
484512
}
485513
else
@@ -495,7 +523,12 @@ public short Open(String fileName)
495523
if (stream != null)
496524
{
497525
stream.Position = 0;
498-
GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream });
526+
527+
#if NETCORE
528+
ef = GxExcelUtils.InvokeStatic(ass, classType.FullName, LoadMethod, new object[] { stream });
529+
#else
530+
GxExcelUtils.Invoke(ef, LoadMethod, new object[] { stream });
531+
#endif
499532
}
500533
}
501534
else
@@ -523,8 +556,14 @@ public short Save()
523556
{
524557
GxFile file = new GxFile(Path.GetDirectoryName(xlsFileName), xlsFileName, GxFileType.Private);
525558
MemoryStream content = new MemoryStream();
526-
GxExcelUtils.Invoke(ef, "SaveXls", new object[]{content});
527-
content.Position = 0;
559+
560+
#if NETCORE
561+
object saveOption = GxExcelUtils.GetEnumValue(ExcelDocument.ass, ExcelDocument.nmspace + ".SaveOptions", "XlsDefault");
562+
GxExcelUtils.Invoke(ef, SaveMethod, new object[] { content, saveOption });
563+
#else
564+
GxExcelUtils.Invoke(ef, SaveMethod, new object[]{content});
565+
#endif
566+
content.Position = 0;
528567
file.Create(content);
529568
}
530569
catch (Exception e)

dotnet/test/DotNetCoreUnitTest/DotNetCoreUnitTest.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@
6767
<PrivateAssets>all</PrivateAssets>
6868
</PackageReference>
6969
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
70+
<PackageReference Include="GemBox.Spreadsheet" Version="49.0.1454"/>
7071
</ItemGroup>
7172

7273
<ItemGroup>
7374
<ProjectReference Include="..\..\src\dotnetcommon\GxEncrypt\GxEncrypt.csproj" />
7475
<ProjectReference Include="..\..\src\dotnetcore\GxClasses.Web\GxClasses.Web.csproj" />
7576
<ProjectReference Include="..\..\src\dotnetcore\GxClasses\GxClasses.csproj" />
77+
<ProjectReference Include="..\..\src\dotnetcore\GxExcel\GxExcel.csproj" />
7678
<ProjectReference Include="..\..\src\dotnetcore\GxMail\GxMail.csproj" />
7779
<ProjectReference Include="..\..\src\dotnetcore\GxOffice\GxOffice.csproj" />
7880
<ProjectReference Include="..\..\src\dotnetcore\GxPdfReportsCS\GxPdfReportsCS.csproj" />
@@ -150,6 +152,9 @@
150152
<None Update="resources\text.txt">
151153
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
152154
</None>
155+
<None Update="SampleXLS.xls">
156+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
157+
</None>
153158
</ItemGroup>
154159

155160

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.IO;
2+
using GeneXus.Office;
3+
using GeneXus.Utils;
4+
using UnitTesting;
5+
using Xunit;
6+
7+
namespace DotNetCoreUnitTest.Excel
8+
{
9+
public class ExcelLiteTest : FileSystemTest
10+
{
11+
[Fact]
12+
public void ExcelLiteReadTest()
13+
{
14+
ExcelDocumentI excelDocument= new ExcelDocumentI();
15+
string fileName = Path.Combine(BaseDir, "SampleXLS.xls");
16+
excelDocument.Open(fileName);
17+
Assert.Equal(0, excelDocument.ErrCode);
18+
double number = excelDocument.get_Cells(2, 1).Number;
19+
Assert.Equal(1, number);
20+
string text = excelDocument.get_Cells(2, 2).Text;
21+
Assert.Equal("A", text);
22+
23+
number = excelDocument.get_Cells(3, 1).Number;
24+
Assert.Equal(2, number);
25+
text = excelDocument.get_Cells(3, 2).Text;
26+
Assert.Equal("A", text);
27+
28+
number = excelDocument.get_Cells(101, 1).Number;
29+
Assert.Equal(100, number);
30+
text = excelDocument.get_Cells(101, 2).Text;
31+
Assert.Equal("A", text);
32+
33+
excelDocument.Close();
34+
}
35+
}
36+
}
29.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)