locked
Microsoft.Office.Interop.Excel with .Net Core errors RRS feed

  • General discussion

  • I tried to open an excel document in my .net core desktop application using Microsoft.Office.Interop.Excel but 

    Microsoft.Office.Interop.Excel.Application (exApp ) object in dynamic view contains the error : 

    Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057.

    Also, Workbook (exWbk ) object has the same error and I couldn't work with worksheets.

    Code:

                Microsoft.Office.Interop.Excel.Application exApp = new Microsoft.Office.Interop.Excel.Application();

                Workbook exWbk = exApp.Workbooks.Add(filePath);
    Monday, November 9, 2020 8:28 AM

All replies

  • //New instance of ExcelEngine is created //Equivalent to launching Microsoft Excel with no workbooks open //Instantiate the spreadsheet creation engine ExcelEngine excelEngine = new ExcelEngine(); //Instantiate the Excel application object IApplication application = excelEngine.Excel; //Assigns default application version application.DefaultVersion = ExcelVersion.Excel2013; //A existing workbook is opened. string basePath = _hostingEnvironment.WebRootPath + @"\XlsIO\Sample.xlsx"; FileStream sampleFile = new FileStream(basePath, FileMode.Open); IWorkbook workbook = application.Workbooks.Open(sampleFile); //Access first worksheet from the workbook. IWorksheet worksheet = workbook.Worksheets[0]; //Set Text in cell A3. worksheet.Range["A3"].Text ="Hello World"; //Defining the ContentType for excel file. string ContentType = "Application/msexcel"; //Define the file name. string fileName = "Output.xlsx"; //Creating stream object. MemoryStream stream = new MemoryStream(); //Saving the workbook to stream in XLSX format workbook.SaveAs(stream); stream.Position = 0; //Closing the workbook. workbook.Close(); //Dispose the Excel engine excelEngine.Dispose(); //Creates a FileContentResult object by using the file contents, content type, and file name. return File(stream, ContentType, fileName);

    You can try this

    Monday, November 9, 2020 8:31 AM
  • Check this StackOverflow thread and see if it works for you.
    Monday, November 9, 2020 9:24 AM
    Answerer