Valid characters for Excel sheet names

后端 未结 4 1697
谎友^
谎友^ 2020-12-09 15:04

In Java, we\'re using the following package to programmatically create excel documents:

org.apache.poi.hssf

If you attempt to set a sheet\'

4条回答
  •  渐次进展
    2020-12-09 15:39

    This is what I use in C# (should be similar in Java):

    const string invalidCharsRegex = @"[/\\*'?[\]:]+";
    const int maxLength = 31;
    
    string safeName = Regex.Replace(worksheetName, invalidCharsRegex, " ")
                            .Replace("  ", " ")
                            .Trim();
    
    if (string.IsNullOrEmpty(safeName))
    {
        safeName = "Default";   // cannot be empty
    }
    else if (safeName.Length > maxLength)
    {
        safeName = safeName.Substring(0, maxLength);
    }
    

提交回复
热议问题