简体   繁体   English

无法在C#中使用saveAs方法保存Excel文件

[英]unable to save excel file using saveAs method in c#

I'm saving an excel file using SaveAs method in c#. 我在c#中使用SaveAs方法保存一个excel文件。 but it shows error like : 但它显示如下错误:

Additional information: The file could not be accessed. 附加信息:无法访问该文件。 Try one of the following: 请尝试以下方法之一:

• Make sure the specified folder exists. •确保指定的文件夹存在。

• Make sure the folder that contains the file is not read-only. •确保包含文件的文件夹不是只读的。

• Make sure the file name does not contain any of the following characters: < > ? •确保文件名不包含以下任何字符:<>? [ ] : | []:| or * 要么 *

• Make sure the file/path name doesn't contain more than 218 characters. •确保文件名/路径名不超过218个字符。

my code is this : 我的代码是这样的:

string savepath = AppDomain.CurrentDomain.BaseDirectory + @"\Salary Slips\a.xlsx";
xlwbOP.SaveAs(savepath, Excel.XlFileFormat.xlExcel12);

the savepath variable have value savepath="D:\\Application\\WindowsFormsApplication1\\WindowsFormsApplication1\\bin\\Debug\\Salary Slips\\a.xlsx" savepath变量的值为savepath =“ D:\\ Application \\ WindowsFormsApplication1 \\ WindowsFormsApplication1 \\ bin \\ Debug \\ Salary Slips \\ a.xlsx”

and the directory :"D:\\Application\\WindowsFormsApplication1\\WindowsFormsApplication1\\bin\\Debug\\Salary Slips\\" is exsits 并且存在目录“ D:\\ Application \\ WindowsFormsApplication1 \\ WindowsFormsApplication1 \\ bin \\ Debug \\ Salary Slips \\”

If I'm remembering correctly (it's been awhile), you should check to makes sure the file exists before attempting to save. 如果我没记错(已经有一段时间了),那么在尝试保存之前,应检查并确保文件存在。 You might also want to wrap this in a try-catch block to handle exceptions caused by problems with permissions or paths not existing. 您可能还希望将其包装在try-catch块中,以处理由于权限或路径不存在而导致的异常。

string savepath = AppDomain.CurrentDomain.BaseDirectory + @"\Salary Slips\a.xlsx";
try
{
    if(!File.Exists(savepath))
         xlwbOP.SaveAs(savepath, Excel.XlFileFormat.xlExcel12);
    else
         xlwbOP.Save();
}
catch
{  /*input your exception handling here*/ }

I also recommend verifying that your path doesn't contain invalid characters before trying to use it to save. 我还建议在尝试使用路径进行保存之前,先验证其路径中是否包含无效字符

You may also want to look into using SaveCopyAs(string) for saving copies as new files. 您可能还想研究使用SaveCopyAs(string)将副本另存为新文件。

As described in Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs Error , the proper enum to save as .xlsx is not Excel.XlFileFormat.xlExcel12 but Excel.XlFileFormat.xlOpenXMLWorkbook 使用Microsoft.Office.Interop.Excel SaveAs错误导出到.xlsx中所述, 另存为.xlsx的正确枚举不是Excel.XlFileFormat.xlExcel12而是Excel.XlFileFormat.xlOpenXMLWorkbook

The correct way, here also incorporating the Missing.Value is therefore: 因此,此处也合并了Missing.Value的正确方法是:

var missing = System.Reflection.Missing.Value;
var savepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Salary Slips", "a.xlsx");
xlwbOP.SaveAs(savepath, Excel.XlFileFormat.xlOpenXMLWorkbook, missing,
    missing, false, false, missing, missing, true, missing, missing, missing);

Credits to SO user MoonKnight. 归功于SO用户MoonKnight。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM