c# - Why xls opens while/after reading it from code? -


i have tool parsing excel files. user chooses xls/xlsx read, tool creates copy of in temp random name, parses copy. relevant part of code:

string sourcefile = textbox1.text; string filename = system.io.path.getrandomfilename(); string destfile = system.io.path.combine(path.gettemppath(), filename + ".xlsx"); system.io.file.copy(sourcefile, destfile, true);  microsoft.office.interop.excel.application xlapp = new microsoft.office.interop.excel.application(); microsoft.office.interop.excel.workbook excelbook = xlapp.workbooks.open(destfile); oledbconnection cnn = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=" + destfile + "; extended properties=excel 12.0;"); string qtext = @"select * [sheet1$]"; oledbcommand oconn = new oledbcommand(qtext, cnn); cnn.open(); oledbdataadapter adp = new oledbdataadapter(oconn); datatable dt = new datatable(); adp.fill(dt); cnn.close(); 

and start work datatable. in case original source files (not copy) open in excel, copy temp folder opens in excel. , here comes strangest stuff. if it's closed during script run, , open original xls windows explorer, copy temp opens again. moreover, if run script e.g. 10 times (while original closed), when open it, 10 randomly named copies open along it. suppose it's not due code itself, windows/office bug/feature. please advise.

you have 2 lines here initializes , open copy of destination file

microsoft.office.interop.excel.application xlapp = new microsoft.office.interop.excel.application(); microsoft.office.interop.excel.workbook excelbook = xlapp.workbooks.open(destfile); 

you should remove them. oledb interaction excel file doesn't need interop work.

by way, when working these ugly interop variables use simple trick shorten these names

using excellib = microsoft.office.interop.excel; .....  excellib.application xlapp = new excellib.application(); 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -