Ignore cells if datasource is empty

Posted by: nimish.g on 21 October 2021, 5:16 pm EST

  • Posted 21 October 2021, 5:16 pm EST

    When the list of accounts is empty grapecity throws a NPE.

    Expectation: I would like to know if there is a way to not show rows A2 to A4 if list of accounts is empty.


    PFA excel template and exception stacktrace present in zip file

    final List<Account> accounts=new ArrayList<>();
    final Map<String, Object> dataSourceMap = new HashMap<>();
    dataSourceMap.put("account", accounts);

    final Workbook workbook = new Workbook();
    workbook.open(templateFilePath);
    dataSourceMap.forEach((key, value) -> workbook.addDataSource(key, value));
    workbook.processTemplate();


    Class Account{
    String name;
    String List<String> statementNames;
    }
    grapecity.zip
  • Replied 21 October 2021, 7:59 pm EST

    Hello Nimish,

    You can put a check before calling the process template to check if the list is empty or not. If the list is empty, you can simply delete the particular range.

    if(!accounts.isEmpty()){
    workbook.processTemplate();
    }
    else{
    workbook.getActiveSheet().getRange("A2:A4").delete();
    workbook.save("templateSave.xlsx");
    }

    Regards,
    Prabhat Sharma.
  • Replied 21 October 2021, 8:19 pm EST

    Hi Prabhat,

    I was wondering if its possible to do in the excel template as i have other datasources for other sheets.
  • Replied 24 October 2021, 5:51 pm EST

    Hello Nimish,

    We are discussing this with the developers and will get back to you with the updates soon.
    [Internal Tracking Id: DOCXLS-5052]

    Regards,
    Prabhat Sharma.
  • Marked as Answer

    Replied 2 November 2021, 6:09 pm EST

    Hello,

    As per the developers, if the list is empty, you need to delete the particular range manually.

    Can you please share a stripped-down sample of the Empty data source so that the developers can see what you are doing exactly and see if they can add something in the API to avoid the exception?

    Regards,
    Prabhat Sharma.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels