2013-05-22 114 views
1

我已經使用.NET interop創建了excel工作簿。 Excel工作簿是通過我的C#代碼成功創建的。當用戶在Excel中進行任何更改時,我想要做一些事情。我使用了ExcelWorkSheet.Change事件。但是這個事件並沒有解除。這裏是我的代碼 - 當用戶進行一些更改Excel工作表更改事件未觸發

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

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

我知道Worksheet Change事件適用於特定的工作表。在我的代碼中,我將它連接到'xlWorkSheet'。但事件仍未解決。 – user1749310

+0

但是xlWorkbook中沒有xlWorksheet,在您連接事件後立即關閉它? –

+0

是的。那麼我應該在哪裏關閉工作簿並釋放對象。我打破了這個念頭。請幫忙。 – user1749310