0

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?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    What does the message say? Where are you trying to save? Windows default settings will prevent you from saving to the root of C unless your running Visual Studio as Administrator. – Tyler Jennings Oct 27 '15 at 17:39
  • It's all in the code; the message is "Excel file created , you can find the file c:\\csharp-Excel.xls"; it's saving it to root, since no folder is given in the SaveAs() call. It should be, anyway, unless I'm missing something. – B. Clay Shannon-B. Crow Raven Oct 27 '15 at 17:43
  • 2
    I misunderstood, I thought the messagebox in the catch was displaying. Try right-clicking on Visual Studio and say Run As Administrator and see if that helps. Or specify a path such as your desktop. – Tyler Jennings Oct 27 '15 at 17:46

1 Answers1

0

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.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862