2010-01-25 19:02:42 +0000 2010-01-25 19:02:42 +0000
24
24
Advertisement

エクセルのスプレッドシートを固定幅のテキストファイルにエクスポート?

Advertisement

Excelには固定幅のテキストファイルをインポートする機能があり、フィールドの開始位置と終了位置を選択するダイアログが表示され、それを列に入れることができます。

既存のスプレッドシートを固定幅のテキストファイルにexportする機能もありますか?

もしそうだとしたら、どうやってアクセスすればいいのでしょうか?名前を付けて保存してテキストファイルを選択してみましたが、タブ区切りでしか保存できないようです。

これはExcel2003です。

Advertisement
Advertisement

回答 (7)

24
24
24
2010-01-25 19:16:55 +0000

Excelのネイティブ機能で一番近いのは、Save As | Formatted Text (Space Delimited) (*.prn)だと思います。これは自動的に幅を決定し、必要に応じてその幅にパッドするためにスペースを挿入します。

それ以上のことができるようにするためには、マクロや他のアドインが必要です。

14
14
14
2010-01-25 20:34:43 +0000

Office Professionalをお持ちの場合は、AccessでExcelファイルを開き、Accessからエクスポートすることができます。Accessでは、エクスポートしたファイルに固定幅のレイアウトを指定することができ、その幅を指定するための非常に細かいコントロールが可能になります。

5
Advertisement
5
5
2010-03-18 01:43:08 +0000
Advertisement

うわー、自分で質問しようと思っていたのですが、すでに質問されていました。Excelのクリップボード出力はすべてデフォルトでタブ区切りになっています。これは、あなたが固定幅のフォントを持っているが、必ずしもタブ区切りをサポートしていない場合、"本当の “プレーンテキスト出力のためにちょっとイライラします。

とにかく、現在選択されている領域を単純な固定幅列のASCIIテーブルとしてコピーする小さなExcelマクロを見つけて修正しました。

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

以下がマクロのコードです。これを使用するには、Excel 2007 以降を使用している場合は、Excel オプションの「開発者」タブを有効にしてください。

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
``` 0x1&
4
4
4
2010-01-25 21:12:44 +0000

まず、データをCourier New(または他の固定幅フォント)でフォーマットします。その後、.prnとして保存すると、真の固定幅が得られます。

2
Advertisement
2
2
2015-07-02 17:00:34 +0000
Advertisement

Jeff Atwood氏の回答を拡張すると、コメントできなくなるので。

私は彼のマクロを修正して、列の幅をその列の最も広いセルに設定し、各列に独自の幅を持たせるようにしました。彼のマクロは最初の行で最も幅の広いセルのみを検出し、それにすべての列の幅を設定しています。

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
0
0
0
2015-05-28 08:21:09 +0000

これは私にとってはキラーです。選択肢もいくつかあります。 http://www.sensefulsolutions.com/2010/10/format-text-as-table.html

0
Advertisement
0
0
2018-06-13 11:29:12 +0000
Advertisement

これは、箱から出してすぐにAccessで動作します。 https://support.office.com/en-ie/article/export-data-to-a-text-file-f72dfc38-a8a0-4c5b-8c2c-bf2950814140#bmsteps この方法で、私はそれを非常に簡単かつ高速に管理しました - Excelを使用するよりも優れています。私の場合は、テーブルの変換でした。

Advertisement

関連する質問

12
8
5
2
1
Advertisement