Skip to main content
Solved

Generation of report programmatically fails for Excel files


DewaldH
Jr Varsity II

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
 

Best answer by Dmitrii Naumov

@DewaldH @Zoltan Febert 

You need to provide deviceInfo parameter.

 

var deviceInfo = new System.Collections.Hashtable();

deviceInfo[PX.Reports.Web.WebReport.Keys.PageNumber] = -1;
deviceInfo[PX.Reports.Web.WebReport.Keys.StartPage] = -1;
deviceInfo[PX.Reports.Web.WebReport.Keys.EndPage] = -1;

ReportRenderer.Render(format, reportNode, deviceInfo, manager);

 

View original
Did this topic help you find an answer to your question?

2 replies

Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3
  • Jr Varsity I
  • 175 replies
  • September 15, 2023

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.


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+6
  • Acumatica Moderator
  • 592 replies
  • Answer
  • September 11, 2024

@DewaldH @Zoltan Febert 

You need to provide deviceInfo parameter.

 

var deviceInfo = new System.Collections.Hashtable();

deviceInfo[PX.Reports.Web.WebReport.Keys.PageNumber] = -1;
deviceInfo[PX.Reports.Web.WebReport.Keys.StartPage] = -1;
deviceInfo[PX.Reports.Web.WebReport.Keys.EndPage] = -1;

ReportRenderer.Render(format, reportNode, deviceInfo, manager);

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings