Excel Fixed Columns Width Text in C# and VB.NET

A fixed width text file is a text file where each column can hold a fixed number of characters. There is no need for field delimiters or text quoting. If a value is smaller than the specified column width, it is simply padded with space characters to fill the remaining space. With GemBox.Spreadsheet you can read and write these kinds of text files programmatically.

The following example shows how to read and write a fixed width text file in C# and VB.NET.

Fixed-columns-width text file modified with GemBox.Spreadsheet
Screenshot of Fixed-columns-width text file modified with GemBox.Spreadsheet
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        // Define columns width (for input file format).
        var loadOptions = new FixedWidthLoadOptions(
            new FixedWidthColumn(8),
            new FixedWidthColumn(8),
            new FixedWidthColumn(8));

        // Load file.
        var workbook = ExcelFile.Load("%#FixedColumnsWidthText.prn%", loadOptions);

        // Modify file.
        workbook.Worksheets.ActiveWorksheet.GetUsedCellRange(true).Sort(false).By(1).Apply();

        // Define columns width (for output file format).
        var saveOptions = new FixedWidthSaveOptions(
            new FixedWidthColumn(8),
            new FixedWidthColumn(8),
            new FixedWidthColumn(8));

        workbook.Save("Fixed Columns Width Text.prn", saveOptions);
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        ' Define columns width (for input file format).
        Dim loadOptions As New FixedWidthLoadOptions(
            New FixedWidthColumn(8),
            New FixedWidthColumn(8),
            New FixedWidthColumn(8))

        ' Load file.
        Dim workbook = ExcelFile.Load("%#FixedColumnsWidthText.prn%", loadOptions)

        ' Modify file.
        workbook.Worksheets.ActiveWorksheet.GetUsedCellRange(True).Sort(False).By(1).Apply()

        ' Define columns width (for output file format).
        Dim saveOptions As New FixedWidthSaveOptions(
            New FixedWidthColumn(8),
            New FixedWidthColumn(8),
            New FixedWidthColumn(8))

        workbook.Save("Fixed Columns Width Text.prn", saveOptions)
    End Sub
End Module

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy