简体   繁体   English

如何在 Dynamics AX2012 中使用代码 x++ 创建一个 Excel 文件及其 Email?

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

I need to create an Excel file and to send it by Email, by X++ code in to Dyncamics AX2012.我需要创建一个 Excel 文件并通过 X++ 代码通过 Email 将其发送到 Dyncamics AX2012。 If it's possible I don't want to save the data in a physical path/folder.如果可能的话,我不想将数据保存在物理路径/文件夹中。

I wrote this code, but after I need to send the Excel.我写了这段代码,但在我需要发送 Excel 之后。

I would like to follow the D365 instructions ( https://axexplorer.wordpress.com/2017/07/18/create-an-excel-file-and-send-it-through-e-mail-in-d365-for-operationax-7/ )in Dynamics AX 2012 (In AX2012 some command are not available)我想按照 D365 说明( https://axexplorer.wordpress.com/2017/07/18/create-an-excel-file-and-send-it-through-e-mail-in-d365-for -operationax-7/ )在Dynamics AX 2012中(在 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);

I'm not able to save the file in temporary stream.我无法将文件保存在临时 stream 中。

In AX2012 are not available the在 AX2012 中不可用

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

//The below line used to attach excel file to email. mailer.addAttachment(memoryStream, 'MYFILE.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //以下行用于将 excel 文件附加到 email。mailer.addAttachment(memoryStream, 'MYFILE.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

There is any way in Dynamics AX2012 to create an excel file and send the file to email (withouth saving in to the path). Dynamics AX2012 中有任何方法可以创建 excel 文件并将文件发送到 email(无需保存到路径中)。

Thanks in advance.提前致谢。

Following the @Alex Kwitny kind comment, I used this code:在@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 = WinAPI::getTempPath();

tempPath = WinAPIServer::getTempPath(); tempPath = WinAPIServer::getTempPath();

Thanks all.谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM