Excel工作表更改事件未触发

时间:2013-05-22 05:18:13

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

我使用.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文件中不会反映任何更改。 请帮帮我。 提前致谢

1 个答案:

答案 0 :(得分:2)

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