**This method exports a range in a worksheet to a text file (\*.csv, \*.tsv, \*.txt, etc). You can configure the format of the file content by using the ExcelOutputTextFormat class as input parameter. This method generates the same output as the [ToText](<https://epplussoftware.com/docs/import-export-data/export/totext/index.md>) method, but sends the output to a file instead of a string. You can also let the method send its output to any Stream of your choice.**

### Basic usage

Let's create a worksheet and add some values to the cells:

```csharp
using(var package = new ExcelPackage())
{
    var sheet = package.Workbook.Worksheets.Add("test");
    sheet.Cells["A1"].Value = 1;
    sheet.Cells["B1"].Value = 2;
    sheet.Cells["A2"].Value = 3;
    sheet.Cells["B2"].Value = 4;
}
```

```vbnet
Using package As New ExcelPackage()
    Dim sheet = package.Workbook.Worksheets.Add("test")
    sheet.Cells("A1").Value = 1
    sheet.Cells("B1").Value = 2
    sheet.Cells("A2").Value = 3
    sheet.Cells("B2").Value = 4
End Using
```

...and then call the `SaveToText` method:

```csharp
// the output file
var file = new FileInfo(@"c:\myCsvFile.txt");
// format with default parameters
var format = new ExcelOutputTextFormat();
sheet.Cells["A1:B2"].SaveToText(file, format);
```

```vbnet
' the output file
Dim file = New FileInfo("c:\myCsvFile.txt")
' format with default parameters
Dim format As New ExcelOutputTextFormat()
sheet.Cells("A1:B2").SaveToText(file, format)
```

The content of the file will be "1,2\\r\\n3,4";

### ExcelOutputTextFormat

An instance of this class is sent in as a parameter to the `SaveToText` method.

```csharp
var format = new ExcelOutputTextFormat{
    TextQualifier = '\''
};
sheet.Cells["A1:B2"].SaveToText(file, format);
```

```vbnet
Dim format As New ExcelOutputTextFormat With {
    .TextQualifier = "'"c
}
sheet.Cells("A1:B2").SaveToText(file, format)
```

The `ExcelOutputTextFormat` class has the following properties:

| Property | Datatype | Default value | Description |
| :--- | :--- | :--- | :--- |
| `Delimiter` | `char` | `','` (comma) | Delimiter character |
| `TextQualifier` | `char` | `'\0'` (null) | A character that encapsulates text |
| `EOL` | `string` | `"\r\n"` | End of line characters |
| `Culture` | `CultureInfo` | `CultureInfo.InvariantCulture` | Culture used when parsing cell values |
| `SkipLinesBeginning` | `int` | 0 | Number of lines skipped at the beginning |
| `SkipLinesEnd` | `int` | 0 | Number of lines skipped at the end |
| `Encoding` | `Encoding` | `Encoding.ASCII` | Only used when reading/writing files from disk using a `FileInfo` object |
| `Header` | `string` | `null` | A text written at the start of the content |
| `Footer` | `string` | `null` | A text written at the end of the content |
| `FirstRowIsHeader` | `bool` | `true` | First row of the range contains the headers. All header cells will be treated as strings |
| `UseCellFormat` | `bool` | `true` | Use the cells `Text` property with the applied culture. This only applies to columns with no format set in the `Formats` collection. If `SkipLinesBeginning` (see above) is larger than zero, headers will still be read from the first row in the range. If a `TextQualifier` (see above) is set, non numeric and date columns will be wrapped with the TextQualifier. |
| `Formats` | `string[]` | `null` | A specific .NET format for the column. Format is applied with the used culture. For a text column use $ as format. |
| `DecimalSeparator` | `string` | `null` | Decimal separator, if other than the used culture. |
| `ThousandsSeparator` | `string` | `null` | Thousands separator, if other than the used culture |
| `EncodedTextQualifier` | `string` | `null` | What to replace the `TextQualifier` with inside a text when `TextQualifier` is set. Default is two TextQualifier characters, for example " is replaced with "" |
| `Transpose` | `bool` | `false` | Transpose data on export |

## See also

- [Sample 2.3 (C\#)](<https://github.com/EPPlusSoftware/EPPlus.Samples.CSharp/tree/master/02-Import and export/03-Import export csv files>) or [Sample 2.3 (VB.NET)](<https://github.com/EPPlusSoftware/EPPlus.Samples.VB/tree/master/02-Import and export/03-Import export csv files>)
- [Export data](<https://epplussoftware.com/docs/import-export-data/export/index.md>)
