Continuing in the theme of my last post, the next issue I had transitioning running a .NET Core app from Windows to Mac was in the area of Excel export. I’m not referring to CSV output here but fully formatted worksheets via ClosedXML.

I won’t go into the Excel code itself but there’s some similar usage in this much older post of mine on JIRA to Excel with the Atlassian.Net SDK. When I ran this console app and looked at the logs I saw this exception logged.

[16:25:30 WRN] Failed to export Version data to Excel
System.TypeInitializationException: The type initializer for 'ClosedXML.Excel.XLHelper' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
 ---> System.DllNotFoundException: Unable to load DLL 'libgdiplus': The specified module could not be found.
   at System.Runtime.InteropServices.FunctionWrapper`1.get_Delegate()
   at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInput& input, StartupOutput& output)
   at System.Drawing.SafeNativeMethods.Gdip..cctor()
   --- End of inner exception stack trace ---
   at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, Int32 format, HandleRef scan0, IntPtr& bitmap)
   at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
   at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
   at ClosedXML.Excel.XLHelper..cctor()
   --- End of inner exception stack trace ---
   at ClosedXML.Excel.XLHelper.GetColumnNumberFromLetter(String columnLetter)
   at ClosedXML.Excel.XLWorksheet.Column(String column)
   at ClosedXML.Excel.XLWorksheet.ClosedXML.Excel.IXLWorksheet.Column(String column)

ClosedXML appears to have a fair amount of dependency on System.Drawing and it appeared I needed libgdiplus for GDI+ support on Mac. I installed that with brew install mono-libgdiplus and noticed an error at the end of the output.

==> Installing python@2
Error: An exception occurred within a child process:
    FormulaUnavailableError: No available formula with the name "/usr/local/opt/python@2/.brew/python@2.rb"

Despite the error the Excel spreadsheet was generated on the next run of the app. However I now noticed new stdout from the app of Unable to revert mtime: /Library/Fonts. That was resolved with brew install libmagic; I believe that has some file type determination logic.

More background on this GDI+ / System.Drawing issue can be found in dotnet/core #2746.