Tuesday, 15 January 2013

c# - Excel Import to DataSet Breaks When File Is Open -



c# - Excel Import to DataSet Breaks When File Is Open -

i have service using excel file imports, , implementation shown below:

[attributes.importfileextension(extension=".xls")] [attributes.importfileextension(extension=".xlt")] [attributes.importfileextension(extension=".xlsx")] public class excelimportservice:fileimportservicebase,ifileimportservice { public dataset import(system.io.stream filestream) { iexceldatareader excelreader = null; switch (extension) { case ".xls": case ".xlt": excelreader = excelreaderfactory.createbinaryreader(filestream); break; case ".xlsx": excelreader = excelreaderfactory.createopenxmlreader(filestream); break; } excelreader.isfirstrowascolumnnames = true; dataset dataset = excelreader.asdataset(); homecoming dataset; } }

additionally, have next code calling service through mill beingness created. code invoking service shown below:

seek { var extension = path.getextension(dialog.filename); var importservice = fileimportservicefactory.create(extension); var stream = dialog.openfile(); var info = importservice.import(stream); stream.close(); stream.dispose(); var result = new fileimportedmessage { info = info }; result.dispatch(); } grab (exception e) { messagedispatcher.dispatch(new exceptionmessage(e)); }

the import works find unless file open while beingness imported.

i have 2 questions:

1)what best practice/strategy handling situation?

2)how i, or should handle this?

the problem opening file in read mode , when adder programme opens in read write mode locks file.

you have 2 options.

1- greedy way open file in read write mode locking program.

2- (only if file not big) read finish file memory stream , processing there.

c# excel io stream import-from-excel

No comments:

Post a Comment