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.

Fixed Columns Width Text

Following example shows how to read and write Fixed Columns Width Text file which is text file with each column size fixed to some arbitrary number of characters.

File used in this example can be downloaded from FixedColumnsWidthText.prn.

Screenshot
Fixed Columns Width Text Screenshot

See the full code below.

using System;
using System.Collections.Generic;
using System.Text;
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.ConditionalFormatting;
using GemBox.Spreadsheet.PivotTables;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

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

        // Load file
        ExcelFile ef = ExcelFile.Load("FixedColumnsWidthText.prn", loadOptions);

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

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

        ef.Save("FixedColumnsWidthText.prn", saveOptions);
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.ConditionalFormatting
Imports GemBox.Spreadsheet.PivotTables

Module Samples

    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 ef As ExcelFile = ExcelFile.Load("FixedColumnsWidthText.prn", loadOptions)

        ' Modify file
        ef.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))

        ef.Save("FixedColumnsWidthText.prn", saveOptions)

    End Sub

End Module

Check next sample.