smons
smons

Reputation: 485

The type initializer for 'ClosedXML.Excel.XLWorkbook' threw an exception

using ClosedXML.Excel;

class XLSXWriter : BExporter
    {
        private readonly string _fName;

        public XLSXWriter(string fileName)
        {
            _fName = fileName;
        }

        public override void Export(IEnumerable<AnimalData> animals)
        {
            var workBook = new XLWorkbook(); <---Throws an exception

            var workSheet = workBook.Worksheets.Add("MySheet");

            workSheet.Cell("A1").Value = "Hello World";            

            FileStream fs = new FileStream(_fName, FileMode.Create);
            workBook.SaveAs(fs);
            fs.Close();          
        }
    }

Just trying this library ClosedXML.dll, and it throws an exception can't figure out what could be the problem. I get the _fName from the savefiledialog. Ideas anybody?

Upvotes: 4

Views: 22706

Answers (2)

Asad Naeem
Asad Naeem

Reputation: 636

I had fixed the same error after updating closedXML library. In other words, if ClosedXML and OpenXML are updated packages then this error will not occur.

Upvotes: 1

smons
smons

Reputation: 485

Thought would delete this post, but might help someone in future....All i had to do was reference one more DLL DocumentFormat.OpenXml.dll . Works perfectly now!

Upvotes: 11

Related Questions