c# - Programmatically save Excel table in opened window, into file -


so need do

there program connects server , fetches data @ speed of approximately 15-20 entries per second

right @ moment when connection server has been established, excel window opens , entries fetched server, dynamically moved excel table

however content remains in ram, i.e. excel file created when try close excel window , 'save as..' dialog appears

as have understood, don't want save file manually every time, , hence need way programmatically save excel table in opened window, file

is there way of doing ?

if you're not running on server , don't mind manually triggering excel save, can create connect excel instance using interop. following connect excel instance , return excel.workbook object specified workbook name:

private excel.workbook getworkbook(string workbookname) {     excel.window window = null;      // excel window object application grabbed     excel.application app = null;    // excel instance open workbooks     excel.workbooks wbs = null;      // list of workbooks     excel.workbook wb = null;        // workbook return     enumchildcallback cb;            // callback routine child window enumeration routine     list<process> procs = new list<process>();           // list of processes      // full list of processes have name of "excel"      procs.addrange(process.getprocessesbyname("excel"));      foreach (process proc in procs)     {         // make sure have valid handle window          if ((int)proc.mainwindowhandle > 0)         {             // handle of child window in current excel process              int childwindow = 0;             cb = new enumchildcallback(enumchildproc);             enumchildwindows((int)proc.mainwindowhandle, cb, ref childwindow);              // make sure got valid handle              if (childwindow > 0)             {                 // address of child window can talk ,                 // workbooks                  const uint objid_nativeom = 0xfffffff0;                 guid iid_idispatch =                     new guid("{00020400-0000-0000-c000-000000000046}");                 int res = accessibleobjectfromwindow(childwindow, objid_nativeom,                     iid_idispatch.tobytearray(), ref window);                  if (res >= 0)                 {                     app = window.application;                     wbs = app.workbooks;                      // loop through workbooks within current excel window                     // see if match                      (int = 1; <= wbs.count; i++)                     {                         wb = wbs[i];                          if (wb.name == workbookname)                         {                             break;                         }                          wb = null;                     }                 }             }         }          // if we've found our workbook there's no point in continuing         // through remaining processes          if (wb != null)         {             break;         }     }      release(wbs);     release(app);     release(window);      return wb; } 

the release() methods called above set references null , call marshal.finalreleasecomobject() on them, otherwise end headless instances of excel on place.

you'll need following perform of functionality grab windows:

private delegate bool enumchildcallback(int hwnd, ref int lparam);  [dllimport("user32.dll")] private static extern bool enumchildwindows(int hwndparent, enumchildcallback lpenumfunc, ref int lparam);  [dllimport("oleacc.dll")] private static extern int accessibleobjectfromwindow(int hwnd, uint dwobjectid, byte[] riid, ref excel.window ptr);  private bool enumchildproc(int hwndchild, ref int lparam) {     // name of class owns passed-in window handle      stringbuilder buf = new stringbuilder(128);     getclassname(hwndchild, buf, 128);      // if class name excel7 we've got valid excel window      if (buf.tostring() == "excel7")     {         lparam = hwndchild;         return false;     }      return true; }  [dllimport("user32.dll")] private static extern int getclassname(int hwnd, stringbuilder lpclassname, int nmaxcount); 

once have workbook can call workbook.saveas() save it.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -