Export an Excel spreadsheet to fixed-width text file?

24

6

Excel has the functionality to import fixed-width text files where it presents a dialog that lets you choose where the begins and ends of fields are which it puts into columns.

Does it also have functionality where, given an existing spreadsheet, you can export to a fixed-width text file?

If so, how do I access this? I have tried using Save As and choosing Text File but it seems to only save as Tab-delimited which doesn't help me.

This is Excel 2003 if it matters.

RationalGeek

Posted 2010-01-25T19:02:42.113

Reputation: 579

Answers

24

I think the closest you can get from native Excel functionality is Save As | Formatted Text (Space Delimited) (*.prn). It will automatically determine the widths and insert spaces to pad to that width as necessary.

Beyond that you need to have a macro or other add-in that will let you do more.

squillman

Posted 2010-01-25T19:02:42.113

Reputation: 5 676

3Just to add some clarity - it will "automatically" determine widths based on the width you specify for your columns. While normally the measurements approximate to ems (as far as I recall), when exporting to fixed width prn they are taken literally and rounded down to give integer values used for the number of characters in a column of the prn file. Spaces are added for padding, to the left or right depending on alignment in the cells (so by default numbers wil be padded on the left to force them right, text on the right to force left, but if you have changed alignments they will be different) – AdamV – 2010-01-25T21:55:44.527

This does do what I was looking for. Thanks! – RationalGeek – 2010-01-26T19:34:41.637

In order to use the macro pointed to by iokevins, you have to remove "& delimiter" from the two places where CellText is defined. Otherwise, it will add an additional space for each cell, which saving as a prn does not do. – farrenthorpe – 2015-10-12T19:49:17.977

1

Please note: Formatted text (.prn) is limited to 240 characters per line in Excel http://support.microsoft.com/kb/249885

– iokevins – 2012-09-20T16:57:07.693

14

If you have Office Professional, you can open your Excel file in Access, and then Export from Access. Access will let you specify a fixed-width layout for your exported file, and gives you extremely granular controls for specifying those widths.

davidcl

Posted 2010-01-25T19:02:42.113

Reputation: 595

While Access might do it, so will Excel which is what was asked for. – AdamV – 2010-01-25T21:56:32.917

7Thanks for the downvote, but I thought it was worth mentioning that Access has a full version of this feature. Excel doesn't let you specify column widths directly, Access does. Yes, you can get the right column widths in Excel by diddling around with the column widths in the worksheet, but it's a pain and as someone who has to create fixed width files on a regular basis it isn't worth the time. If you have Access (which many people do) there's a much better way. – davidcl – 2010-01-27T23:44:39.290

Excellent; thank you for this alternative : o ) – iokevins – 2012-09-20T17:00:27.510

5

Wow, I was going to ask this question myself but it was already asked. All Excel clipboard output is tab delimited by default. This is kind of annoying for "real" plain text output when you have a fixed width font but not necessarily tab delimiter support.

Anyway, I found and modified a small Excel Macro that will copy the currently selected region as a simple fixed-width columns ASCII table -- like so:

187712 201    37     0.18   
2525   580    149    0.25   
136829 137    43     0.31   

Here's the Macro code. To use it, make sure you enable the Developer tab in Excel Options if you are using Excel 2007 or later.

Sub CopySelectionToClipboardAsText()

   ' requires a reference to "Windows Forms 2.0 Object Library"
   ' add it via Tools / References; if it does not appear in the list
   ' manually add it as the path C:\Windows\System32\FM20.dll

    Dim r As Long, c As Long
    Dim selectedrows As Integer, selectedcols As Integer

    Dim arr
    arr = ActiveSheet.UsedRange
    selectedrows = UBound(arr, 1)
    selectedcols = UBound(arr, 2)

    Dim temp As Integer
    Dim cellsize As Integer
    cellsize = 0
    For c = 1 To selectedcols
        temp = Len(CStr(Cells(1, c)))
        If temp > cellsize Then
            cellsize = temp
        End If
    Next c
    cellsize = cellsize + 1

    Dim line As String
    Dim output As String

    For r = 1 To selectedrows
        line = Space(selectedcols * cellsize)
        For c = 1 To selectedcols
            Mid(line, c * cellsize - cellsize + 1, cellsize) = Cells(r, c)
        Next c
        output = output + line + Chr(13) + Chr(10)
    Next r

    Dim MyData As MSForms.DataObject
    Set MyData = New DataObject
    MyData.SetText output
    MyData.PutInClipboard

    MsgBox "The current selection was formatted and copied to the clipboard"

End Sub

Jeff Atwood

Posted 2010-01-25T19:02:42.113

Reputation: 22 108

4

First, format your data as Courier New (or some other fixed width font). Then save as .prn and you'll get true fixed width.

dkusleika

Posted 2010-01-25T19:02:42.113

Reputation: 1 776

This worked great for me. I also like to format everything as text before I do this. That way all data is left aligned when saved. – Gary Brunton – 2013-01-04T20:17:26.550

2

Expanding on Jeff Atwood's answer, since it would not let me comment there:

I modified his macro to set the column width to the widest cell in that column and to have each column its own width. His macro only found the widest cell in the first row and then set the width of all columns to it.

Sub CopySelectionToClipboardAsText()

   ' requires a reference to "Windows Forms 2.0 Object Library"
   ' add it via Tools / References; if it does not appear in the list
   ' manually add it as the path C:\Windows\System32\FM20.dll

    Dim r As Long, c As Long, linesize As Long
    Dim selectedrows As Integer, selectedcols As Integer

    Dim arr
    arr = ActiveSheet.UsedRange
    selectedrows = UBound(arr, 1)
    selectedcols = UBound(arr, 2)
    ReDim CellSizes(1 To selectedcols, 2) As Integer

    Dim temp As Integer
    Dim cellsize As Integer
    linesize = 0
    For c = 1 To selectedcols
        cellsize = 0
        For r = 1 To selectedrows
            temp = Len(CStr(Cells(r, c)))
            If temp > cellsize Then
                cellsize = temp
            End If
        Next
        CellSizes(c, 0) = cellsize + 1
        CellSizes(c, 1) = linesize
        linesize = linesize + cellsize + 1
    Next c

    Dim line As String
    Dim output As String

    For r = 1 To selectedrows
        line = Space(linesize)
        For c = 1 To selectedcols
            Mid(line, CellSizes(c, 1) + 1, CellSizes(c, 0)) = Cells(r, c)
        Next c
        output = output + line + Chr(13) + Chr(10)
    Next r

    Dim MyData As MSForms.DataObject
    Set MyData = New DataObject
    MyData.SetText output
    MyData.PutInClipboard

    MsgBox "The current selection was formatted and copied to the clipboard"

End Sub

Peter

Posted 2010-01-25T19:02:42.113

Reputation: 21

0

It works with Access out of the box: https://support.office.com/en-ie/article/export-data-to-a-text-file-f72dfc38-a8a0-4c5b-8c2c-bf2950814140#bmsteps with this way I managaed it quite easy and fast - better than with Excel. In my case it was a conversion of the table.

Alex

Posted 2010-01-25T19:02:42.113

Reputation: 1

Looks like you've posted your answer on the wrong question ;-) This question is asking about Excel, whilst your answer is talking about Access. Also for future answers: Providing the link for reference is useful, but you must also include the essential information in the post.

– robinCTS – 2018-06-13T12:17:43.587

0

This is a killer for me. It has a few options as well.

http://www.sensefulsolutions.com/2010/10/format-text-as-table.html

sancho.s Reinstate Monica

Posted 2010-01-25T19:02:42.113

Reputation: 2 404

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Ramhound – 2015-07-02T17:39:20.280

2@Ramhound - In general, I agree. Buti n this case, there is nothing I can copy here. That website is a place where you paste your input data and you get "Excel-formatted" output. If the link becomes invalid, then the service is gone. – sancho.s Reinstate Monica – 2015-07-03T00:13:39.600

The service has moved: https://senseful.github.io/web-tools/text-table/, and source is available: https://github.com/senseful/web-tools

– ngm – 2016-10-06T12:31:22.497

Which again has moved to: https://senseful.github.io/text-table/

– Chris – 2017-08-23T13:17:48.980