Excel 中的格式化文本 (.prn) 限制为每行 240 个字符

症状

在 Microsoft Excel 中,将工作表另存为带格式文本 (空格分隔) (.prn) 文件时,超过 2000 个 40 个字符的任何字符将包装到下一行。

注意

如果同一工作表上的多行包含超过 240 个字符的文本,则文本开始换行到包含文本的最后一行之后的行。

例如,请考虑以下工作表:

Cell 字符数
A1 40
A2 255
A3 10
A4 21
A5 2
A6 52
A7 255
A8 5
A9 3
A20 13

生成的格式化文本文件包含具有以下字符数的行:

行号 Characters
1 40
2 240
3 10
4 21
5 2
6 52
7 240
8 5
9 3
10-19 0
20 13
21 0
22 15
23-26 0
27 15

在此示例中 (最后一行之后,第 20 行) ,换行行的行号从 1 开始。 实际上,第 21 行对应于第 1 行,第 22 行对应于第 2 行,依此而行。

原因

出现此行为的原因是,根据设计,带格式的文本 (空格分隔) (.prn) 文件每行限制为 240 个字符。

解决方法

Microsoft 提供的编程示例仅用于进行说明,而不提供明示或默示担保。 这包括但不限于适销性或对特定用途的适用性的默示担保。 本文假设您熟悉正在演示的编程语言和用于创建和调试过程的工具。 Microsoft 支持工程师可以帮助解释特定过程的功能,但他们不会修改这些示例以提供新增功能或构建步骤以满足你的特定需要。 若要创建超过每行 240 个字符限制的空格分隔文本文件,请使用类似于以下示例宏的宏。

注意

在运行此宏之前,请执行以下操作:

  • 选择要包含在文本文件中的单元格。
  • 验证列宽是否足以查看每列中的最大字符串。 使用固定宽度字体设置格式时,单个 Excel 列的最大列宽为 255 个字符。
  • 使用“样式”菜单命令设置工作表的格式,以使用固定宽度的字体。 例如,Courier 是固定宽度的字体。

可以修改以下示例代码,以导出用空格以外的字符分隔的数据。 在运行此示例宏之前,必须选择要导出的数据范围。

Sub ExportText()

Dim delimiter As String
   Dim quotes As Integer
   Dim Returned As String

       delimiter = " "

       quotes = MsgBox("Surround Cell Information with Quotes?", vbYesNo)

' Call the WriteFile function passing the delimiter and quotes options.
      Returned = WriteFile(delimiter, quotes)

' Print a message box indicating if the process was completed.
      Select Case Returned
         Case "Canceled"
            MsgBox "The export operation was canceled."
         Case "Exported"
            MsgBox "The information was exported."
      End Select

End Sub

'------------------------------------------------------------

Function WriteFile(delimiter As String, quotes As Integer) As String

  ' Dimension variables to be used in this function.
   Dim CurFile As String
   Dim SaveFileName
   Dim CellText As String
   Dim RowNum As Integer
   Dim ColNum As Integer
   Dim FNum As Integer
   Dim TotalRows As Double
   Dim TotalCols As Double

   ' Show Save As dialog box with the .TXT file name as the default.
   ' Test to see what kind of system this macro is being run on.
   If Left(Application.OperatingSystem, 3) = "Win" Then
      SaveFileName = Application.GetSaveAsFilename(CurFile, _
      "Text Delimited (*.txt), *.txt", , "Text Delimited Exporter")
   Else
       SaveFileName = Application.GetSaveAsFilename(CurFile, _
      "TEXT", , "Text Delimited Exporter")
   End If

   ' Check to see if Cancel was clicked.
      If SaveFileName = False Then
         WriteFile = "Canceled"
         Exit Function
      End If
   ' Obtain the next free file number.
      FNum = FreeFile()

   ' Open the selected file name for data output.
      Open SaveFileName For Output As #FNum

   ' Store the total number of rows and columns to variables.
      TotalRows = Selection.Rows.Count
      TotalCols = Selection.Columns.Count

   ' Loop through every cell, from left to right and top to bottom.
      For RowNum = 1 To TotalRows
         For ColNum = 1 To TotalCols
            With Selection.Cells(RowNum, ColNum)
            Dim ColWidth as Integer
            ColWidth=Application.RoundUp(.ColumnWidth, 0)
            ' Store the current cells contents to a variable.
            Select Case .HorizontalAlignment
               Case xlRight
                  CellText = Space(Abs(ColWidth - Len(.Text))) & .Text
               Case xlCenter
                  CellText = Space(Abs(ColWidth - Len(.Text))/2) & .Text & _
                             Space(Abs(ColWidth - Len(.Text))/2)
               Case Else
                  CellText = .Text & Space(Abs(ColWidth - Len(.Text)))
            End Select
            End With
   ' Write the contents to the file.
   ' With or without quotation marks around the cell information.
            Select Case quotes
               Case vbYes
                  CellText = Chr(34) & CellText & Chr(34) & delimiter
               Case vbNo
                  CellText = CellText & delimiter
            End Select
            Print #FNum, CellText;

   ' Update the status bar with the progress.
            Application.StatusBar = Format((((RowNum - 1) * TotalCols) _
               + ColNum) / (TotalRows * TotalCols), "0%") & " Completed."

   ' Loop to the next column.
         Next ColNum
   ' Add a linefeed character at the end of each row.
         If RowNum <> TotalRows Then Print #FNum, ""
   ' Loop to the next row.
      Next RowNum

   ' Close the .prn file.
      Close #FNum

   ' Reset the status bar.
      Application.StatusBar = False
      WriteFile = "Exported"
   End Function

注意

根据定义,此例程创建的输出文件不同于格式化文本 (*.prn) 文件。 根据定义,格式化文本文件每行不能包含超过 240 个字符。 此外,带格式的文本文件还包含打印机字体信息。 此示例宏没有。 此解决方案旨在在导出到文本文件时提供灵活性。