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
This function is not available out-of-the-box, but you can easily build it yourself using the following utilities:
TableUtility
to convert rows and columns to a data matrix.CsvHelper
to generate CSV output from the data matrix.IDesktop#openUri
to send the output as a downloadable file to the browser.// 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);