Workaround for Excel Automation Issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OpenFileDialog openFileDialog1 = new OpenFileDialog(); | |
openFileDialog1.Filter = "Excel Files|*.xls*"; | |
DialogResult result = openFileDialog1.ShowDialog(); | |
if (result == DialogResult.OK) | |
{ | |
new Thread(() => | |
{ | |
Excel.Application excelApp = null; | |
Excel.Workbook excelWorkbook = null; | |
this.Invoke(new Action(() => | |
{ | |
try | |
{ | |
excelApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); | |
} | |
catch | |
{ | |
excelApp = new Excel.Application(); | |
} | |
string workbookPath = openFileDialog1.FileName; | |
excelWorkbook = excelApp.Workbooks.Open(workbookPath); | |
excelApp.Visible = true; | |
})); | |
Marshal.ReleaseComObject(excelWorkbook); | |
Marshal.ReleaseComObject(excelApp); | |
excelApp = null; | |
excelWorkbook = null; | |
openFileDialog1 = null; | |
GC.Collect(); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.WaitForFullGCComplete(); | |
}).Start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment