0

I have Windows 10 OS, Excel 2016 and working on Visual Studio 2017.

I'm using Microsoft Excel 16.0 Object Library, version 1.9.

I want to save 50 rows having 2 columns in Excel file.

I get error hresult '0x800a03ec' on saveas method.

I have seen more than 25+ links till now, which are previously associated with the same error, but no clue.

Here is my code:

            object misValue = System.Reflection.Missing.Value;
            var excel = new Application();

            // for making Excel visible
            excel.Visible = false;
            excel.DisplayAlerts = false;

            // Creation a new Workbook
            var excelWorkBook = excel.Workbooks.Add(Type.Missing);
            // Work sheet
            var excelSheet = (Worksheet)excelWorkBook.ActiveSheet;
            excelSheet.Name = "xxxxx";

            excelSheet.Cells[1][1] = "head1";
            excelSheet.Cells[1][2] = "head2";

            for (int i = 0; i < list.Count; i++)
            {
                excelSheet.Cells[i + 2][1] = list[i].xxx;
                excelSheet.Cells[i + 2][2] = list[i].yyy;
            }

            excelSheet.SaveAs(@"E:\data.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue);
            excel.Quit();

            Marshal.ReleaseComObject(excelSheet);
            Marshal.ReleaseComObject(excelWorkBook);
            Marshal.ReleaseComObject(excel);

Need help.

Edit:

1) xls or xlsx, none of them works.

2) Based on some previous answers found, I tried this too: Console Root -> Computers -> DCOM Config -> Microsoft Excel Application -> Not found, triple checked.

Someone please try to apply the same scenario in your machine & let me know if you can find the answers.

vipul_surana
  • 120
  • 1
  • 5
  • 21
  • Could it be related to this link? [https://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range](https://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range) – TravisWhidden Jul 08 '17 at 16:29
  • @TravisWhidden no, I tried this, no help for me. I'm using all latest entities from top to bottom – vipul_surana Jul 08 '17 at 18:14
  • @vipul_surana instead of using Excel interop to create Excel sheets programmatically, you should use OpenXML 2.5 SDK instead because it's safer and it doesn't require Excel installation at all. – Eriawan Kusumawardhono Jul 11 '17 at 04:41
  • Do you have permissions to write data on `E:` drive ? – Maciej Los Jul 11 '17 at 10:07
  • Have a look at this https://stackoverflow.com/questions/35642527/unhandled-exception-system-runtime-interopservices-comexception-0x800a03ec – jambonick Jul 12 '17 at 13:46

3 Answers3

1

I don't have your exact versions, I used VS2015 and Excel 15 object and got the exact same exception.

I tried a simplified version of your program (as I don't have your list class details). I was able to save the file with one simple change.

instead of

excelSheet.SaveAs(@"E:\data.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
            Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue);

I had to use

excelSheet.SaveAs(@"E:\data.xlsx")

My for loop is:

        for (int i = 0; i < 1000; i++)
        {
            excelSheet.Cells[i + 2][1] = "xxx" + i;
            excelSheet.Cells[i + 2][2] = "yyy" + i;
        }

I was able to save the xlsx and open it successfully in Excel.

Saved sheet

Can you try the simple SaveAs and see whether you get a different exception ?

If you are looking to get rid of Office automation, I strongly suggest the EPPlus

Subbu
  • 1,635
  • 1
  • 15
  • 22
0

I work with C# and Excel, and Excel VBA a lot, and all my HRESULT type of errors were associated with Excel invalid formulas. In your case your list might contain text that start with =, + or -: and Excel might interpret this as invalid excel formula. (For instance =-bt would result in #NAME? and would cause HRESULT and whatever text after error. Try to format those cells to text and then apply your 50 rows, in this case it should work.

Richard Mneyan
  • 538
  • 1
  • 11
  • 15
0

This happnes to and old ASP app I inherited but the error occurs with any interation with Microsoft.Office.Interop library but only when deployed to IIS. It works fine in debugging mode. I think there must be some config on IIS to make it work as the IIS Express instance created in Debugging mode form VS. Still trying to avoid refactore code to use OpenXML.