【发布时间】:2020-03-13 15:36:23
【问题描述】:
我正在尝试保存具有特定路径和文件名的 Excel 工作簿,文件扩展名为 .xlsx。
这是完整的错误,当我点击按钮保存时:
$exception {"无法访问该文件。请尝试以下方法之一:\n\n• 确保指定的文件夹存在。\n• 确保包含该文件的文件夹不是只读的。\ n• 确保文件名和文件夹路径不包含以下任何字符: ? [ ] : | 或 *\n• 确保文件名和文件夹路径不包含超过 218 个字符。"} System.Runtime .InteropServices.COMException
我看到它说 - do not contain any of the following characters: < > ? [ ] : |
代码:
//Path
string result = "C:\\Users\\LV98\\Desktop\\Test C#\\";
//Save Path
public string TheValue
{
get { return $"{result}{Path.GetFileNameWithoutExtension(f8.filePath)} [{DateTime.Now.ToString("ddMyyyyHHmmss")}].xlsx"; }
}
//Button
private void button1_Click(object sender, EventArgs e)
{
//Excel Application
excelApp = new Excel.Application();
//Excel Workbooks
workbooks = excelApp.Workbooks;
//Excel Workbook
target = workbooks.Add(f8.filePath);
//Excel all sheets from Workbook
Excel.Sheets sheets = target.Worksheets;
//Get specific sheet name
Excel.Worksheet workingSheet = (Excel.Worksheet)sheets.get_Item(value);
var newbook = excelApp.Workbooks.Add(1);
workingSheet.Copy(newbook.Sheets[1]);
newbook.SaveAs(TheValue); // ----------------------------------------------------- SAVE VALUE HERE
newbook.Close();
target.Close();
excelApp.Quit();
killExcel();
this.Close()
}
详情:
我的“另存为”文件路径中显然有 []。
但我可以手动创建一个文件,并使用[] 命名它没有问题..
这是TheValue的输出:
关注:
为什么这不让我使用[]?
【问题讨论】:
-
\ / : * ? " < > |这些是不可能的字符。 -
@TheGridLock 和 OP 文件名不包含其中任何一个。 OP 询问
[ ] -
我认为他要保存一个文件并且路径超过255个字符!所以他收到了这个错误。
-
@TheGridLock 没有,路径贴出来了,只有56个。
-
别让我猜。只有他知道到底发生了什么!