Search code examples
c#exceloffice-interopexcel-interopole-automation

Why does the supposedly-saved file not really get saved?


The following code is taken from this tutorial to create and save an Excel file with C#:

using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsAppExcelTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonCreateExcelFile_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
            xlWorkBook.SaveAs("csharp-Excel.xls",
                Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
                Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit(); 
            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
            MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls"); 
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 
    } // class
} // namespace

It seems to run fine -- I see the MessageBox msg, and I can step through it with no signs of problems. But the file does not get saved to the hard drive, as it should. Why not?


Solution

  • Saving to a specific non-root location (a subfolder) works, e.g. this:

    String savefullpath = @"C:\misc\csharpExcelTest.xls";
        xlWorkBook.SaveAs(savefullpath,
            Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
            Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    

    The file is indeed saved to that spot.

    Apparently the only problem was trying to save to C/root. Even attempting to explicitly save to root, like this:

    String savefullpath = @"C:\csharpExcelTest.xls";
    

    ...(as opposed to by simply providing a bare filename) fails ignominiously, telling me:

    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.
        . . .
    

    So: save to somewhere other than root to avoid this problem.

    UPDATE

    Also, it seems that it was being saved to my "Documents library" when I was not providing a folder/full path, and assuming it would save it to C:. I just happened to look into that folder this morning, and saw the file there.