Excel 具有导入固定宽度文本文件的功能,它会显示一个对话框,让您选择放入列中的字段的开始和结束位置。
它是否还具有以下功能:给定现有的电子表格,您可以出口到固定宽度的文本文件?
如果是,我该如何访问它?我尝试使用另存为并选择文本文件,但它似乎只能保存为制表符分隔文件,这对我没有帮助。
如果重要的话,这是 Excel 2003。
答案1
我认为您可以从原生 Excel 功能中获得最接近的功能另存为 | 格式化文本(空格分隔)(*.prn)。它将自动确定宽度并根据需要插入空格来填充该宽度。
除此之外,您还需要一个宏或其他插件可以让您做更多的事情。
答案2
如果您有 Office Professional,则可以在 Access 中打开 Excel 文件,然后从 Access 导出。Access 将允许您为导出的文件指定固定宽度布局,并为您提供极其精细的控制以指定这些宽度。
答案3
哇,我本来要自己问这个问题,但已经有人问过了。tab默认情况下,所有 Excel 剪贴板输出都是分隔的。当您使用固定宽度字体但不一定支持制表符分隔符时,这对于“真正的”纯文本输出来说有点烦人。
无论如何,我找到并修改了一个小型 Excel 宏,它将当前选定的区域复制为一个简单的固定宽度列 ASCII 表——如下所示:
187712201370.18 2525 580 149 0.25 136829 137 43 0.31
这是宏代码。要使用它,请确保在 Excel 选项中启用“开发人员”选项卡如果您使用的是 Excel 2007 或更高版本。
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
答案4
扩展 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