简体   繁体   English

C#无法将文本框数据保存到Excel文档

[英]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. 希望第二只眼睛可以帮助我找到解决方案。

I moved the line ~WB.Close()~ before the release ~releaseObject(WB)~ The code changes cell[1,1] with some text. 我将行〜WB.Close()〜移到了发行版〜releaseObject(WB)〜之前,代码用一些文本更改了cell [1,1]。 I changed the path and the text from the form, but the excel part saves properly. 我从表单更改了路径和文本,但是excel部分保存正确。 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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM