c# - Excel worksheet change event is not firing -
i have created excel workbook using .net interop. excel workbook created through c# code. when user makes changes in excel, want stuff. have used excelworksheet.change event. event not firing. here code-
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 dumped 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) { } { xlapp.enableevents = true; } } } no change reflected in excel file when user makes changes. please me out. in advance
the worksheet_change event not global - applies particular worksheet. in code wire event handler xlsheet1.change event, close workbook , release excel objects.
edit: popped code behind form , adapted slightly. event fire , formula in cell y2 set. i'm not 100% sure of circumstances, try code , compare own. hope helps.
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 dumped 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) { } { xlapp.enableevents = true; } } }
Comments
Post a Comment