CsvExtensionSaveCsv Method

Saves the contents of data as a CSV file.

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public static void SaveCsv(
	this IEnumerable<IEnumerable<Object?>?> data,
	string filePath,
	char delimiter = ',',
	IFormatProvider? formatProvider = null,
	Encoding? textEncoding = null,
	string? format = null
)

Parameters

data  IEnumerableIEnumerableObject
The data to save.
filePath  String
The file path of the CSV file to be written.
delimiter  Char  (Optional)
The field separator character.
formatProvider  IFormatProvider  (Optional)

The provider to use to format the value.

- or -

A null reference for InvariantCulture.

textEncoding  Encoding  (Optional)
The Encoding to be used or null for UTF8.
format  String  (Optional)

A format String to use for all items that implement IFormattable.

- or -

A null reference to use the default format for each item.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableIEnumerableObject. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

Creates a new CSV file. If the target file already exists, it is truncated and overwritten.

For serialization ToString(String, IFormatProvider) is used if the item implements IFormattable, otherwise ToString.

When exchanging CSV data with Excel, the appropriate arguments can be determined with GetExcelArguments.

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.
C#
using FolkerKinzel.CsvTools;

namespace Examples;

internal static class LinqOnCsvExample
{
    public static void LinqOnCsv(string filePath)
    {
        object?[][] customers =
            [
                ["Customer", "PhoneNumbers", "Sales"],
                ["Detlef",    null,            3.85m],
                ["Susi",     """
                             0177-4711,
                             123-4567
                             """,         39_457.26m],
                ["Gabi",    "08-15",      28.70m]
            ];

        customers.SaveCsv(filePath);

        Console.WriteLine(File.ReadAllText(filePath));

        using CsvReader csv = Csv.OpenRead(filePath);

        Console.WriteLine();
        Console.WriteLine("What phone numbers does Susi have?:");
        Console.WriteLine(
            csv.FirstOrDefault(x => x["Customer"].Span.Equals("Susi", StringComparison.Ordinal))?["PhoneNumbers"]);
    }
}
/*
Console Output:

Customer,PhoneNumbers,Sales
Detlef,,3.85
Susi,"0177-4711,
123-4567",39457.26
Gabi,08-15,28.70

What phone numbers does Susi have?:
0177-4711,
123-4567
*/

Exceptions

ArgumentNullExceptiondata or filePath is null.
ArgumentExceptionfilePath is not a valid file path.
ArgumentOutOfRangeExceptiondelimiter is either the double quotes " or a line break character ('\r' or '\n').
IOExceptionI/O error.

See Also