-
Notifications
You must be signed in to change notification settings - Fork 297
LoadFromText (Fixed width text files)
As of EPPlus 7.2 it can read and write fixed width text files. It supports two ways of doing ut using either start positions of columns or the length of each column.
In these examples we will specify input data like below. Data can be a string or content of a text file.
Name Date Ammount Percent Category
David 2024/03/02 130000 2% A
Meryl 2024/02/15 999 10% B
Hal 2005/11/24 0 0% A
Frank 1988/10/12 40,00 59% C
Naomi 2015/09/03 245000,99 100% C
Now let's create a workbook with a worksheet and add this data into it using the LoadFromText method. We will use the length of each column in this example.
using (var p = new ExcelPackage())
{
var ws = p.Workbook.Worksheets.Add("Sheet1");
ExcelTextFormatFixedWidth format = new ExcelTextFormatFixedWidth();
format.SetColumnLengths(16, 10, 16, 8, 1);
ws.Cells["A1"].LoadFromText(myFile, format);
}- The value "David" will be written to address A2.
- The third value 130000 will be converted to a number since it is numeric, and will be written into D1.
- In this example, the header row will be included and since the last column has a longer header name than data, it will be truncated.
Now we will create a workbook with a workshees and add data into it using LoadFromText method, but this time we will use positions.
using (var p = new ExcelPackage())
{
var ws = p.Workbook.Worksheets.Add("Sheet1");
ExcelTextFormatFixedWidth format = new ExcelTextFormatFixedWidth();
format.SetColumnPositions((51, 0, 16, 26, 42, 50);
format.ReadType = FixedWidthReadType.Positions;
ws.Cells["A1"].LoadFromText(myFile, format);
}Just as before we will have the same contents of each cell as the previous example. Somethings to note:
- We need to provide the length of a line in the fixed width file.
- If setting the lineLength to 0 or a negative number we will read until the end of the line (Which in this case would write Category in full instead of just the first letter in the column header).
You can configure more behaviours by overriding the properties in the ExcelTextFormatFixedWidth class.
A list containing each column. You can use this to access parameters for each column.
There are two ways to handle errors when reading and writing fixed width text files, ThrowError or Truncate. ThrowError will throw an error if data read has a length greater than the length specified for example. Truncate will ignore errors and write data anyway. This can however lead to loss of some data.
The character which is used for padding the column when saving, or the character to trim when loading. Deafult is a space character.
The character which is used for padding numberd in the column when saving, or the character to trim when loading a number. Deafult is null and numerics will use PaddingCharacter instead.
You can supply a name for the column when loading a text. You can use SetColumnsNames provding names for each column to set them or you can use the ColumnFormats list and set them individually.
format.SetColumnNames("Name", "Date", "Ammount", "Percent", "Category");You can override how EPPlus converts data by specifying what data types should be used for each item in a row
format.SetColumnDataTypes(eDataTypes.String, eDataTypes.DateTime, eDataTypes.Number, eDataTypes.Percent, eDataTypes.String);You can override how EPPlus aligns numbers and text by specifing a padding type. when writing a fixed width text file. By default it is set to auto and strings will be aligned to the left, and numbers to the right.
format.SetColumnPaddingAlignmentType(PaddingAlignmentType.Left, PaddingAlignmentType.Auto, PaddingAlignmentType.Right, PaddingAlignmentType.Right, PaddingAlignmentType.Auto);You can specify to exclude certain columns when reading or writing a fixed width text.
format.SetUseColumns(true, false, true, false, false);Check if row should be included based on user provided functionality. This check is done on the whole row.
format.ShouldUseRow = row =>
{
if (row.Contains("Hal") || string.IsNullOrEmpty(row))
{
return false;
}
return true;
};This will result in that all rows containing the word Hal will be skipped when reading.
You can make LoadFromText skip a number of lines in the beginning and/or in the end:
// will ignore the first line
format.SkipLinesBeginning = 1;
// will ignore the last two lines
format.SkipLinesEnd = 2;The third argument - TableStyle - gives you the possibility to style the range as a table.
sheet.Cells["C1"].LoadFromText(file, format, TableStyles.Dark1);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
- The ExcelRange.Text property
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles