Class CsvWriter<T>
- Type Parameters:
T- The type of the data row
Supports building columns dynamically, writing to a file line-by-line, and handling basic CSV escaping (quotes, commas, line breaks).
- Since:
- 2025-07-19
-
Method Summary
Modifier and TypeMethodDescriptionafterData(CsvAfterDataWriter afterDataWriter) Registers a callback that writes custom content after all data rows.bom(boolean bom) Sets whether to write a UTF-8 BOM at the start of the file.Sets the character encoding for the output file.column(String name, RowFunction<T, @Nullable Object> function) Adds a new column to the CSV output using a row+cursor-based function.Adds a new column using a basic row-only function.columnIf(String name, boolean condition, RowFunction<T, @Nullable Object> function) Conditionally adds a column using a row+cursor-based function.Conditionally adds a column using a basic row-only function.constColumn(String name, @Nullable Object value) Adds a column with a constant value for all rows.constColumnIf(String name, boolean condition, @Nullable Object value) Conditionally adds a column with a constant value for all rows.static <T> CsvWriter<T> create()Creates a new CSV writer with default settings.csvInjectionDefense(boolean enabled) Enables or disables CSV injection defense.delimiter(char delimiter) Sets the delimiter character used to separate fields.dialect(CsvDialect dialect) Applies a predefined CSV dialect configuration.Creates a CsvWriter pre-configured to write rows ofMap<String, Object>, with one column per given column name.onProgress(int interval, ProgressCallback callback) Registers a progress callback that fires everyintervalrows.quoting(CsvQuoting quoting) Sets the quoting strategy for CSV field values.Writes the given stream of rows to a temporary CSV file.
-
Method Details
-
create
Creates a new CSV writer with default settings.Mirrors
ExcelWriter.create()for API symmetry.CsvWriter.<User>create() .column("Name", User::getName) .write(users) .writeTo(out);- Type Parameters:
T- the row type- Returns:
- a new CsvWriter
- Since:
- 0.16.6
-
forMap
Creates a CsvWriter pre-configured to write rows ofMap<String, Object>, with one column per given column name. Each column reads its value from the map by using the column name as the key.The returned writer is a regular
CsvWriter, so all of its fluent configuration methods (dialect, delimiter, charset, BOM, quoting, CSV injection defense, afterData, etc.) are available.CsvWriter.forMap("Name", "Age", "Email") .dialect(CsvDialect.EXCEL) .bom(true) .write(stream) .write(out);- Parameters:
columnNames- the column names (used as both header labels and map keys)- Returns:
- a new CsvWriter with the columns registered
- Since:
- 0.11.0
-
dialect
Applies a predefined CSV dialect configuration.Sets the delimiter, charset, and BOM settings in one call. Individual settings can be overridden after calling this method.
- Parameters:
dialect- the dialect to apply- Returns:
- This writer instance (for chaining)
- Since:
- 0.9.2
-
delimiter
Sets the delimiter character used to separate fields. Defaults to comma (',').- Parameters:
delimiter- The delimiter character- Returns:
- This writer instance (for chaining)
-
charset
Sets the character encoding for the output file. Defaults toStandardCharsets.UTF_8.- Parameters:
charset- The charset to use- Returns:
- This writer instance (for chaining)
-
bom
Sets whether to write a UTF-8 BOM at the start of the file. Defaults totrue.- Parameters:
bom- Whether to write the BOM- Returns:
- This writer instance (for chaining)
-
afterData
Registers a callback that writes custom content after all data rows.The callback receives the
PrintWriterused to write the CSV, allowing additional lines to be appended after the data rows.- Parameters:
afterDataWriter- the callback to invoke after data rows- Returns:
- This writer instance (for chaining)
-
column
Adds a new column to the CSV output using a row+cursor-based function.- Parameters:
name- The column headerfunction- A function to compute the value for each row- Returns:
- This writer instance (for chaining)
-
column
Adds a new column using a basic row-only function.- Parameters:
name- The column headerfunction- A function to compute the value from the row- Returns:
- This writer instance
-
columnIf
public CsvWriter<T> columnIf(String name, boolean condition, RowFunction<T, @Nullable Object> function) Conditionally adds a column using a row+cursor-based function. If condition is false, the column is not added.- Parameters:
name- The column headercondition- Whether to include this columnfunction- A function to compute the value for each row- Returns:
- This writer instance
-
columnIf
Conditionally adds a column using a basic row-only function. If condition is false, the column is not added.- Parameters:
name- The column headercondition- Whether to include this columnfunction- A function to compute the value from the row- Returns:
- This writer instance
-
constColumn
Adds a column with a constant value for all rows.- Parameters:
name- The column headervalue- The constant value- Returns:
- This writer instance
-
constColumnIf
Conditionally adds a column with a constant value for all rows.- Parameters:
name- The column headercondition- Whether to add the columnvalue- The constant value- Returns:
- This writer instance
- Since:
- 0.14.0
-
onProgress
Registers a progress callback that fires everyintervalrows.- Parameters:
interval- the number of rows between each callback invocation (must be positive)callback- the callback to invoke- Returns:
- This writer instance (for chaining)
-
quoting
Sets the quoting strategy for CSV field values.Defaults to
CsvQuoting.MINIMAL(quote only when necessary).- Parameters:
quoting- the quoting strategy- Returns:
- This writer instance (for chaining)
- Since:
- 0.9.2
-
csvInjectionDefense
Enables or disables CSV injection defense.When enabled (default), cell values starting with formula characters (
=,+,-,@,\t,\r) are prefixed with a single quote to prevent formula injection.Disable only when writing trusted data where the prefix would corrupt values.
- Parameters:
enabled- whether to enable injection defense (default: true)- Returns:
- This writer instance (for chaining)
-
write
Writes the given stream of rows to a temporary CSV file.The returned
CsvHandlercan be used to write the file to anOutputStream.- Parameters:
stream- The row data stream- Returns:
- A handler for streaming the resulting CSV
-