此代码基本上重新格式化xls文件并将其保存为xlsx。但它使用@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAM_REQUEST && resultCode == RESULT_OK) {
Uri uri = data.getData();
Attachment att = new Attachment (uri, "BUNA");
list.add(att);
}
}
& G2
获取新格式化文件的文件名。
这意味着某些字符不能出现在文件名中。我添加了一大块代码来替换这些字符(
H2
) activeworkbook.saves是调试器总是带我的地方
我收到一条错误消息,指出即使' Remove/Replace Invalid File Name Characters
WkbName = Range("H2")
MyArray = Array("<", ">", "|", "/", "*", "\", ".", "?", """")
For X = LBound(MyArray) To UBound(MyArray)
WkbName = Replace(WkbName, MyArray(X), "_", 1)
Next X
'MsgBox WkbName 'dispaly file name with illegal characters removed
ActiveWorkbook.SaveAs Filename:= _
WBPath & "\BOM_" & Range("G2") & "_" & WkbName & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
中只是普通文字,我也总是非法的,我错过了什么?
以下完整代码
h2
请原谅我的笔记和代码中的随机废话。在把它交给别人之前我总是把它清理干净
答案 0 :(得分:5)
因为文件名中可能有更多非法字符。您的方法是正确的,但在保存之前,不是要从文件名中删除或替换的非法字符的完整列表。例如。您的代码中的数组中缺少这些字符 - &gt; :&amp; 。但是建议保持文件名摆脱其他允许的特殊字符。
下面,我提供的函数返回一个安全字符串,可以在保存之前用于生成文件名。
Function ReplaceIllegalCharacters(strIn As String, strChar As String) As String
Dim strSpecialChars As String
Dim i As Long
strSpecialChars = "~""#%&*:<>?{|}/\[]" & Chr(10) & Chr(13)
For i = 1 To Len(strSpecialChars)
strIn = Replace(strIn , Mid$(strSpecialChars, i, 1), strChar)
Next
ReplaceIllegalCharacters = strIn
End Function
具体来说,在您的代码中,将ActiveWorkbook.SaveAs行替换为以下行:
ActiveWorkbook.SaveAs Filename:= _
WBPath & "\BOM_" & Range("G2").Value2 & "_" & ReplaceIllegalCharacters(Range("H2").Value2, "_") & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False