-
Notifications
You must be signed in to change notification settings - Fork 287
ToText (Fixed width text files)
As of EPPlus 7.2 it can read and write fixed width text files. It supports two ways of doing it using either start positions of columns or the length of each column.
Now let's create a workbook with a worksheet and add this data into it using the ToText
method.
In this case ToText
will need an ExcelOutputTextFormatFixedWidth
where we enter the lengths of each column. This is the default behavior.
using (var p = new ExcelPackage())
{
var ws = p.Workbook.Worksheets.Add("Sheet1");
ExcelOutputTextFormatFixedWidth format = new ExcelOutputTextFormatFixedWidth();
format.SetColumnLengths(16, 10, 16, 8, 1);
ws.Cells["A1:E6"].ToText(myFile, format);
}
- This will write the contents from cell A1 to E6
- Numbers will be right aligned. Text will be left aligned
- By default the space character will be the padding character.
Now we will create a workbook with a worksheet and add data into it using ToText
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:E6"].LoadFromText(myFile, format);
}
- This will write the contents from cell A1 to E6
- Numbers will be right aligned. Text will be left aligned
- By default the space character will be the padding character.
You have the same properties when loading a fixed width text file as when saving, but you also have some extra properties as documented under the ToText article.
You can write fixed width file with trailing minus sign for numerics by setting the property. This will put the minus sign after the number in the column.
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
- 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
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles