Package: groovy.csv

## [Java] Class CsvBuilder

- groovy.csv.CsvBuilder

- All Implemented Interfaces and Traits:
    Writable

      @Incubating
      public class CsvBuilder
      extends Object
      implements Writable

  Builds CSV output from collections of maps or typed objects.

  Example with maps:

       def data = [[name: 'Alice', age: 30], [name: 'Bob', age: 25]]
       def csv = groovy.csv.CsvBuilder.toCsv(data)
       assert csv.contains('name,age')
       assert csv.contains('Alice,30')

  Since:
    6.0.0

- - ### Constructor Summary

    Constructors
    Constructor and description
    `CsvBuilder()` Creates a builder that uses comma-separated columns and double quotes for field escaping.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public CsvBuilder` | `call(Collection<? extends Map<String, ?>> data)` Build CSV from a collection of maps.
    `` | `public CsvBuilder` | `setQuoteChar(char quoteChar)` Set the quote character (default: double-quote).
    `` | `public CsvBuilder` | `setSeparator(char separator)` Set the column separator character (default: comma).
    `` | `public static String` | `toCsv(Collection<? extends Map<String, ?>> data)` Convert a collection of maps to CSV.
    `<T>` | `public static String` | `toCsv(Collection<T> data, Class<T> type)` Convert a collection of typed objects to CSV using Jackson databinding.
    `` | `public String` | `toString()` Returns the CSV content built so far, or an empty string if no data has been written yet.
    `` | `public Writer` | `writeTo(Writer out)` Writes the current CSV content to the supplied writer.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### public CsvBuilder()

      Creates a builder that uses comma-separated columns and double quotes for field escaping.

  - ### Method Detail

    - #### public CsvBuilder call(Collection<? extends Map<String, ?>> data)

      Build CSV from a collection of maps.

      Parameters:
        `data` - the collection of maps
      Returns:
        this builder

    - #### public CsvBuilder setQuoteChar(char quoteChar)

      Set the quote character (default: double-quote).

      Parameters:
        `quoteChar` - the quote character
      Returns:
        this builder for chaining

    - #### public CsvBuilder setSeparator(char separator)

      Set the column separator character (default: comma).

      Parameters:
        `separator` - the separator character
      Returns:
        this builder for chaining

    - #### public static String toCsv(Collection<? extends Map<String, ?>> data)

      Convert a collection of maps to CSV. The keys of the first map are used as column headers.

      Parameters:
        `data` - the collection of maps
      Returns:
        the CSV string

    - #### <T> public static String toCsv(Collection<T> data, Class<T> type)

      Convert a collection of typed objects to CSV using Jackson databinding. Supports `@JsonProperty` and `@JsonFormat` annotations. Columns for records appear in component declaration order; for other classes the order is alphabetical. Use `@JsonPropertyOrder` on the type to set an explicit column order.

      Parameters:
        `data` - the collection of objects
        `type` - the object type (used to derive the schema)
      Type Parameters:
        `T` - the object type
      Returns:
        the CSV string

    - #### public String toString()

      Returns the CSV content built so far, or an empty string if no data has been written yet.

      Returns:
        the current CSV content

    - #### public Writer writeTo(Writer out)

      Writes the current CSV content to the supplied writer.

      throws:
        IOException if the writer cannot be written to
      Parameters:
        `out` - the destination writer
      Returns:
        the writer passed in