Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中打开并保存excel文档#_C#_Excel_File Io_Save - Fatal编程技术网

C# 在c中打开并保存excel文档#

C# 在c中打开并保存excel文档#,c#,excel,file-io,save,C#,Excel,File Io,Save,我试图在C#中打开并保存一个Excel文档,但总是出现错误:“无法使用已与其基础RCW分离的COM对象。”我在谷歌上搜索了很多解决方案,但到目前为止没有一个适合我 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usin

我试图在C#中打开并保存一个Excel文档,但总是出现错误:“无法使用已与其基础RCW分离的COM对象。”我在谷歌上搜索了很多解决方案,但到目前为止没有一个适合我

这是我的密码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

namespace test_excel
{
    public partial class Form1 : Form
    {

        Excel.Application ExcelApp = null;
        Excel.Workbook ExcelWorkBook = null;
        Excel.Sheets ExcelSheets = null;

        Excel.Worksheet MySheet = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExcelApp = new Excel.Application();
            ExcelApp.Visible = false;
            ExcelWorkBook = ExcelApp.Workbooks.Open("c:\\test.xls", 0, true, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

            ExcelSheets = ExcelWorkBook.Worksheets;
            MySheet = (Excel.Worksheet)ExcelSheets.get_Item("Sheet1");

            int a = (int)MySheet.Range["a1"].Value2;
            label1.Visible = true;
            int b = a + 1;

            label1.Text = a.ToString() + " | " + b.ToString();
            MySheet.Range["a1"].Value2 = b;



        }
        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();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelSheets);


                ExcelWorkBook.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                releaseObject(ExcelApp);
                releaseObject(ExcelSheets);
                releaseObject(ExcelWorkBook);
                ExcelApp.Visible = false;
                ExcelApp.UserControl = false;

                ExcelWorkBook.SaveAs("c:\\test.xls", Excel.XlFileFormat.xlWorkbookNormal,
                             null, null, false, false, Excel.XlSaveAsAccessMode.xlShared,
                             false, false, null, null, null);
                ExcelWorkBook.Close();
            }
            catch (Exception err)
            {
                String msg;
                msg = "Error: ";
                msg = String.Concat(msg, err.Message);
                msg = String.Concat(msg, " Line: ");
                msg = String.Concat(msg, err.Source);
                Console.WriteLine(msg);
            }
            finally
            {

                try
                {


                    ExcelApp.Visible = false;
                    ExcelApp.UserControl = false;
                    ExcelWorkBook.Close(true, null, null);
                    ExcelApp.Workbooks.Close();
                }
                catch { }


                ExcelApp.Quit();


                if (MySheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(MySheet); }
                if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
                if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }


                MySheet = null;
                ExcelWorkBook = null;
                ExcelApp = null;
                GC.Collect();
            }
        } 
    }
}

我认为您正在导致
ExcelApp
在此处获得
GC
'd:
releaseObject(ExcelApp)

然后您试图告诉它在这里执行一个方法:
ExcelApp.Quit()


尝试移动
releaseObject(ExcelApp)
在您呼叫后退出

您看到了吗?我尝试了您编写的内容,没有出现错误,但我的应用程序仍然无法写入并保存到excel。你能帮我吗?如果我的答案解决了你的问题,请投票并选择它作为答案。至于编写excel,您上面的问题与编写或保存无关。请打开另一个问题,说明您的问题。