首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel工作表的名称长度有限制吗?

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

Stack Overflow用户
提问于 2010-09-10 10:05:35
回答 5查看 117.5K关注 0票数 75

当我尝试使用拼音和win32ole使用以下代码设置较长的工作表名称时:

代码语言:javascript
复制
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

我得到了以下信息:

代码语言:javascript
复制
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,但它抱怨工作表名称太长。我看过this related answer中提到的Excel 2007 specifications and limits,但我找不到它提到任何这样的限制。(但是,尝试手动重命名工作表表明可能存在这样的限制)

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-09-10 10:24:40

文件格式最多允许255个字符的工作表名称,但如果Excel UI不希望超过31个字符,请不要尝试超过31个字符。App充满了奇怪的无文档记录的限制和怪癖,向它提供符合规范但不在测试人员测试范围内的文件通常会导致非常奇怪的行为。(个人最喜欢的示例:在具有Excel 97样式的字符串表格的文件中,对if()函数使用Excel 4.0字节码时,禁用了Excel 97中的粗体工具栏按钮。)

票数 115
EN

Stack Overflow用户

发布于 2010-09-10 10:08:49

在Excel中手动重命名工作表时,您会遇到31个字符的限制,所以我建议这是一个硬限制。

票数 9
EN

Stack Overflow用户

发布于 2017-02-28 05:52:30

我使用下面的vba代码,其中filename是包含我想要的文件名的字符串,函数RemoveSpecialCharactersAndTruncate的定义如下:

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3681868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档