How to create Excel file with Visual Studio 2022 and C#

Dmtr_Grms 331 Reputation points
2023-08-06T15:37:30.7133333+00:00

I'm working with Visual Studio 2022, Project WPF C# with .Net 6.0 and Entity Framework Core. DataBase MS SQL Express.I have also a valid and active licence Microsoft Office 365. I try to Create an Excel File but I have different error msg (see after)

Code behind:

using XXXXXX.DbModel;

using Microsoft.Data.SqlClient;

using Microsoft.EntityFrameworkCore;

using System;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

// Imports necessary to add

using System.Windows.Data;

using System.IO;

using System.Windows.Media;

using System.Collections.Generic;

using Excel = Microsoft.Office.Interop.Excel;

using Window = System.Windows.Window;

        private void GenerateInvoiceHeaderExcel(List<InvoiceHeader> headers)
        {
            // Create an Excel application and workbook
            Excel.Application excelApp = new Excel.Application();
            Excel.Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
            Excel.Worksheet worksheet = (Worksheet)workbook.ActiveSheet;

I have an error msg that CSo246 User's image

At project level I have the following Reference:

User's image

User's image

I tried also to install the MSOffice.Interop office 2016 nuget as suggested in some posts but noway. I f installed I don't have the problem with msg CSo246 but in running the program I have an error IO indicating that the program cannot find and load Office 2016 file.

Could someone be so kind to explain me what I'm doing wrong and how to Create these Excel file simply using standard MS functions without installing Extension?

I'm also interested in knowing how to do the same considering that I have to develop that function for a clients and I don't know what version of Office is installed and if Office is installed. My objectif is only to create an Excel file with data that will then import that Excel somewhere else Thanks in advance.

There is something more that I cant understand. I uninstall Visual Studio 2022 and clean directories. I uninstall Office 365 and clean all directories with Removal PGM from Microsoft Support. Clean all directories. I reinstall my Office 365 from my online Microsoft 365 account and I see that in C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c first of all I see that the directories is indicated as 15.0.0.0 and the object property is Screenshot 2023-08-06 213136

MICROSOFT OFFICE 2013 !!!! Really I don't understand..........

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
2,175 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,339 questions
{count} votes

Accepted answer
  1. Brian Zarb 1,670 Reputation points
    2023-08-06T15:53:06.2933333+00:00

    from past experience, this typically is caused by some sort of versioning. Check the following and let me know if it resolves your issue:

    • Reference Correct Version: Ensure you've referenced the correct version of Microsoft.Office.Interop.Excel. It should match the version of Office you have installed. For Office 365, the version is usually the latest one. Sometimes installing a different version (like 2016) might create a mismatch.
    • Check Build Configuration: Make sure your project's build configuration (x86 vs. x64) doesn't conflict with the installed version of Office (32-bit vs. 64-bit).

    In your references, find Microsoft.Office.Interop.Excel, right-click it, go to properties, and set 'Embed Interop Types' to True. This helps to eliminate some version-specific conflicts


    also, make sure of the following:

    • that the assemblies are actually loaded and not just referenced too.
    • excel is installed (you never know)
    • Make sure the required Interop assemblies are available on the machine, and the user has the necessary permissions to access them.

    Remember to Release COM Objects:

    This doesn't relate directly to your problem, but it's a good code practice. When working with Interop, always release your COM objects. This ensures that the Excel process is terminated correctly, and you don't leave hanging instances of Excel running in the background.

    Marshal.ReleaseComObject(worksheet);
    workbook.Close(false);
    Marshal.ReleaseComObject(workbook);
    excelApp.Quit();
    Marshal.ReleaseComObject(excelApp);
    
    1 person found this answer helpful.

6 additional answers

Sort by: Most helpful
  1. Stefan Krabbe 0 Reputation points
    2025-01-24T10:23:44.6866667+00:00

    I wanted to edit my comment to specify that I am talking about Visual Studio 2022, with regards to the templates.User's image

    0 comments No comments

  2. Stefan Krabbe 0 Reputation points
    2025-01-24T10:28:07.3566667+00:00

    How can it be a violation of your Code of Conduct to point out that we are wasting billions on a global scale due the quality of Microsoft products? The answer I provided is the only correct answer, and none of the others have been able to provide a correct answer, because your documentation is not ok. The thread itself shows that people waste time, and don't even arrive at the correct solution, after spending a lot of time, that they shouldn't have to spend in the first case.

    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.