Close Excel File After execution
I am reading some data from an excel sheet, processing it, and saving it to another Excel file. I am using LinqToExcelModern
I would like to move the file from the 'To Do' directory to a 'Done' directory, but I can't figure out how to close/dispose of the file. The file seems to remain in use by my linqpad query until i take some action (like saving the query, or starting and stopping it)
My code is as follows:
void Main()
{
IQueryable importItems;
string fileName;
using (ExcelQueryFactory excel = new ExcelQueryFactory(fullPath)) { excel.UsePersistentConnection = false; excel.ReadOnly = true; importItems = excel.Worksheet<importtemplate>("sheet1"); excel.Dispose();//i tried this, doesn't seem to do anything }; //do some processing //save (this works fine) Util.ToSpreadsheet(processedItems, "Sheet1", true).Save(completePath); //move file File.Move(fullPath, dest); //file is still in use at this point, so i get an error //it is also in use if i try to move it manually in windows. Tell mes file is open in LinqPad, even after execution has stopped
}
Comments
I don't think this is likely to related to LinqPad as the Util.ToSpreadsheet method does not use the same path or importItems so unlikely to interfere.
It is more likely to be caused by LinqToExcelModern. Do you need to use it? It seems to be a two year old port of an abandoned project, so getting support may be difficult.
Try
excel.UsePersistentConnection = true;
https://github.com/alixion/LinqToExcelModern/blob/11373667fbfecabecf4aee0dd6ffae6ae4eab613/src/LinqToExcelModern/ExcelQueryFactory.cs#L683
I appreciate the response!
I agree its in LinqToExcelModern and not the Util at all. I went to that project first, but didnt have high hopes, so came here hoping to find a way to just kill my query thread/process, hoping that would correct seeing this:
Thanks for the reply. I tried switching to use persistent connection, but it doesn't seem to have made any difference, unfortunately