locked
I am getting System.Runtime.InteropServices.COMException while opening xlsx file RRS feed

  • Question

  • User-595461256 posted

    Hi,

    Thanks in Advance..

    I have developed document upload screen and doing some manipulation in the excel data.

    I am getting  generic error message while trying to open the given xlsx file. 

    Error Message:  Exception from HRESULT: 0x800A03EC

    When I try to upload xls file its working fine.

    My Code to open Excel

    var excelApplication = new Excel.Application();

    Excel.Workbook excelWorkBook = null;

    object paramMissing = Type.Missing;

    excelWorkBook = excelApplication.Workbooks.Open(excelPath,
    paramMissing, paramMissing, paramMissing, paramMissing,
    paramMissing, paramMissing, paramMissing, paramMissing,
    paramMissing, paramMissing, paramMissing, paramMissing,
    paramMissing, paramMissing, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile);

    Please help me out what is causing the issue. What I needs to be done to open xlsx file without any error using InteropServices(Microsoft.Office.Interop.Excel)

    Thanks

    Wednesday, November 21, 2018 7:41 AM

Answers

  • User-821857111 posted

    What I need to be done to open xlsx file without any error using InteropServices(Microsoft.Office.Interop.Excel)
    Simple answer is "don't". You shouldn't use Office Interop on a web server. It is not designed for that and MS specifically warn against it. 

    What you should use instead is a library based on OpenXML (which is what xlsx files are). You can either use the Open XML SDK (https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk), or you can use one of the third party open source libraries that already use Open XML to make it easier to work with Excel. The one I use is EPPlus (https://www.nuget.org/packages/EPPlus/)

    Here's a bunch of articles I've written on using EPPlus: https://www.mikesdotnetting.com/category/34/epplus

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2018 7:56 AM
  • User-821857111 posted

    You can't use OpenXML to create PDF files. You can use PDFSharp or iTextSharp or similar. There are also commercial libraries. Perhaps look at Aspose. They do stuff like that.

    OpenXML does not support xls formats. If you really have to work with that format, I suggest using JET 4.0 or the Access Engine to query those files like a database.

    I would persuade users to use the xlsx format instead. It's supported by all versions of Excel from 2007 onwards, and all older versions of Excel are no longer supported at all. There's no reason why people should be using .xls files these days. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2018 12:21 PM

All replies

  • User-821857111 posted

    What I need to be done to open xlsx file without any error using InteropServices(Microsoft.Office.Interop.Excel)
    Simple answer is "don't". You shouldn't use Office Interop on a web server. It is not designed for that and MS specifically warn against it. 

    What you should use instead is a library based on OpenXML (which is what xlsx files are). You can either use the Open XML SDK (https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk), or you can use one of the third party open source libraries that already use Open XML to make it easier to work with Excel. The one I use is EPPlus (https://www.nuget.org/packages/EPPlus/)

    Here's a bunch of articles I've written on using EPPlus: https://www.mikesdotnetting.com/category/34/epplus

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2018 7:56 AM
  • User-595461256 posted

    Hi ,

    Thanks for your valuable information. I have some queries like,

    1.After manipulation I am converting the each sheet to pdf format, Do we need any extra component or Can we use OpenXML SDK?

    2.Will OpenXML SDK support xls format?

    Thanks

    Pughal

    Wednesday, November 21, 2018 9:25 AM
  • User-821857111 posted

    You can't use OpenXML to create PDF files. You can use PDFSharp or iTextSharp or similar. There are also commercial libraries. Perhaps look at Aspose. They do stuff like that.

    OpenXML does not support xls formats. If you really have to work with that format, I suggest using JET 4.0 or the Access Engine to query those files like a database.

    I would persuade users to use the xlsx format instead. It's supported by all versions of Excel from 2007 onwards, and all older versions of Excel are no longer supported at all. There's no reason why people should be using .xls files these days. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2018 12:21 PM