Question

Generation of report programmatically fails for Excel files

  • 14 September 2023
  • 1 reply
  • 55 views

Good day, 

In a customisation package I have to buttons on the Invoices & Memos (AR301000) screen - these buttons generate a report and attaches it as a file to the invoice or memo. 

This is the code to generate a PDF which works perfectly. 


#region Action Button - Export PDF Report
public PXAction<ARInvoice> ExportReport;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Export PDF Report", Enabled = true)]
protected void exportReport()
{

// Report Paramenters
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters["ARInvoice.DocType"] = Base.Document.Current.DocType;
parameters["ARInvoice.RefNbr"] = Base.Document.Current.RefNbr;
var filename = Base.Document.Current.DocType + "_" + Base.Document.Current.RefNbr;

// Report Processing
Report _report = ReportLoader.LoadReport("AR641000", null);
ReportLoader.InitDefaultReportParameters(_report, parameters);

// Read Report
byte[] data = null;
using (StreamManager streamMgr = new StreamManager())
{
ReportRenderer.Render(RenderType.FilterPdf, _report, null, streamMgr);
data = streamMgr.MainStream.GetBytes();
}

//Generation PDF
PX.SM.FileInfo file = new PX.SM.FileInfo(filename + ".pdf", null, data);

//Saving report to Invoice as Attachment
UploadFileMaintenance graph = new UploadFileMaintenance();
graph.SaveFile(file);
PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, file);


}
#endregion

Here is the code that generates the Excel version


#region Action Button - Export Excel Report
public PXAction<ARInvoice> ExportReportExcel;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Export Excel Report", Enabled = true)]
protected void exportReportExcel()
{
// Report Paramenters
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters["ARInvoice.DocType"] = Base.Document.Current.DocType;
parameters["ARInvoice.RefNbr"] = Base.Document.Current.RefNbr;
var filename = Base.Document.Current.DocType + "_" + Base.Document.Current.RefNbr;

// Report Processing
Report _report = ReportLoader.LoadReport("AR641000", null);
ReportLoader.InitDefaultReportParameters(_report, parameters);

// Read Report
byte[] data = null;
using (StreamManager streamMgr = new StreamManager())
{
ReportRenderer.Render(RenderType.FilterExcel, _report, null, streamMgr);
data = streamMgr.MainStream.GetBytes();
}

//Generation Excel
PX.SM.FileInfo file = new PX.SM.FileInfo(filename + ".xlsx", null, data);

//Saving report to Invoice as Attachment
UploadFileMaintenance graph = new UploadFileMaintenance();
graph.SaveFile(file);
PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, file);


}
#endregion

The excel version generates a report, but the data is either corrupt or cut off. 
When I run the report from the AR641000 screen, it works fine.

For example, the excel report generated from the report screen is 21kb while the one generated from the button is only 7kb. 

Has any one experienced this before or know of a bug? Any help is greatly appreciated. 

Thanks
 


1 reply

Userlevel 6
Badge +3

It looks like a bug for me. I did some testing, and realized ReportRenderer only renders the first page of the report into the excel file.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved