-
Notifications
You must be signed in to change notification settings - Fork 293
LoadFromText
Loads data from a text file or string content into the worksheet. The method supports various formats as specified below.
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 '%'.
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);
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 Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles