public static string GetSafeSheetName(string sheetName) { if (string.IsNullOrEmpty(sheetName)) { return " "; } var sb = new StringBuilder(); foreach (var c in sheetName) { switch (c) { case '*': case '/': case ':': case '?': case '[': case '\\': case ']': sb.Append(' '); break; default: sb.Append(c); break; } if (sb.Length >= 31) break; } if (sb[0] == '\'') { sb[0] = ' '; } if (sb[sb.Length - 1] == '\'') { sb[sb.Length - 1] = ' '; } return sb.ToString(); }