C# excel文件保存单元格范围中的值

C# excel文件保存单元格范围中的值,c#,C#,我正在尝试打开一个excel文件和一个名为[ExpiredAccount]的特定工作表。我需要在其中设置该特定范围内的值 这是我的代码,但它不仅保存值,而且不会抛出任何错误 请让我知道我哪里出了问题。如果你能在这方面帮助我,那就太好了 谢谢 Prince您必须保存工作簿。并释放运行时可调用包装器 只需添加以下代码: protected void Page_Load(object sender, EventArgs e) { try

我正在尝试打开一个excel文件和一个名为[ExpiredAccount]的特定工作表。我需要在其中设置该特定范围内的值

这是我的代码,但它不仅保存值,而且不会抛出任何错误

请让我知道我哪里出了问题。如果你能在这方面帮助我,那就太好了

谢谢
Prince

您必须保存工作簿。并释放运行时可调用包装器

只需添加以下代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

                Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(@"D:\Tesco\NGC\Output\temp_02Feb2012.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing);

                Worksheet sheet = (Worksheet)wb.Sheets["ExpiredAccount"];
                Range excelRange = sheet.UsedRange;
                Range rng1 = sheet.get_Range("A2", "A2");
                rng1.Value2 = "India";
                Range rng2 = sheet.get_Range("A3", "A3");
                rng2.Value2 = "Good work"; 

               // string A4D4 = GetRange("A" + 2 + ":A" + 2 + "", sheet);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
p、 如果您没有释放该对象,我建议运行任务管理器,检查“进程”选项卡,并结束所有Excel.exe进程。。。应该有很多:)

wb.Close(true, Type.Missing, Type.Missing); //closes and saves the workbook
app.Quit();
Marshal.FinalReleaseComObject(app); //release the wrapper