System.Runtime.InteropServices.COMException Type mismatch. Error when trying to create an xls file with data

0

I followed the guide in this link:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/how-to-access-office-onterop-objects

Here is my code:

        string fname = "Inventory Report.xls";

        var excelApp = new Excel.Application();
        // Make the object visible.
        excelApp.Visible = true;

        // Create a new, empty workbook and add it to the collection returned 
        // by property Workbooks. The new workbook becomes the active workbook.
        // Add has an optional parameter for specifying a praticular template. 
        // Because no argument is sent in this example, Add creates a new workbook. 
        excelApp.Workbooks.Add();

        // This example uses a single workSheet. The explicit type casting is
        // removed in a later procedure.
        Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;

        int row = 1;
        foreach (ReportRevenueMV reportRow in affliateListTemp)
        {
            row++;
            workSheet.Cells[row, "Id"] = reportRow.Id;
            workSheet.Cells[row, "Impressions"] = reportRow.Impressions;
            workSheet.Cells[row, "Full Name"] = reportRow.FullName;
            workSheet.Cells[row, "Email"] = reportRow.Email;
            workSheet.Cells[row, "Visits"] = reportRow.Visits;
            workSheet.Cells[row, "Registrations"] = reportRow.Registrations;
            workSheet.Cells[row, "Number of Deposits"] = reportRow.NumberDeposits;
            workSheet.Cells[row, "Amount of Deposits"] = reportRow.AmountDeposits;
            workSheet.Cells[row, "CPL"] = reportRow.CPL;
            workSheet.Cells[row, "CPA"] = reportRow.CPA;
            workSheet.Cells[row, "Turnover"] = reportRow.Turnover;
            workSheet.Cells[row, "Profit"] = reportRow.Profit; 
        }



        excelApp.Save(fname);

My aim is to input my collection into a spreadsheet and then save it inside a folder.

But what happens instead is that I have an excel file openning up and I get this error,

{ "Message": "An error has occurred.", "ExceptionMessage": "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))", "ExceptionType": "System.Runtime.InteropServices.COMException", "StackTrace": " at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n at Microsoft.Office.Interop.Excel.Range.set__Default(Object RowIndex, Object ColumnIndex, Object )\r\n

What I am doing wrong? What am I missing?

c#
interop
asked on Stack Overflow Dec 24, 2019 by LastGrip

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0