Ignore cells if datasource is empty

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

    • Post Options:
    • Link

    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;
    }
    
    ```[zip filename="grapecity.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-fdfaa484-f38c-488f-9799-d9e87378a872.zip[/zip]
  • Posted 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.

  • Posted 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.

  • Posted 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.

  • Posted 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.

  • Posted 15 April 2024, 9:25 pm EST

    Hello,

    We are glad to inform you that the issue has been fixed in the 7.1.0 version of DsExcel API. Please upgrade to the latest version to resolve this issue:

    https://www.nuget.org/packages/DS.Documents.Excel/7.1.0

    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