[YARG] How to create report template builder for webapp


Similar to my question here, I’d like to know how I’ll be able to instantiate a template builder when the template is deployed in a webapp.
I tried doing this:

        ReportTemplateBuilder reportTemplateBuilder = new ReportTemplateBuilder()

However, it seems from this code, it reads from the local file system:

   public ReportTemplateBuilder readFileFromPath() throws IOException {
        Preconditions.checkNotNull(reportTemplate.documentPath, "\"documentPath\" parameter is null. Can not load data from null path");
        reportTemplate.documentContent = FileUtils.readFileToByteArray(new File(reportTemplate.documentPath));
        return this;

My code after instantiating the template are the ff.:

        BandBuilder bandBuilder = new BandBuilder();
        ReportBand main= bandBuilder.name("Main").query("Main", "return [\n" +
                "                              [\n" +
                "                               'district':  'District 1',\n" +
                "                               'locale' : 'Chiba',\n" +
                "                               'forTheMonthOf' : 'For the Month of May 2016'\n" +
                "                            ]]", "groovy").build();

        Report report = reportBuilder.build();
        Reporting reporting = new Reporting();
        reporting.setFormatterFactory(new DefaultFormatterFactory());
                new DefaultLoaderFactory()
                        .setGroovyDataLoader(new GroovyDataLoader(new DefaultScriptingImpl())));

        ReportOutputDocument reportOutputDocument = reporting.runReport(new RunParams(report));
        byte[] contents = reportOutputDocument.getContent();

        HttpHeaders headers = new HttpHeaders();
        String filename = "liquidation_report.xlsx";
        headers.set("Content-Disposition", "attachment; filename=" + filename);
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");

        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
        return response;

This results to the ff. excel error:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
Excel found unreadable content in 'XXXXX.xlsx'
    <info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info>
  <removedParts summary="Removed parts:">
    <removedPart>Removed part: XML error in /xl/calcChain.xml .
Formula from /xl/calcChain.xml part (Calculation properties) fatal error. Location, line 1, column 421.</removedPart>
  <repairedParts summary="List of Repair">
    <repairedPart>Repaired Part: XML error in /xl/worksheets/sheet1.xml part. Fatal error. Line 1, Column 1268.</repairedPart>

I’m attaching the actual error as well (albeit it’s in Japanese). I just translated it here.




First of all, I need to say that you don’t have to use readFileFromPath if you already have document content - just set it to ReportTemplateBuilder with com.haulmont.yarg.structure.impl.ReportTemplateBuilder#documentContent method.

Could you please provide the template and the resulting file, and I will try to find the issue?

Thank you.

1 Like

Hello Eugene,

Thank you. Attached is my Controller, report template, and the resulting file(liquidation_report.xlsx).
Hope you can help me out.

liquidationReport1stPage.xlsx (12.4K)

YargReportController.java (7.2K)

liquidation_report.xlsx (9.5K)

Hello Eugene, hope you could help me out . I’m stuck. Thanks.