如何在 Dynamics AX2012 中使用代码 x++ 通过电子邮件创建 Excel 文件和它?

How to create an Excel file and it by Email, using code x++ in Dynamics AX2012?

我需要创建一个 Excel 文件并通过电子邮件将其通过 X++ 代码发送到 Dyncamics AX2012。 如果可能的话,我不想将数据保存在物理 path/folder.

我写了这段代码,但是在我需要发送 Excel.

我想遵循 Dynamics AX 2012 中的 D365 说明 (https://axexplorer.wordpress.com/2017/07/18/create-an-excel-file-and-send-it-through-e-mail-in-d365-for-operationax-7/ )(在 AX2012 中某些命令不可用)

SysEmailParameters parameters = SysEmailParameters::find();
SMTPRelayServerName relayServer;
SMTPPortNumber portNumber;
SMTPUserName userName;
SMTPPassword password;
Str1260 subject,body;
InteropPermission interopPermission;
SysMailerMessageBuilder  mailer;
SysMailerAttachments attach;
System.Exception e;

SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
SysExcelCell cell;

int row;
str contact;
int recordscount;
MYTABLE myTable;
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
row = 1 ;

mailer.subject("MY subject");
mailer.fromAddress("MY_from@com");
mailer.htmlBody("MY_BODY");
mailer.tos().appendAddress("MY_to.com");

application = SysExcelApplication::construct();

workbooks = application.workbooks();
workbook = workbooks.add();
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
//cells.range(‘A:A’).numberFormat(‘@’);
cell = cells.item(1,1);
cell.value("Field_I");
cell = cells.item(1,2);
cell.value("Field_II");

while select myTable
{
    row++;

    cell = cells.item(row, 1);
    cell.value(myTable.Field_I);
    cell = cells.item(row, 2);
    cell.value(myTable.Field_I);
}
application.visible(true);
application.save();
memoryStream.Seek(0, System.IO.SeekOrigin::Begin);

我无法将文件保存到临时流中。

在AX2012中不可用

SysMailerMessageBuilder mailer = new SysMailerMessageBuilder();;
var package = new OfficeOpenXml.ExcelPackage(memoryStream)

//以下行用于将 excel 文件附加到电子邮件。 mailer.addAttachment(memoryStream, 'MYFILE.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

在 Dynamics AX2012 中有任何方法可以创建 excel 文件并将文件发送到电子邮件(无需保存到路径)。

提前致谢。

根据@Alex Kwitny 的善意评论,我使用了以下代码:

#Properties
#AOT
#File
#define.io_rw("rw")
Filename                  filename;
str                       tempPath;
FileIOPermission          fileIOPermission;

if(_runOnService)
{
    fileIOPermission = new FileIOPermission('','r');
    fileIOPermission.assert();

    tempPath = WinAPIServer::getTempPath(); 

    CodeAccessPermission::revertAssert();
    filename = tempPath + _name + #xml;
}
else
{
   tempPath = WinAPI::getTempPath();

   filename = tempPath + _name + #xml;
}

tempPath = WinAPI::getTempPath();

tempPath = WinAPIServer::getTempPath();

谢谢大家