Excel 文件生成 - API/Methods 公开?
Excel file generation - API/Methods exposed?
Acumatica 显然能够从系统中的不同位置创建 excel 文件。
用于生成 excel 文件的方法是否偶然公开并且可以在标准网格、报告、导入服务之外使用?
我需要能够从我的一个操作中生成一个 excel 文件并将其作为文件附件附加。如果可能的话,我更愿意使用内置的 excel 方法来简化操作,然后再沿着包含 EPPlus dll 或类似文件的路线前进。
感谢您提供任何信息
您可以利用 PX.Export.Excel.Core.Package
生成 Excel
文件。
生成的 Excel 可以使用 PXRedirectToFileException
重定向例外下载,也可以使用通知模板和 TemplateNotificationGenerator
.
作为电子邮件附件发送
using System;
using System.Linq;
using System.Text;
using PX.Objects.SO;
using PX.Objects.CR;
using PX.Common;
using PX.Data;
using PX.SM;
using System.IO;
namespace PXDemoPkg
{
public class SOOrderEntryPXExt : PXGraphExtension<SOOrderEntry>
{
public PXAction<SOOrder> ExportToExcelAndSendEmailAttachment;
[PXUIField(DisplayName = "Export To Excel And Send Email Attachment", MapViewRights = PXCacheRights.Select, MapEnableRights = PXCacheRights.Update)]
[PXButton]
protected virtual void exportToExcelAndSendEmailAttachment()
{
if (Base.Document.Current == null ||
Base.Document.Cache.GetStatus(Base.Document.Current) == PXEntryStatus.Inserted) return;
var excel = new PX.Export.Excel.Core.Package();
var sheet = excel.Workbook.Sheets[1];
//Add Header
sheet.Add(1, 1, "Line #");
sheet.Add(1, 2, "Transaction Description");
sheet.Add(1, 3, "Ordered Quantity");
//Add Data
var index = 2;
foreach (PXResult<SOLine> lineItem in Base.Transactions.Select())
{
SOLine dataRow = (SOLine)lineItem;
sheet.Add(index, 1, Convert.ToString(dataRow.LineNbr));
sheet.Add(index, 2, dataRow.TranDesc);
sheet.Add(index, 3, Convert.ToString(dataRow.OrderQty));
index++;
}
sheet.SetColumnWidth(1, 20);
sheet.SetColumnWidth(2, 45);
sheet.SetColumnWidth(3, 35);
//ExportFile(excel);
SendEmail(excel);
}
//To download generated Excel
private void ExportFile(PX.Export.Excel.Core.Package excel)
{
using (MemoryStream stream = new MemoryStream())
{
excel.Write(stream);
string path = String.Format("SO-{0}-Transaction Info.xlsx", Base.Document.Current.OrderNbr);
var info = new PX.SM.FileInfo(path, null, stream.ToArray());
throw new PXRedirectToFileException(info, true);
}
}
//Email generated Excel as an attachment
private void SendEmail(PX.Export.Excel.Core.Package excel)
{
bool sent = false;
//Notiftcaion with name "SOTransactionInfo" should be created via screen SM204003 prior to using this code.
Notification rowNotification = PXSelect<Notification,
Where<Notification.name,
Equal<Required<Notification.name>>>>
.Select(Base, "SOTransactionInfo");
if (rowNotification == null)
throw new PXException("Notification Template for is not specified.");
var sender = PX.Objects.EP.TemplateNotificationGenerator.Create(Base.Document.Current,
rowNotification.NotificationID.Value);
sender.MailAccountId = rowNotification.NFrom.HasValue ?
rowNotification.NFrom.Value :
PX.Data.EP.MailAccountManager.DefaultMailAccountID;
sender.To = "demo@demo.com";
//Attach Excel
using (MemoryStream stream = new MemoryStream())
{
excel.Write(stream);
string path = String.Format("SO-{0}-Transaction Info.xlsx", Base.Document.Current.OrderNbr);
sender.AddAttachment(path, stream.ToArray());
}
sent |= sender.Send().Any();
}
}
}
Acumatica 显然能够从系统中的不同位置创建 excel 文件。
用于生成 excel 文件的方法是否偶然公开并且可以在标准网格、报告、导入服务之外使用?
我需要能够从我的一个操作中生成一个 excel 文件并将其作为文件附件附加。如果可能的话,我更愿意使用内置的 excel 方法来简化操作,然后再沿着包含 EPPlus dll 或类似文件的路线前进。
感谢您提供任何信息
您可以利用 PX.Export.Excel.Core.Package
生成 Excel
文件。
生成的 Excel 可以使用 PXRedirectToFileException
重定向例外下载,也可以使用通知模板和 TemplateNotificationGenerator
.
using System;
using System.Linq;
using System.Text;
using PX.Objects.SO;
using PX.Objects.CR;
using PX.Common;
using PX.Data;
using PX.SM;
using System.IO;
namespace PXDemoPkg
{
public class SOOrderEntryPXExt : PXGraphExtension<SOOrderEntry>
{
public PXAction<SOOrder> ExportToExcelAndSendEmailAttachment;
[PXUIField(DisplayName = "Export To Excel And Send Email Attachment", MapViewRights = PXCacheRights.Select, MapEnableRights = PXCacheRights.Update)]
[PXButton]
protected virtual void exportToExcelAndSendEmailAttachment()
{
if (Base.Document.Current == null ||
Base.Document.Cache.GetStatus(Base.Document.Current) == PXEntryStatus.Inserted) return;
var excel = new PX.Export.Excel.Core.Package();
var sheet = excel.Workbook.Sheets[1];
//Add Header
sheet.Add(1, 1, "Line #");
sheet.Add(1, 2, "Transaction Description");
sheet.Add(1, 3, "Ordered Quantity");
//Add Data
var index = 2;
foreach (PXResult<SOLine> lineItem in Base.Transactions.Select())
{
SOLine dataRow = (SOLine)lineItem;
sheet.Add(index, 1, Convert.ToString(dataRow.LineNbr));
sheet.Add(index, 2, dataRow.TranDesc);
sheet.Add(index, 3, Convert.ToString(dataRow.OrderQty));
index++;
}
sheet.SetColumnWidth(1, 20);
sheet.SetColumnWidth(2, 45);
sheet.SetColumnWidth(3, 35);
//ExportFile(excel);
SendEmail(excel);
}
//To download generated Excel
private void ExportFile(PX.Export.Excel.Core.Package excel)
{
using (MemoryStream stream = new MemoryStream())
{
excel.Write(stream);
string path = String.Format("SO-{0}-Transaction Info.xlsx", Base.Document.Current.OrderNbr);
var info = new PX.SM.FileInfo(path, null, stream.ToArray());
throw new PXRedirectToFileException(info, true);
}
}
//Email generated Excel as an attachment
private void SendEmail(PX.Export.Excel.Core.Package excel)
{
bool sent = false;
//Notiftcaion with name "SOTransactionInfo" should be created via screen SM204003 prior to using this code.
Notification rowNotification = PXSelect<Notification,
Where<Notification.name,
Equal<Required<Notification.name>>>>
.Select(Base, "SOTransactionInfo");
if (rowNotification == null)
throw new PXException("Notification Template for is not specified.");
var sender = PX.Objects.EP.TemplateNotificationGenerator.Create(Base.Document.Current,
rowNotification.NotificationID.Value);
sender.MailAccountId = rowNotification.NFrom.HasValue ?
rowNotification.NFrom.Value :
PX.Data.EP.MailAccountManager.DefaultMailAccountID;
sender.To = "demo@demo.com";
//Attach Excel
using (MemoryStream stream = new MemoryStream())
{
excel.Write(stream);
string path = String.Format("SO-{0}-Transaction Info.xlsx", Base.Document.Current.OrderNbr);
sender.AddAttachment(path, stream.ToArray());
}
sent |= sender.Send().Any();
}
}
}