javaeclipseexport-to-csveclipse-scout

Export Eclipse Scout table rows as a CSV file and let user download the file


I would like to export the rows (all rows, as well as just filtered rows) of an Eclipse Scout table as a CSV file. And after creation of this file, I would like the user to be able to download it.

I see in the ITable interface of the framework, a method called exportTableRowsAsCSV, but I cannot find examples of its usage.

Thanks a lot for your kind assistance.

JDaniel


Solution

  • This function is not available out-of-the-box, but you can easily build it yourself using the following utilities:

    // Select data to export
    Table table = getTable();
    List<ITableRow> rows = table.getFilteredRows();
    List<IColumn<?>> columns = table.getColumnSet().getVisibleColumns();
    
    // Convert to data matrix (with column headers)
    Object[][] csvData = TableUtility.exportRowsAsCSV(rows, columns, true, false, false);
    
    // Write data to byte array
    CsvHelper csvHelper = new CsvHelper(NlsLocale.get(), ";", "\"", "\n");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
      csvHelper.exportData(csvData, writer, null, false, null, false);
    }
    catch (IOException e) {
      throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
    }
    
    // Download as file
    BinaryResource binaryResource = new BinaryResource("export.csv", out.toByteArray());
    IDesktop.CURRENT.get().openUri(binaryResource);