Would you like to try using NetOfficeFw.Excel? You can get it from nuget.
https://www.nuget.org/packages/NetOfficeFw.Excel
-
There is no need to write Marshal.ReleaseComObject at all. All objects implement IDisposable, and when you Dispose them, they are released internally using ReleaseComObject. Also, if you Dispose the top level NetOffice.ExcelApi.Application, all the objects under it will be released.
-
Since it is late binding, it does not depend on the Office version.
■ new file save as.
using Excel = NetOffice.ExcelApi;
using (var xlApp = new Excel.Application()) {
using (var xlBook = xlApp.Workbooks.Add()) {
using (Excel.Worksheet xlSheet
= (Excel.Worksheet)xlBook.Worksheets.Add()) {
xlSheet.Cells[1, 1].Value = "1";
}
xlApp.DisplayAlerts = false;
xlBook.SaveAs(@"z:\test.xlsx");
}
xlApp.Quit();
}
■ Like GetObject in VB
object book = Marshal.BindToMoniker(@"z:\test.xlsx");
Excel.Workbook xlBook = new Excel.Workbook(null, book);
Excel.Application xlApp = xlBook.Application;