Schreiben in eine vorhandene Excel-Datei mit c #

Ich versuche, eine Excel-Datei zu öffnen (oder eine neue xls-Datei zu erstellen) und einige Werte darauf zu schreiben. Obwohl das folgende Programm gut funktioniert, wenn ich einfach eine neue xls-Datei erstelle, stoße ich auf ein Problem in der Zeile

**mWorkBook = oXL.Workbooks.Open (path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);**

Hier ist der Fehler: Auf 'LOG.xls' kann nicht zugegriffen werden. Die Datei ist möglicherweise beschädigt, befindet sich auf einem Server, der nicht antwortet, oder ist schreibgeschützt. Es ist nicht schreibgeschützt, es ist nicht beschädigt (da die Datei manchmal zur Laufzeit erstellt wird). Was ist das Problem dann?

private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
    private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
    private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
    private static Microsoft.Office.Interop.Excel.Application oXL;

    private void btnSignIn_Click ( object sender, EventArgs e )
    {

        string path = "D:\\LOG.xls";
        if(!File.Exists(path))
        {
            File.Create (path);
        }


        oXL = new Microsoft.Office.Interop.Excel.Application ();
        oXL.Visible = true;
        oXL.DisplayAlerts = false;
        //error on this line
        mWorkBook = oXL.Workbooks.Open (path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

        //Get all the sheets in the workbook
        mWorkSheets = mWorkBook.Worksheets;

        //Get the allready exists sheet
        mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item ("Sheet1");

        Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;

        int colCount = range.Columns.Count;
        int rowCount = range.Rows.Count;

        for ( int index = 1; index < 15; index++ )
        {
            mWSheet1.Cells [rowCount + index, 1] = rowCount + index;
            mWSheet1.Cells [rowCount + index, 2] = "New Item" + index;
        }

        mWorkBook.SaveAs (path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
        Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value);

        mWorkBook.Close (Missing.Value, Missing.Value, Missing.Value);
        mWSheet1 = null;

        mWorkBook = null;

        oXL.Quit ();
        GC.WaitForPendingFinalizers ();
        GC.Collect ();
        GC.WaitForPendingFinalizers ();
        GC.Collect ();
    }

Antworten auf die Frage(2)

Ihre Antwort auf die Frage