Skip to main content

CSV

Process custom CSV files.

Introduction

Netuno generates CSV files through the CSV resource, which provides a low-code abstraction over Apache Commons CSV for the various programming languages supported by Netuno.

The application included with Netuno called demo (demonstration application) contains several examples of possible implementations in multiple programming languages, including a demonstration of CSV export through the csv service located at:

  • netuno/apps/demo/server/services/samples/javascript/csv.js

Creating a CSV file

To create a CSV file, you first need to initialize a CSVPrinter from Apache Commons CSV. To do this, you must provide the folder name and file name, and the CSV format. In this example, the file will be generated inside the application's storage:

const csvPrinter = _csv.printer(
_storage.filesystem("server", "data.csv"),
_csv.format("EXCEL")
)

The supported formats are described here.

Writing

To write rows to the CSV file:

csvPrinter.printRecord("id", "userName", "firstName", "lastName", "birthday")
csvPrinter.printRecord(1, "john73", "John", "Doe", 123)

Closing

When finished, you must close the CSVPrinter:

csvPrinter.close()

Reading a CSV file

To read a CSV file, you first need to initialize a CSVParser from Apache Commons CSV. To do this, you must provide the folder name and the file name. In this example, the file is read from the application's storage:

const csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))

Rows

You can now iterate through the CSVRecord objects from Apache Commons CSV, each representing a row in the CSV file:

for (const record of csvParser) {
const columnOne = record.get(0)
const columnTwo = record.get(1)
}

Alternatively, you can retrieve each row using the getRecords method from Apache Commons CSV. In this example, the first get retrieves the first row. The second get retrieves the first field/value of that row:

csvParser.getRecords().get(0).get(0)

Closing

When finished, you must close the CSVParser:

csvParser.close()