0

I'm saving an excel file using SaveAs method in c#. 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.

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"

and the directory :"D:\Application\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Salary Slips\" is exsits

vivek padelkar
  • 195
  • 1
  • 5
  • 31
  • What is the error? Also, make sure the application identity has write permission – Joe Feb 14 '18 at 12:29
  • 1
    Did you try an absolute path like "C:\Temp\a.xlsx"? Did you check if the file exists and has no writeLock? – kara Feb 14 '18 at 12:30
  • 2
    Not sure whether BaseDirectory returns a trailing backslash or not. Never concatenate paths like that. Use System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Salary Slips", "a.xlsx"); – LocEngineer Feb 14 '18 at 12:31
  • 2
    Does `"D:\Application\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Salary Slips\"` exist on the file system? – mjwills Feb 14 '18 at 12:35
  • "D:\Application\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Salary Slips\" is also exist – vivek padelkar Feb 14 '18 at 12:40
  • 1
    Try saving as `Excel.XlFileFormat.xlOpenXMLWorkbook` instead of `xlExcel12` as described here: https://stackoverflow.com/questions/9769703/exporting-to-xlsx-using-microsoft-office-interop-excel-saveas-error – LocEngineer Feb 14 '18 at 12:43
  • @mjwills yes i can save an Excel file there – vivek padelkar Feb 14 '18 at 12:44
  • Have you created that folder manually? Copied it there? Dynamically created during execution? What happens if you try `Directory.CreateDirectory(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Salary Slips");` first? – LocEngineer Feb 14 '18 at 13:19
  • 4
    Are you using Excel Interop? Check your task manager and see if there a bunch of instances of Excel running. If so, one of them could still have your file open, and because of that you can't overwrite it. That's why Excel Interop is evil. I mean, that's one of the reasons why Excel Interop is evil. If there are a bunch of instances running (or even just one extra one that shouldn't be there), you've found the solution to your problem, but now you have to find the solution to a different problem. – Scott Hannen Feb 14 '18 at 15:18
  • Thanks guys for your helpful solution but @LocEngineer 's rick solve my issue. – vivek padelkar Feb 15 '18 at 05:56

2 Answers2

0

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.

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.

CDove
  • 1,940
  • 10
  • 19
0

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

The correct way, here also incorporating the Missing.Value is therefore:

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.

LocEngineer
  • 2,847
  • 1
  • 16
  • 28