Skip to main content

CSV

Process custom PDF files.

Introduction​

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

The application that comes with Netuno called demo (demonstration application) contains many examples of possible code implementations in various languages, including the demonstration of export to CSV through the csv service located at:

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

Create a CSV file​

To create a CSV file, first you need to initialize a CSVPrinter from Apache Commons CSV. To do this, we need to pass the folder name and the file name, as well as the CSV format. In the example, we will generate the file 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​

At the end, you need to close the CSVPrinter:

csvPrinter.close()

Read a CSV file​

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

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

Rows​

Now it is possible to iterate over the CSVRecord from Apache Commons CSV, which represent each row of the CSV file:

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

Or get each row with the getRecord method from Apache Commons CSV. In this example, the first get gets the first row. The second get gets the first field/value of that row:

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

Closing​

At the end, you need to close the CSVParser:

csvParser.close()