LoadFromText
Loads data from a text file or string content into the worksheet. The method supports various formats as specified below.
Basic usage¶
In these examples we will specify input data like below. Data can either be a string or content of a text file.
Now let's create a workbook with a worksheet and add this data into it using the LoadFromText method. In this case LoadFromText will use the default delimiters - comma for separating items in a row and "\r\n" for newline.
With the default behaviour the first row will be stored like this:
- The value "a" will be written to address C1.
- The second value 2 will be converted to a number, since it is numeric and written into D1.
- The third value will be written into E1 as 0.03 since it ends with a '%'.
ExcelTextFormat¶
By supplying an instance of the ExcelTextFormat class you can configure the behaviour of the LoadFromText function.
Data types¶
You can override how EPPlus converts the data by specifying what data types should be used for each item in a row.
var format = new ExcelTextFormat();
// specify the data type for each corresponding column position in a row
// with the configuration below all three items will be written into the worksheet as strings.
format.DataTypes = new eDataTypes[] { eDataTypes.String, eDataTypes.String, eDataTypes.String};
sheet.Cells["C1"].LoadFromText(file, format);
Dim format As New ExcelTextFormat()
' specify the data type for each corresponding column position in a row
' with the configuration below all three items will be written into the worksheet as strings.
format.DataTypes = New eDataTypes() {eDataTypes.String, eDataTypes.String, eDataTypes.String}
sheet.Cells("C1").LoadFromText(file, format)
NOTE: Date's and number's data types will be parsed according to the culture set in the ExcelTextFormat.CultureInfo property.
Culture/Encoding¶
You can configure these via properties on the ExcelTextFormat class. Default Culture is InvariantCulture and default encoding is ASCII.
TextQualifier¶
The TextQualifier is a character used for defining a string/block of text in a row. The default value is null which means that you must specify this if you need it.
Input:
Set the TextQualifier to parse this input correctly:
Skipping lines¶
You can make LoadFromText skip a number of lines in the beginning and/or in the end:
Transpose¶
You can load the data in a transposed format by setting the Transpose property:
Table style¶
The third argument - TableStyle - gives you the possibility to style the range as a table.
The TableStyles enum gives you over 60 different table styles to pick from!
Headers¶
If the first row of the content should be used as headers in the range, set the bool argument FirstRowIsHeader to true.
Given this input:
...you can call LoadFromText like below to get headers for each column in the range from the first row: