Wednesday, 12 September 2012

C# - Excel - How to fix error Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.

Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.
The type 'Microsoft.Office.Interop.Excel.ApplicationClass' has no constructors defined   




Replace the following code:

            string fileTest = "C:\\Temp\\ExcelTest\\test.xlsx";
            if (File.Exists(fileTest))
            {
                File.Delete(fileTest);
            }

            Excel.Application oApp;
            Excel.Workbook oBook;
            Excel.Worksheet oSheet;
            object misValue = System.Reflection.Missing.Value;

            oApp = new Excel.ApplicationClass();
            oBook = oApp.Workbooks.Add(misValue);

            oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
            oSheet.Cells[1, 1] = "aaa";

            oBook.SaveAs(fileTest);
           
            oBook.Close(true, misValue, misValue);
            oApp.Quit();

with
            string fileTest = "C:\\Temp\\ExcelTest\\test.xlsx";
            if (File.Exists(fileTest))
            {
                File.Delete(fileTest);
            }

            Excel.Application oApp;
            Excel.Workbook oBook;
            Excel.Worksheet oSheet;
            object misValue = System.Reflection.Missing.Value;

            oApp = new Excel.Application();
            oBook = oApp.Workbooks.Add(misValue);

            oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
            oSheet.Cells[1, 1] = "aaa";

            oBook.SaveAs(fileTest);
           
            oBook.Close(true, misValue, misValue);
            oApp.Quit();

Same example on YouTube:

1 comment: