0
votes

I'm busy with an application that manipulates excel data using an C# application.

xlApp = new Excel.Application();
            xlWorkbook = xlApp.Workbooks.Open(ConString, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);

Up until this point, I've saved the workbook by simply saying "xlApp.Quit" this would bring up the standard, "Would you like to save your changes" dialog you get from any Microsoft office product.

However I was having an issue with the workbooks data that was being saved, so I was told to use the Workbook.Save method. It now saves the correct data but I cannot specify the filename/location.

I read about the .SaveCopyAs method, I'm using a save file dialog to provide the filename/location, but when I push save and close the application there is no file to be found.

This is the Savefile method.

    private void SaveFile()
    {
        using (SaveFileDialog Save = new SaveFileDialog())
        {
            Save.Filter = "Excel Files (*.xlsx)|*.xlsx";
            Save.InitialDirectory = "C:";
            Save.Title = "Save";
            if (Save.ShowDialog() == DialogResult.OK)
            {
                // Using .SaveCopyAs doesnt save at all
                xlWorkbook.SaveCopyAs(saveFileDialog1.FileName);
                xlWorkbook.Saved = true;
                //Using .Save saves the file, but i cannot specify the location/File name
                //xlWorkbook.Save();
                xlApp.Quit();
                ReleaseObject(xlWorksheet);
                ReleaseObject(xlWorkbook);
                ReleaseObject(xlApp);
            }
        }
    } 

Not sure if this is relevant but here is my ReleaseObject method.

private void ReleaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch(Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

I've seen alot of people saying their SaveCopyAs method isn't saving either and that's been due to them not giving the file a name/extension. This was the first thing I checked. This interop stuff is new to me, so please forgive any errors.

Filename variable

Thank you

1
personally for Excel work I use ClosedXML, its far easier and simpler to work with than the COM \ Interop object. It lives here github.com/closedxml/closedxml, and was originally here, checkout the documentation closedxml.codeplex.com - Simon Price
First of all I agree with @SimonPrice - unless you actually want to recalculate the workbook in which case you might be better off with NetOffice (netoffice.codeplex.com). Meanwhile, have you tried the SaveAs method (msdn.microsoft.com/en-us/library/…)? - Jon Davies

1 Answers

2
votes

Try xlWorkbook.SaveAs("TheSavePath") instead ofxlWorkbook.SaveCopyAs`.

And for overwrite use:

xlWorkbook.SaveAs("TheSavePath", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);