Ignore cells if datasource is empty

Posted by: nimish.g on 22 October 2021, 2:16 am EST

    • Post Options:
    • Link

    Posted 22 October 2021, 2:16 am 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 22 October 2021, 4:59 am 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 22 October 2021, 5:19 am EST

    Hi Prabhat,

    I was wondering if its possible to do in the excel template as i have other datasources for other sheets.

  • Posted 25 October 2021, 2:51 am 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 3 November 2021, 3:09 am 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 16 April 2024, 6:25 am 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