Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么假定保存的文件没有真正保存?_C#_Excel_Office Interop_Excel Interop_Ole Automation - Fatal编程技术网

C# 为什么假定保存的文件没有真正保存?

C# 为什么假定保存的文件没有真正保存?,c#,excel,office-interop,excel-interop,ole-automation,C#,Excel,Office Interop,Excel Interop,Ole Automation,以下代码取自,用于使用C#创建和保存Excel文件: 它似乎运行得很好——我看到了MessageBox消息,我可以一步一步地完成它,没有任何问题的迹象。但是文件并没有像应该的那样保存到硬盘上。为什么不可以?保存到特定的非根位置(子文件夹)可以工作,例如: String savefullpath = @"C:\misc\csharpExcelTest.xls"; xlWorkBook.SaveAs(savefullpath, Excel.XlFileFormat.xlWor

以下代码取自,用于使用C#创建和保存Excel文件:


它似乎运行得很好——我看到了MessageBox消息,我可以一步一步地完成它,没有任何问题的迹象。但是文件并没有像应该的那样保存到硬盘上。为什么不可以?

保存到特定的非根位置(子文件夹)可以工作,例如:

String savefullpath = @"C:\misc\csharpExcelTest.xls";
    xlWorkBook.SaveAs(savefullpath,
        Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
        Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
文件确实保存到了那个位置

显然,唯一的问题是试图保存到C/root。甚至尝试显式保存到root,如下所示:

String savefullpath = @"C:\csharpExcelTest.xls";
…(而不是简单地提供一个简单的文件名)不光彩地失败了,告诉我:

System.Runtime.InteropServices.COMException was unhandled
  HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
  HResult=-2146827284
  Message=Microsoft Office Excel cannot access the file 'C:'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
    . . .
因此:保存到根目录以外的其他位置以避免此问题

使现代化
另外,当我没有提供文件夹/完整路径时,它似乎正在保存到我的“文档库”,并假设它会保存到C:。今天早上我碰巧查看了那个文件夹,看到了那里的文件。

消息说什么?你想救谁?Windows默认设置将阻止您保存到C的根目录,除非您以管理员身份运行Visual Studio;消息是“Excel文件已创建,您可以找到文件c:\\csharp Excel.xls”;它正在将其保存到root,因为SaveAs()调用中没有给定文件夹。无论如何,它应该是,除非我遗漏了什么。我误解了,我以为捕获中的消息框正在显示。尝试右键单击VisualStudio并说“以管理员身份运行”,看看这是否有帮助。或者指定一个路径,例如桌面。
System.Runtime.InteropServices.COMException was unhandled
  HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
  HResult=-2146827284
  Message=Microsoft Office Excel cannot access the file 'C:'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
    . . .