Skip to content

LoadFromText

Mats Alm edited this page Jul 20, 2020 · 20 revisions

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.

a,2,3%
d,3,5%

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.

using (var pck = new ExcelPackage())
{
    var sheet = pck.Workbook.Worksheets.Add("sheet");
    var file = new FileInfo("c:\\temp\\my_file.txt");
    sheet.Cells["C1"].LoadFromText(file);
}

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.

var format = new ExcelTextFormat();
// change delimiter to ';'
format.Delimiter = ';';
sheet.Cells["C1"].LoadFromText(file, format);

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 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);

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally