该进程无法访问另一个进程正在使用的文件.发送附件之前关闭excel文件 [英] The process cannot access the file it is being used by another process. to close excel file before sending attachment

查看:90
本文介绍了该进程无法访问另一个进程正在使用的文件.发送附件之前关闭excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码时,错误消息即将来临 进程无法访问文件"E:\ NotNeed \ 636153238546697234.xlsx",因为它正在被另一个进程使用.

数据导出

{

}

SendEmail()

SendEmail()

{

  System.Net.Mail.Attachment _attachment;

}

polachan

推荐答案

你好,

您可以打开任务管理器",以查看进程"选项卡下有多少个Excel实例.然后从此处确定操作.如果是一个实例,则表明垃圾回收尚未发生,并且内存中仍然有一个或多个对象 即使您退出了Excel自动化.

You could have Task Manager open to see how many instances of Excel are under the process tab. Then determine actions from here. If one instance this would indicate the garbage collection has not occurred and there is still one or more objects still in memory even thou you quit Excel automation. 

关于所有对象的处理,以下(一个随机示例)是以一种形式执行的模式,但很容易放在一个类中.您的代码没有清除所有对象,这就是为什么我要显示它.

In regards to disposal of all objects, the following (a random example) is a pattern to follow done in a form but could easily be in a class. Your code is not cleaning up all objects which is why I'm showing this.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace Example_C2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(new object[] { "Karen", "Payne" });
            dataGridView1.Rows.Add(new object[] { "Mary", "White" });
            dataGridView1.Rows.Add(new object[] { "Joe", "Adams" });
        }

        private void Demo()
        {
            string SheetName = "Sheet1";
            string FileName = System.IO.Path.Combine
                (AppDomain.CurrentDomain.BaseDirectory, "Demo.xlsx");

            bool Proceed = false;

            Excel.Application xlApp = null;
            Excel.Workbooks xlWorkBooks = null;
            Excel.Workbook xlWorkBook = null;
            Excel.Worksheet xlWorkSheet = null;
            Excel.Sheets xlWorkSheets = null;
            Excel.Range xlCells = null;

            xlApp = new Excel.Application();
            xlApp.DisplayAlerts = false;
            xlWorkBooks = xlApp.Workbooks;
            xlWorkBook = xlWorkBooks.Open(FileName);

            xlApp.Visible = false;

            xlWorkSheets = xlWorkBook.Sheets;

            for (int x = 1; x <= xlWorkSheets.Count; x++)
            {
                xlWorkSheet = (Excel.Worksheet)xlWorkSheets[x];

                if (xlWorkSheet.Name == SheetName)
                {
                    Proceed = true;
                    break;
                }

                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet);
                xlWorkSheet = null;
            }

            for (int row = 0; row < dataGridView1.Rows.Count; row++)
            {
                xlCells = xlWorkSheet.Range["A" + (row + 1)];
                xlCells.Value = dataGridView1.Rows[row].Cells["Column1"].Value;
                Marshal.ReleaseComObject(xlCells);
                xlCells = null;

                xlCells = xlWorkSheet.Range["B" + (row + 1)];
                xlCells.Value = dataGridView1.Rows[row].Cells["Column2"].Value;
                Marshal.ReleaseComObject(xlCells);
                xlCells = null;
            }

            xlWorkSheet.SaveAs(FileName);
            xlWorkBook.Close();
            xlApp.UserControl = true;
            xlApp.Quit();

            ReleaseComObject(xlCells);
            ReleaseComObject(xlWorkSheets);
            ReleaseComObject(xlWorkSheet);
            ReleaseComObject(xlWorkBook);
            ReleaseComObject(xlWorkBooks);
            ReleaseComObject(xlApp);

            MessageBox.Show("Done");
        }
        private void ReleaseComObject(object obj)
        {
            try
            {
                Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception)
            {
                obj = null;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Demo();
        }
    }
}


这篇关于该进程无法访问另一个进程正在使用的文件.发送附件之前关闭excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆