FileNotFoundException: Could not load file or assembly 'DocumentFormat.OpenXml

1 Answer 478 Views
.NET Framework
Erin
Top achievements
Rank 1
Erin asked on 30 May 2024, 03:21 PM

I have tried generating a PDF directly from my report and I have tried opening it in a viewer.  I always come back to this error.

FileNotFoundException: Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

I've tried installing the package listed in Nuget, but that exact version does not appear to be available.  The versions I tried did not fix the problem.

Here is my code for generating the report...


InstanceReportSource report = new InstanceReportSource();
report.ReportDocument = new BatchReport();
report.Parameters.Add("BatchNumber", batch.ID);

ReportGenerator.DisplayPDF(report);



public static void DisplayPDF(InstanceReportSource source)
{
    ReportProcessor reportProcessor = new ReportProcessor();
    RenderingResult result = reportProcessor.RenderReport("PDF", source, null);

    if (!result.HasErrors)
    {
        string fileName = result.DocumentName + "." + result.Extension;
        string path = System.IO.Path.GetTempPath();
        string filePath = System.IO.Path.Combine(path, fileName);

        using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
        {
            fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
        }

        Process.Start($"file://{filePath}");
    }
}

BTW, I am working on a .NET Framework 4.7.2 project.
Erin
Top achievements
Rank 1
commented on 30 May 2024, 03:59 PM

The exception I posted above is the INNER exception.  The outer one is this...

System.TypeInitializationException: 'The type initializer for 'Telerik.Reporting.OpenXmlRendering.Wordprocessing.WordprocessingReport' threw an exception.'

1 Answer, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 04 Jun 2024, 09:41 AM

Hello Erin,

Generally, when rendering in PDF with the ReportProcessor, you don't need the assembly "DocumentFormat.OpenXml". You need it when you render to OpenXML formats like DOCX. Our assembly 'Telerik.Reporting.OpenXmlRendering.dll' depends on it - Deploying Open XML.

The failure to load the above file is handled in our code and should appear as a warning instead. Is it possible that your Visual Studio treats warnings as errors - Warning as error - How to get rid of these?

As an alternative, you may add a newer version of the package, up to 2.5.5631.0 with a BindingRedirect in the configuration file of the application. Note that you may use also newer "DocumentFormat.OpenXml" versions with the other "Telerik.Reporting.OpenXmlRendering' assemblies we provide, as explained in the above Documentation article.

Regards,
Todor
Progress Telerik

Stay tuned by visiting our roadmap and feedback portal pages, enjoy a smooth take-off with our Getting Started resources, or visit the free self-paced technical training at https://learn.telerik.com/.
Tags
.NET Framework
Asked by
Erin
Top achievements
Rank 1
Answers by
Todor
Telerik team
Share this question
or