OpenXML Error when creating new WorksheetPart on deployed dll

Mike Gerbi 0 Reputation points
2024-04-07T16:11:25.71+00:00

So first off, my code works in a console application. I installed OpenXML via NuGet into the solution. I created a new solution that is a .NET dll library and installed openXML the same way and deployed the dll with the OpenXML dlls and when my dll is called, I'm getting an error as shown below.

OpenXML that is installed is 3.0.2

VerificationException: Method DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPart: type argument 'DocumentFormat.OpenXml.Packaging.WorksheetPart' violates the constraint of type parameter 'T'.

I've commented out most of my code to try to isolate where this is happening and I think I've found it. Interesting, when it does throw the error, it doesn't even step into the routine CreateExcel(). But when I comment out a certain line of code, it will step through. Here is the basic structure I have.

My main routine.


internal static void CreateRawMtlExcel(List<RawMtl> Mtls, string OutputPath)
        {
            //SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(string.Format(@"{0}\{1}MexRawMtls.xlsx", OutputPath), SpreadsheetDocumentType.Workbook);

            using (SpreadsheetDocument package = SpreadsheetDocument.Create(OutputPath, SpreadsheetDocumentType.Workbook))
            {
                CreateExcel(package, Mtls);
            }
        }


private static void CreateExcel(SpreadsheetDocument document, List<RawMtl> Mtls)
        {

            WorkbookPart workbookPart = document.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();
            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(); //This seems to be the line that is causing issues. 
            
            SheetData sheetData = new SheetData();

            worksheetPart.Worksheet = new Worksheet(sheetData);
            //Columns hColumns = GetHeaderColumns();

            //Sheets sheets = document.WorkbookPart.Workbook.
            //    AppendChild<Sheets>(new Sheets());

            //// Append a new worksheet and associate it with the workbook.
            //Sheet sheet = new Sheet()
            //{
            //    Id = document.WorkbookPart.
            //    GetIdOfPart(worksheetPart),
            //    SheetId = 1,
            //    Name = "Raw Materials"
            //};

            //sheets.Append(sheet);
            //var sheetdata = worksheetPart.Worksheet.GetFirstChild<SheetData>();

            ////worksheetPart.Worksheet.InsertBefore(hColumns, sheetdata);
            ////WorkbookStylesPart workbookStylesPart1 = workbookPart.AddNewPart<WorkbookStylesPart>("rId3");
            ////GenerateWorkbookStylesContent(workbookStylesPart1);

            workbookPart.Workbook.Save();
        }
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.
10,268 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 32,076 Reputation points Microsoft Vendor
    2024-04-08T02:50:49.5766667+00:00

    Hi @Mike Gerbi , Welcome to Microsoft Q&A,

    Based on the code you provided, the problem may be in your CreateExcel method. I noticed that you are having issues creating the worksheetPart object, specifically you are encountering an error on the following line of code:

    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
    

    As you can see from the error message, the error is related to type parameter constraints. This indicates that there is a problem when you are trying to add a new worksheet section.

    In order to solve this problem, you can try to use different methods to create WorksheetPart, for example, you can try to use AddNewPart method overloaded form, or directly instantiate WorksheetPart ** kind.

    Here's an alternative you can try:

    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(
    

    Alternatively, you can instantiate WorksheetPart directly:

    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet =
    

    Both methods should effectively create a new WorksheetPart object without causing type parameter constraint errors. Please try one of these methods and check if it resolves the issue you are experiencing.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.