代码之家  ›  专栏  ›  技术社区  ›  Andrew Grimm atk

Excel工作表的名称长度有限制吗?

  •  60
  • Andrew Grimm atk  · 技术社区  · 14 年前

    当我尝试使用ruby和win32ole并使用以下代码设置一个长工作表名称时:

    require "win32ole"
    excel = WIN32OLE.new('Excel.Application')
    excel.Visible = 1
    puts excel.version
    workbook = excel.Workbooks.Add
    worksheet1 = workbook.Worksheets.Add
    worksheet1.Name = "Pseudopseudohypoparathyroidism" #Length 30, fine
    worksheet2 = workbook.Worksheets.Add
    worksheet2.Name = "Supercalifragilisticexpialidocious" #Length 34, not fine
    

    我得到以下信息:

    12.0
    -:9:in `method_missing': (in setting property `Name': ) (WIN32OLERuntimeError)
        OLE error code:800A03EC in Microsoft Office Excel
          You typed an invalid name for a sheet or chart. Make sure that:
    
     The name that you type does not exceed 31 characters.
     The name does not contain any of the following characters:  :  \  /  ?  *  [  or  ]
     You did not leave the name blank.
        HRESULT error code:0x80020009
          Exception occurred.
            from -:9:in `<main>'
    

    版本12.0表明我运行的是Excel2007,但它抱怨工作表名称太长。我看了一眼 Excel 2007 specifications and limits this related answer ,而我却找不到任何这样的限制。(但是,尝试手动重命名工作表可能会有这样的限制)

    是否有限制,是硬限制还是可以通过更改Excel的配置来更改的限制?

    5 回复  |  直到 7 年前
        1
  •  111
  •   mjfgates    14 年前

    文件格式允许最多255个字符的工作表名称,但是如果Excel用户界面不希望您超过31个字符,请不要尝试超过31个字符。应用程序充满了奇怪的未记录的限制和怪癖,给它提供的文件在规范内,但不在测试人员测试的范围内,通常会导致非常奇怪的行为。(个人最喜爱的示例:在带有Excel 97样式stringtable的文件中,将Excel 4.0字节码用于if()函数时,禁用了Excel 97中粗体的工具栏按钮。)

        2
  •  8
  •   Andrew Cooper    14 年前

    在Excel中手动重命名工作表时,您达到了31个字符的限制,因此我建议这是一个硬限制。

        3
  •  2
  •   nikon0iT    9 年前

        4
  •  2
  •   Tunaki Vishal Singh    7 年前

    我使用以下vba代码,其中filename是包含所需文件名的字符串,函数removeSpecialCharacters和truncate的定义如下:

    worksheet1.Name = RemoveSpecialCharactersAndTruncate(filename)
    
    'Function to remove special characters from file before saving
    
    Private Function RemoveSpecialCharactersAndTruncate$(ByVal FormattedString$)
        Dim IllegalCharacterSet$
        Dim i As Integer
    'Set of illegal characters
        IllegalCharacterSet$ = "*." & Chr(34) & "//\[]:;|=,"
        'Iterate through illegal characters and replace any instances
        For i = 1 To Len(IllegalCharacterSet) - 1
            FormattedString$ = Replace(FormattedString$, Mid(IllegalCharacterSet, i, 1), "")
        Next
        'Return the value capped at 31 characters (Excel limit)
        RemoveSpecialCharactersAndTruncate$ = Left(FormattedString$, _
                               Application.WorksheetFunction.Min(Len(FormattedString), 31))
    End Function
    
        5
  •  1
  •   tbc0 Sourcegeek    6 年前

    我刚刚在Windows7上用Excel2013测试了几个路径。我发现总的路径名限制是213,基名长度是186。至少对于超过basename长度的错误对话框是清楚的: basename error

    尝试将不太长的基名移动到太长的路径名也是非常清楚的: enter image description here

    enter image description here

    这是一个懒惰的微软限制。对于这些任意长度限制没有充分的理由,但最终,它在错误对话框中是一个真正的错误。