Search code examples
c#excelinteropoledbexcel-interop

C# Cannot save textbox data to excel document


As far as I know, there are no errors to speak of in the following code:

    private Excel.Application Xls;
    private Excel.Workbooks XlsB;
    private Excel.Workbook WB;
    private Excel.Worksheet WS;
    private Excel.Sheets WBs;

private void btnPrint_Click(object sender, EventArgs e)
    {
        try
        {
            Xls = new Excel.Application();
            XlsB = Xls.Workbooks;
            WB = XlsB.Open(lblPath.Text, 0, false, 5, "", "", true,
                XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Sheets WBs = WB.Worksheets;
            Worksheet WS = WBs.get_Item(1);
            WS.Cells[1, 1] = txtTATW.Text;
            WB.Save();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Write Excel: " + ex.Message);
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            releaseObject(WS);
            releaseObject(WBs);
            releaseObject(WB);
            releaseObject(XlsB);
            WB.Close(true, lblPath.Text, misValue);
            Xls.Quit();
            releaseObject(Xls);
        }
    }

Finally the release method:

        private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }

    }

Some of the data is carried over from a different form, which is not visible here, but everything important should be listed!

It basically just pulls data from a textbox into a particular cell. For now, i'm testing it with only once cell. Here's to hoping a second pair of eyes can help me find a solution.


Solution

  • I moved the line ~WB.Close()~ before the release ~releaseObject(WB)~ The code changes cell[1,1] with some text. I changed the path and the text from the form, but the excel part saves properly. Hope this helps.

    private Excel.Application Xls;
    private Excel.Workbooks XlsB;
    private Excel.Workbook WB;
    private Excel.Worksheet WS;
    private Excel.Sheets WBs;
    string lblPath = "SomePath";
    string txtTATW = "Some Text";
    
    private void btnPrint_Click(object sender, EventArgs e)
    {
      try
      {
        Xls = new Excel.Application();
        XlsB = Xls.Workbooks;
        WB = XlsB.Open(@"H:\ExcelTestFolder\TestAddData.xlsx", 0, false, 5, "", "", true,
            XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        WBs = WB.Worksheets;
        WS = WBs.get_Item(1);
        WS.Cells[1, 1] = txtTATW;
        WB.Save();
      }
      catch (Exception ex)
      {
        MessageBox.Show("Write Excel: " + ex.Message);
      }
      finally
      {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        WB.Close();
        Xls.Quit();
        releaseObject(WS);
        releaseObject(WBs);
        releaseObject(WB);
        releaseObject(XlsB);
        releaseObject(Xls);
      }
      MessageBox.Show("Finished Updating File");
    }