【问题标题】:Excel worksheet change event is not firingExcel 工作表更改事件未触发
【发布时间】:2013-05-22 05:18:13
【问题描述】:

我使用 .NET 互操作创建了 excel 工作簿。通过我的 C# 代码成功创建了 excel 工作簿。当用户在 excel 中进行任何更改时,我想做一些事情。我使用了ExcelWorkSheet.Change 事件。但是这个事件没有触发。这是我的代码-

using Excel = Microsoft.Office.Interop.Excel;    
public class xxx  
{   

    static Excel.Application xlApp;  
    static Excel.Workbook xlWorkBook;  
    static Excel.Worksheet xlWorkSheet;  
    static Excel.Worksheet xlWorkSheet1;
    static Excel.DocEvents_ChangeEventHandler EventDel_CellsChange;   
    public static void ExportToExcel()
    {           
        xlApp = new Excel.ApplicationClass();
        object misValue = System.Reflection.Missing.Value;
        xlWorkBook = xlApp.Workbooks.Add(misValue); 
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);  
        ---------------- data is dumped to the excel here----------------  
        ((Microsoft.Office.Interop.Excel._Worksheet)xlWorkSheet).Activate();
        xlApp.EnableEvents = true;
        EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler(Worksheet_Change);  
        xlWorkSheet.Change += EventDel_CellsChange;
        xlWorkBook.SaveAs("D:\\Test.xlsx", Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);  
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();
        releaseObject(xlWorkSheet1);
        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);  

        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment; filename=Test.xlsx;");
        response.TransmitFile(("D:\\Test.xlsx");
        response.Flush();
        response.End();  
    }  

    public static void Worksheet_Change(Excel.Range Target)
    {
        try
        {
            xlApp.EnableEvents = false;                
            Excel.Range range = xlWorkSheet.get_Range("Y2");                
            range.Formula = "=A2";                
        }
        catch (Exception ex)
        {               
        }
        finally 
        {
            xlApp.EnableEvents = true;
        }
    }  
}    

当用户进行一些更改时,Excel 文件中不会反映任何更改。 请帮帮我。 提前致谢

【问题讨论】:

    标签: c# export-to-excel worksheet-function excel-automation


    【解决方案1】:

    Worksheet_Change 事件不是全局的 - 它仅适用于该特定工作表。在您的代码中,您将事件处理程序连接到 xlSheet1.Change 事件,然后关闭工作簿并释放所有 Excel 对象。

    编辑:我在表单后面弹出了您的代码并稍作调整。我可以触发事件并设置单元格 Y2 中的公式。我不是 100% 确定您的情况,但请尝试此代码,然后与您自己的代码进行比较。希望对您有所帮助。

    public partial class Form1 : Form
    {
        private static Excel.Application xlApp;
    
        private static Excel.Workbook xlWorkBook;
    
        private static Excel.Worksheet xlWorkSheet;
    
        private static Excel.Worksheet xlWorkSheet1;
    
        private static Excel.DocEvents_ChangeEventHandler EventDel_CellsChange;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            xlApp = new Excel.Application();
            xlApp.Visible = true;
            object misValue = System.Reflection.Missing.Value;
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
            //---------------- data is dumped to the excel here----------------  
            ((Microsoft.Office.Interop.Excel._Worksheet)xlWorkSheet).Activate();
            xlApp.EnableEvents = true;
            EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler(Worksheet_Change);
            xlWorkSheet.Change += EventDel_CellsChange;   
        }
    
        public static void Worksheet_Change(Excel.Range Target)
        {
            try
            {
                xlApp.EnableEvents = false;
                Excel.Range range = xlWorkSheet.get_Range("Y2");
                range.Formula = "=A2";
            }
            catch (Exception ex)
            {
            }
            finally
            {
                xlApp.EnableEvents = true;
            }
        }
    }
    

    【讨论】:

    • 我了解工作表更改事件适用于特定工作表。在我的代码中,我已将其连接到 xlWorkSheet。但事件仍然没有触发。
    • 但是 xlWorkbook 中的 xlWorksheet 不是您在连接事件后立即关闭的吗?
    • 是的。那么我应该在哪里关闭工作簿并释放对象。我为此头破血流。请帮忙。
    • 尝试注释掉“xlWorkBook.Close(true, misValue, misValue);”之间的行和“releaseObject(xlApp);”,然后在工作表上进行更改并查看更改事件是否按预期触发。应该在哪里关闭和发布工作簿可能本身就是一个 SO 问题。
    • 我评论了你建议的行。还是一样。在 Excel 文件中看不到任何内容:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2021-08-24
    • 2021-09-30
    • 2014-01-03
    • 1970-01-01
    相关资源
    最近更新 更多