Reputation: 114
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.
Upvotes: 0
Views: 5931
Reputation: 11
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.
Upvotes: 0
Reputation: 692
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.
Upvotes: 1
Reputation: 2205
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.
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
Upvotes: 1