c# How to access opened Excel file? -
please help me stuff!
in new project need access excel files. names of of them known. it's easy open files , write info in. if excel objects stored list, it's easy re-access opened files while programme still running. please, take illustration below.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using excel = microsoft.office.interop.excel; namespace exceltutorial { public partial class form1 : form { list<textbox> texttoexcel = new list<textbox>(); list<string> s_excelfiles = new list<string> { "workbook1.xlsx", "workbook2.xlsx", "workbook3.xlsx", "workbook4.xlsx", "workbook5.xlsx" }; list<excel.application> l_excelapplication = new list<excel.application>(); list<excel.workbook> l_excelworkbook = new list<excel.workbook>(); list<excel.worksheet> l_excelworksheet = new list<excel.worksheet>(); bool fileopened = false; public form1() { initializecomponent(); texttoexcel.add(textbox1); texttoexcel.add(textbox2); texttoexcel.add(textbox3); texttoexcel.add(textbox4); texttoexcel.add(textbox5); } private void btn_unopened_click(object sender, eventargs e) { string path = system.io.path.getdirectoryname(application.executablepath); if (fileopened != true) { foreach (var item in s_excelfiles) { excel.application newexcelapp = new excel.application(); excel.workbook workbook = newexcelapp.workbooks.open(path + "\\" + item , readonly: false, editable: true); excel.worksheet worksheet = (excel.worksheet)workbook.activesheet; newexcelapp.visible = true; ((excel.range)worksheet.cells[1, 1]).value = texttoexcel[s_excelfiles.indexof(item)].text; l_excelapplication.add(newexcelapp); l_excelworkbook.add(workbook); l_excelworksheet.add(worksheet); } fileopened = true; } else { foreach (var item in l_excelworksheet) { ((excel.range)item.cells[1, 1]).value = texttoexcel[l_excelworksheet.indexof(item)].text; } } } private void btn_opened_click(object sender, eventargs e) { } } }
however, there possibility user has workbook opened @ moment when programme runs. obviously, excel sends message user asking workbook reopening. cause info losing. of course of study can check if file opened before accessing how described here next code:
try { stream = file.open(filemode.open, fileaccess.readwrite, fileshare.none); }
and inquire user close file. believe there way better.
so, question is: how access file opened user?
the exceltutorial solution available here!
you can seek code illustration access current active workbook:
//gets excel , gets activeworkbook , worksheet excel.application oxl; excel.workbook owb; excel.worksheet osheet; oxl = (excel.application)marshal.getactiveobject("excel.application"); oxl.visible = true; owb = (excel.workbook)oxl.activeworkbook; docprops = owb.customdocumentproperties
or maybe improve solution:
using excel = microsoft.office.interop.excel; using exceldna.integration; // currect application instance excel.application xlapp = (excel.application)exceldnautil.application; // active workbook excel.workbook wbook = xlapp.activeworkbook;
more info here: get current workbook object in c#
c# excel
No comments:
Post a Comment