Link to home
Start Free TrialLog in
Avatar of mbosch
mbosch

asked on

CREATE A FIXED WIDTH TEXT FILE from a template or other input method.

What methods are available for creating fixed width text files.  I need to create a text file either by converting an excel template or some other method.  What I'm asking is what methods/functions are available to create a new text file and output/write fixed width data into it.  Thanks!
Avatar of srcalc
srcalc

I'm not sure how in depth you plan to get in this, but if you are using a dataset or some similar data handling object, you can use the System.Data.OleDB namespace. Check out this web page:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting03092004.asp
Cut/Paste this code, it will create a fixed line-length file from whatever you enter into the textbox.  I dont work with excel, so reading from your spreadsheet/template etc ill leave upto you, but for writing fixed line-length text files, this does the trick.

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(81, 163)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(131, 29)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Write File"
        '
        'TextBox1
        '
        Me.TextBox1.AutoSize = False
        Me.TextBox1.Location = New System.Drawing.Point(13, 13)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(267, 143)
        Me.TextBox1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim strFixedLength As String
    Dim strTemp As String
    Dim position As Integer
    Dim i As Integer

    Sub WriteFile(ByVal Data As String)
        Dim Stream As FileStream
        Stream = New FileStream("C:\test\test.txt", FileMode.Append)
        Dim Writer As New StreamWriter(Stream)
        Writer.WriteLine(Data)
        Writer.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Writing a file with line length of 10 characters.
        'To adjust length of line you should adjust the
        'Step 10 to Step xx and the 3rd parameter of the
        'Mid expression to match.
        If File.Exists("C:\test\test.txt") Then
            File.Delete("C:\test\test.txt")
        End If

        strTemp = TextBox1.Text
        For i = 1 To Len(TextBox1.Text) Step 10
            strFixedLength = Mid(strTemp, i, 10)
            WriteFile(strFixedLength)
        Next

    End Sub

End Class

Regards
Jaz
I just realised i wrote that in VB.NET 2005 Beta1 - The windows form designer code may (or may not) work with previous versions, i have noticed some differences.  In the event that the designer code doesnt work, just delete the designer region, and draw one multiline textbox, and one button onto your form.

Jaz
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mbosch,

Could you please explain what was wrong with my answer, or how LearnedOne's assisted more.

Thanks,
Jaz
Avatar of mbosch

ASKER

Its just the method I used, but I suppose you're answer is still a correct answer.  How do I adjust the points in this thing?
It would be a CS request to adjust points, the usual deciding factor in who you give points to is the answer that 'helps you the most'.  If more than one answer did help you then you should split them.  I have no problem with you awarding points to LearnedOne, as his answer is correct, I was just querying how it assisted you more than a full solution.  Please only ask CS to split if my answer did help you, if it didnt, just let LearnedOne have them.

Regards
Jaz