0

I am trying to save an excel file via C#, but the file is not being saved in the location I have specified using a variable. Below is the code I have:

Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

if (xlApp == null)
{
    //MessageBox.Show("Excel is not properly installed!!");
    return;
}

Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

if (!System.IO.File.Exists(FileName))
{
    xlWorkBook = xlApp.Workbooks.Add(misValue);
}
else
{
    xlWorkBook = xlApp.Workbooks.Open(FileName, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 1, 0);
}

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//Some Code

xlApp.DisplayAlerts = false;
xlWorkBook.SaveAs(FileName, Type.Missing,Type.Missing,Type.Missing,false,Type.Missing,XlSaveAsAccessMode.xlExclusive,Type.Missing,Type.Missing,Type.Missing);
xlWorkBook.Close(true, FileName, misValue);
xlApp.Application.Quit();
xlApp.Quit();

What am I doing wrong here? I am beyond frustrated that is not saving where I want it to. Any help would be appreciated

FortyTwo
  • 2,414
  • 3
  • 22
  • 33
user3240928
  • 525
  • 2
  • 4
  • 16
  • What do you mean it is not being saved in the location? There is no file found in the desired directory or the file is not overwriiten? – FortyTwo May 09 '17 at 22:37
  • @mohammedlok no file found in the desired directory. – user3240928 May 09 '17 at 22:42
  • What happens if you call `xlWorkBook.SaveAs(FileName)`? Do you get an exception thrown `Cannot access FileName`? – FortyTwo May 09 '17 at 22:44
  • @mohammedlok here is the error: The following error occurred: Microsoft Excel COMException Microsoft Excel cannot access the file. 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. – user3240928 May 09 '17 at 22:47
  • The only other thing I can think of is to check if you have full control permission? – FortyTwo May 09 '17 at 23:10
  • Are you tightly bound to use interop library? I would suggest you use OpenXML. Interop is a COM library, so you need to be careful when using it as it's easy to cause some ugly and hard to find memory leaks (confirmed by myself) if you don't dispose your objects properly, it leaves the Excel.exe process open. Also, it's much slower than previous methods, – FortyTwo May 09 '17 at 23:17

1 Answers1

0

As per @mohammedlok's comment about making sure there are no memory leaks, I always use a try-catch-finally method to ensure that the excel process will always close no matter what. The sample code below worked for me:

Application excelApp = new Application();
Workbook excelWorkbook = null;
string excelFilePath = "C:\\Users\\Desktop\\Sample.xlsx";

try
{
  //Create a workbook
  excelWorkbook = excelApp.Workbooks.Add(Type.Missing);

  //Make and name a worksheet
  activeWorksheet = excelWorkbook.ActiveSheet;

  //Write in cell B3 of the worksheet
  Range r = activeWorksheet.get_Range("B3", Type.Missing);
  r.Value2 = "This is a sample.";

  //Save the workbook
  excelWorkbook.SaveAs(excelFilePath, Type.Missing, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing);  
}
catch (Exception ex)
{
  throw ex;
}
finally
{
  if (excelWorkbook != null)
  {
     excelApp.Calculation = XlCalculation.xlCalculationAutomatic;
     excelApp.DisplayAlerts = false;
     excelWorkbook.RefreshAll();
     excelWorkbook.Close(true, excelFilePath);
     excelApp.Quit();
  }
}
  • @hacklash47 why would I do the open method on a file that does not exist yet? Will that create the new file for editing then I can save it? – user3240928 May 10 '17 at 06:00
  • @user3240928 Sorry, I thought you were wanting to 'Save as' an existing workbook. I've updated the code and it worked for me. Hope it helps. – hackslash47 May 10 '17 at 06:35
  • Doesn't work for me, I get an exception `Cannot access excelFilePath`. – FortyTwo May 10 '17 at 07:17
  • `Try.. catch.. finally` block doesn't ensure that there will be no memory leaks. Many times excel won't quit if it is still holding references to COM objects. [Look at this post](http://stackoverflow.com/questions/158706/how-do-i-properly-clean-up-excel-interop-objects) – FortyTwo May 10 '17 at 07:21