Skip to content
Mats Alm edited this page Sep 13, 2024 · 16 revisions

This method exports a range in a worksheet to a string. You can configure the format of this content by using the ExcelOutputTextFormat class as input parameter. This method generates the same output as the SaveToText method, which sends the output to a file instead of a string.

Basic usage

Lets create a worksheet and add some values to the cells:

using(varpackage=newExcelPackage()){varsheet=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;}

...and then call the ToText method:

varcontent=sheet.Cells["A1:B2"].ToText();

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

ExcelOutputTextFormat

An instance of this class can be sent in as a parameter to the ToText method.

varformat=newExcelOutputTextFormat{TextQualifier='\''};varcontent=sheet.Cells["A1:B2"].ToText(format);

The ExcelOutputTextFormat class has the following properties:

PropertyDatatypeDefault valueDescription
Delimiterchar',' (comma)Delimiter character
TextQualifierchar'\0' (null)A character that encapsulates text
EOLstring"\r\n"End of line characters
CultureCultureInfoCultureInfo.InvariantCultureCulture used when parsing cell values
SkipLinesBeginningint0Number of lines skipped at the end
SkipLinesEndint0Number of lines skipped at the end
EncodingEncodingEncoding.ASCIIOnly used when reading/writing files from disk using a FileInfo object
HeaderstringnullA text written at the start of the content
FooterstringnullA text written at the start of the content
FirstRowIsHeaderbooltrueFirst row of the range contains the headers. All header cells will be treated as strings
UseCellFormatbooltrueUse 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.
Formatsstring[]nullA specific .NET format for the column. Format is applied with the used culture. For a text column use $ as format.
DecimalSeparatorstringnullDecimal separator, if other than the used culture.
ThousandsSeparatorstringnullThousands separator, if other than the used culture
EncodedTextQualifierstringnullWhat to replace the TextQualifier with inside a text when TextQualifier is set. Default is two TextQualifier characters, for example " is replaced with ""
TransposeboolfalseTranspose data on export

See also

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally