-
André Rust
- PowerBuilder
- Thursday, 24 July 2025 12:51 PM UTC
Hi,
I have now tried to create a simple .NET Framework (4.8) version test example with ClosedXML in SnapDevelop. Then I tried to import the DLL using the .NET DLL Importer. Framework Type: .NET Framework
-
Question: In Advanced Settings, does 'Encapsulate a DotNetAssembly' need to be activated?
Then, select everything with 'select and preview' and import it.
C# Code (from the internet):
using System;
using System.IO;
using ClosedXML.Excel;
namespace PowerBuilderExcelUtil
{
public class ExcelHelper
{
public ExcelHelper()
{
// none
}
public int CreateExcelWithData(string filePath, string sheetName, string[] headerRow, string[,] dataRows)
{
try
{
string directory = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add(sheetName);
// Header schreiben
for (int col = 0; col < headerRow.Length; col++)
{
worksheet.Cell(1, col + 1).Value = headerRow[col];
worksheet.Cell(1, col + 1).Style.Font.Bold = true;
worksheet.Cell(1, col + 1).Style.Fill.BackgroundColor = XLColor.LightGray;
}
int numRows = dataRows.GetLength(0);
int numCols = dataRows.GetLength(1);
for (int row = 0; row < numRows; row++)
{
for (int col = 0; col < numCols; col++)
{
worksheet.Cell(row + 2, col + 1).Value = dataRows[row, col];
}
}
worksheet.Columns().AdjustToContents();
workbook.SaveAs(filePath);
}
Console.WriteLine($"C#: Excel-Datei mit Daten erfolgreich erstellt unter: {filePath}");
return 0; // Erfolg
}
catch (Exception ex)
{
Console.Error.WriteLine($"C# Fehler beim Erstellen der Excel-Datei mit Daten: {ex.Message}");
return -1; // Fehler
}
}
}
}
Powerbuilder:
nvo_excelhelper lo_excel_helper
string ls_filepath
string ls_sheetname
string ls_cell_value
integer li_return_code
ls_filepath = "C:\temp\SimplePowerBuilderExcel.xlsx"
ls_sheetname = "FirstSheet"
ls_cell_value = "Hello PowerBuilder direct via .NET!"
lo_excel_helper = CREATE nvo_excelhelper
IF IsValid(lo_excel_helper) THEN
li_return_code = lo_excel_helper.of_CreateSimpleExcelFile(ls_filepath, ls_sheetname, ls_cell_value)
IF li_return_code = 0 THEN
MessageBox("Success", "Simple Excel file created under:~n" + ls_filepath)
ELSE
MessageBox("Error", "Error creating simple Excel file. Code: " + String(li_return_code) + "~nCheck the C# console window for details.")
END IF
DESTROY lo_excel_helper
ELSE
MessageBox("Error", "Could not instantiate ExcelHelper object.~nEnsure that ALL C# DLLs are in the PB application directory.")
END IF
When I run the code in the debugger and it reaches the point with '....of_CreateExcelWithData...', I always get an error message.
The DLLs are in the directory where the PBW (PowerBuilder Workspace) is located.
Which DLLs do I need? Do they have to be in the PBW/PBT folder, or for an EXE, in the EXE's folder?
I don't know what to do next because I don't know what I did wrong, or what I can do differently.
Can anyone help me? e.g. with c# and PB code
Thanks André
Find Questions by Tag
Helpful?
If a reply or comment is helpful for you, please don’t hesitate to click the Helpful button. This action is further confirmation of their invaluable contribution to the Appeon Community.