C# 写数据 excel 保持 退出

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook sheet = excel.Workbooks.Open("D:\\openexcel.xlsx");
            Microsoft.Office.Interop.Excel.Worksheet x = excel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;


            Excel.Range useRange = x.UsedRange;
            int countRecords = useRange.Rows.Count;
            int add = countRecords + 1;
            x.Cells[add, 1] = " total rows " + countRecords;
          //  sheet.Saved = true;
            sheet.Close(true, Type.Missing, Type.Missing);
            excel.Quit();
              excel = null;
             GC.Collect();//垃圾回收  

添加两个引用

using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;

猜你喜欢

转载自blog.csdn.net/time_forgotten/article/details/89460626
今日推荐