Skip to content

Commit

Permalink
Provide support for ExcelLite implementation for .xls documents (#882)
Browse files Browse the repository at this point in the history
* Provide support for ExcelLite implementation for .xls documents (Excel Office 97 to 2003).

* Remove unneeded package reference to GemBox.Spreadsheet.

(cherry picked from commit f531a26)
  • Loading branch information
claudiamurialdo committed Dec 3, 2023
1 parent 0cde42c commit ca82946
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 12 deletions.
19 changes: 12 additions & 7 deletions dotnet/src/dotnetframework/GxExcel/GxExcelI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public bool checkExcelDocument()
{
if (Document == null)
{
#if !NETCORE
if (this.fileName.EndsWith(Constants.EXCEL2003Extension) || this.template.EndsWith(Constants.EXCEL2003ExtensionTemplate))
{
if (document == null || document.ErrCode == 99)
Expand All @@ -63,18 +62,19 @@ public bool checkExcelDocument()
document.ReadOnly = ReadOnly;
document.Init(initErrDesc);
}
#if !NETCORE
if (document == null || document.ErrCode != 0) //Automation
{
GXLogging.Debug(log,"Interop.GXOFFICE2Lib.ExcelDocumentClass");
document = new IExcelDocumentWrapper(new Interop.GXOFFICE2Lib.ExcelDocumentClass());
document.ReadOnly = ReadOnly;
document.Init("");
}
}
else
#endif
{
document = new GeneXus.Office.ExcelGXEPPlus.ExcelDocument();
}
else
{
document = new GeneXus.Office.ExcelGXEPPlus.ExcelDocument();
document.ReadOnly = ReadOnly;
}

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

}
internal static object GetEnumValue(Assembly ass, string enumNameClass, string enumField)
{
Type prn1 = ass.GetType(enumNameClass);
return prn1.GetProperty(enumField).GetValue(null);
}

}
public interface IGxError
}
public interface IGxError
{
void setErrCod(short errCod);
void setErrDes(String errDes);
Expand Down
49 changes: 44 additions & 5 deletions dotnet/src/dotnetframework/GxExcel/GxExcelLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using log4net;
using GeneXus.Application;
using GeneXus.Office.Excel;
using GxClasses.Helpers;


namespace GeneXus.Office.ExcelLite
{
Expand Down Expand Up @@ -368,7 +370,16 @@ public class ExcelDocument : IGxError, IExcelDocument
{
static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static string nmspace;
#if NETCORE
public static string license = "FREE-LIMITED-KEY";
const string LoadMethod = "Load";
const string SaveMethod = "Save";
#else
public static string license;
const string LoadMethod = "LoadXls";
const string SaveMethod = "SaveXls";
#endif

public static Assembly ass;

public short Init(string previousMsgError)
Expand All @@ -381,17 +392,25 @@ public short Init(string previousMsgError)
{
if (nmspace==null)
{
#if NETCORE
ass = AssemblyLoader.LoadAssembly(new AssemblyName("GemBox.Spreadsheet"));
#else

ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "GemBox.Spreadsheet.dll"));
if (ass==null)
{
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", "GemBox.Spreadsheet.dll"));
}
#endif
if (ass!=null)
{
nmspace="GemBox.Spreadsheet";
}
else
{
#if NETCORE
ass = AssemblyLoader.LoadAssembly(new AssemblyName("GemBox.ExcelLite"));
#else
if (ass==null)
{
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), @"GemBox.ExcelLite.dll"));
Expand All @@ -400,6 +419,7 @@ public short Init(string previousMsgError)
{
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", "GemBox.ExcelLite.dll"));
}
#endif
if (ass!=null)
{
nmspace="GemBox.ExcelLite";
Expand All @@ -409,11 +429,15 @@ public short Init(string previousMsgError)
}
else
{
#if NETCORE
ass = AssemblyLoader.LoadAssembly(new AssemblyName(nmspace));
#else
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), nmspace + ".dll"));
if (ass==null)
{
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", nmspace + ".dll"));
}
#endif
}
GXLogging.Debug(log, "nmspace:" + nmspace);

Expand All @@ -434,6 +458,7 @@ public short Init(string previousMsgError)
GXLogging.Error(log, @"Error setting license.", e);
}
}

}
return 0;
}
Expand All @@ -458,7 +483,6 @@ private Assembly loadAssembly(string fileName)
{
return null;
}

}
public short Open(String fileName)
{
Expand All @@ -479,7 +503,11 @@ public short Open(String fileName)
if (stream != null)
{
stream.Position = 0;
GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream });
#if NETCORE
GxExcelUtils.InvokeStatic(ass, classType.FullName, LoadMethod, new object[] { stream });
#else
GxExcelUtils.Invoke(ef, LoadMethod, new object[] { stream });
#endif
}
}
else
Expand All @@ -495,7 +523,12 @@ public short Open(String fileName)
if (stream != null)
{
stream.Position = 0;
GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream });

#if NETCORE
ef = GxExcelUtils.InvokeStatic(ass, classType.FullName, LoadMethod, new object[] { stream });
#else
GxExcelUtils.Invoke(ef, LoadMethod, new object[] { stream });
#endif
}
}
else
Expand Down Expand Up @@ -523,8 +556,14 @@ public short Save()
{
GxFile file = new GxFile(Path.GetDirectoryName(xlsFileName), xlsFileName, GxFileType.Private);
MemoryStream content = new MemoryStream();
GxExcelUtils.Invoke(ef, "SaveXls", new object[]{content});
content.Position = 0;

#if NETCORE
object saveOption = GxExcelUtils.GetEnumValue(ExcelDocument.ass, ExcelDocument.nmspace + ".SaveOptions", "XlsDefault");
GxExcelUtils.Invoke(ef, SaveMethod, new object[] { content, saveOption });
#else
GxExcelUtils.Invoke(ef, SaveMethod, new object[]{content});
#endif
content.Position = 0;
file.Create(content);
}
catch (Exception e)
Expand Down
5 changes: 5 additions & 0 deletions dotnet/test/DotNetCoreUnitTest/DotNetCoreUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
<PackageReference Include="GemBox.Spreadsheet" Version="49.0.1454"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\dotnetcommon\GxEncrypt\GxEncrypt.csproj" />
<ProjectReference Include="..\..\src\dotnetcore\GxClasses.Web\GxClasses.Web.csproj" />
<ProjectReference Include="..\..\src\dotnetcore\GxClasses\GxClasses.csproj" />
<ProjectReference Include="..\..\src\dotnetcore\GxExcel\GxExcel.csproj" />
<ProjectReference Include="..\..\src\dotnetcore\GxMail\GxMail.csproj" />
<ProjectReference Include="..\..\src\dotnetcore\GxOffice\GxOffice.csproj" />
<ProjectReference Include="..\..\src\dotnetcore\GxPdfReportsCS\GxPdfReportsCS.csproj" />
Expand Down Expand Up @@ -150,6 +152,9 @@
<None Update="resources\text.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="SampleXLS.xls">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


Expand Down
36 changes: 36 additions & 0 deletions dotnet/test/DotNetCoreUnitTest/Excel/ExcelLiteTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.IO;
using GeneXus.Office;
using GeneXus.Utils;
using UnitTesting;
using Xunit;

namespace DotNetCoreUnitTest.Excel
{
public class ExcelLiteTest : FileSystemTest
{
[Fact]
public void ExcelLiteReadTest()
{
ExcelDocumentI excelDocument= new ExcelDocumentI();
string fileName = Path.Combine(BaseDir, "SampleXLS.xls");
excelDocument.Open(fileName);
Assert.Equal(0, excelDocument.ErrCode);
double number = excelDocument.get_Cells(2, 1).Number;
Assert.Equal(1, number);
string text = excelDocument.get_Cells(2, 2).Text;
Assert.Equal("A", text);

number = excelDocument.get_Cells(3, 1).Number;
Assert.Equal(2, number);
text = excelDocument.get_Cells(3, 2).Text;
Assert.Equal("A", text);

number = excelDocument.get_Cells(101, 1).Number;
Assert.Equal(100, number);
text = excelDocument.get_Cells(101, 2).Text;
Assert.Equal("A", text);

excelDocument.Close();
}
}
}
Binary file added dotnet/test/DotNetCoreUnitTest/SampleXLS.xls
Binary file not shown.

0 comments on commit ca82946

Please sign in to comment.