EPPlusSoftware / EPPlus

EPPlus-Excel spreadsheets for .NET
https://epplussoftware.com
Other
1.8k stars 274 forks source link

EPPlus support for net6 on linux. #572

Closed gr3ysky closed 2 years ago

gr3ysky commented 2 years ago

I use EPPlus for export data as Excel files. Currently, System.Drawwing.Common is not supported for net6 applications runing under linux (as a docker image).

Here is the exception

//inner exception

              "ClassName" : "System.TypeInitializationException",
              "Message" : "The type initializer for 'Gdip' threw an exception.",
              "Source" : "System.Drawing.Common",
              "StackTraceString" : """   at System.Drawing.SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif(IntPtr& fontfamily)
   at System.Drawing.FontFamily.GetGdipGenericSansSerif()
   at System.Drawing.FontFamily.get_GenericSansSerif()
   at System.Drawing.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical)
   at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)
   at OfficeOpenXml.ExcelRangeBase.AutoFitColumns(Double MinimumWidth, Double MaximumWidth)

-----
//actual exception
"ClassName" : "System.PlatformNotSupportedException",
              "Message" : "System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.",
              "Source" : "System.Drawing.Common",
              "StackTraceString" : """   at System.Drawing.LibraryResolver.EnsureRegistered()
   at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize()
   at System.Drawing.SafeNativeMethods.Gdip..cctor()""",

This was working on net5. Is there a way to fix this? Thanks.

JanKallman commented 2 years ago

We are currently working on replacing System.Drawing.Common, likely available early next year, but for now you can set the System.Drawing.EnableUnixSupport switch. See https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only .

JLOrdaz commented 2 years ago

This not working for me, I waiting for new update. While I have to use a windows container 😞

JanKallman commented 2 years ago

Hmm that's weired. We are currently working on this in this branch https://github.com/EPPlusSoftware/EPPlus/tree/feature/drawing_to_skia, but it's "Work In Progress". System.Drawing.Common will be removed from .NET standard/core (we will also add a new .NET6 build). We will add native image and text measure support with fallback on Skia or System.Drawing.Common.

SymbioticKilla commented 2 years ago

This not working for me, I waiting for new update. While I have to use a windows container 😞

Have you tried with libgdiplus? We use it in alpine image. Ex.:: RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories \ && apk add --update --no-cache \ libgdiplus

farhadmammadli commented 2 years ago

Just set this line anywhere before creating an excel file and forget about it AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);

Alternatively, you can set switch in csproj

<ItemGroup>
      <RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup>

or environment variable in the Dockerfile ENV DOTNET_System_Drawing_EnableUnixSupport=true

durranitech commented 2 years ago

Dotnet 7 preview has removed the compatibility flag also.

https://github.com/dotnet/runtime/pull/64084

JanKallman commented 2 years ago

EPPlus 6 will remove the dependence on System.Drawing.Common. We will release EPPlus 6 as beta this week or the week after. Feel free to help us test if when it's available. If you want to test it now, please build EPPlus from this branch...https://github.com/EPPlusSoftware/EPPlus/tree/beta/epplus6.
See breaking changes here... https://github.com/EPPlusSoftware/EPPlus/wiki/Breaking-Changes-in-EPPlus-6

durranitech commented 2 years ago

Many thanks @JanKallman I'll build from source and try

JanKallman commented 2 years ago

Feel free to try EPPlus 6.0.0-beta, now released on Nuget. Also checkout our new docker sample available here...https://github.com/EPPlusSoftware/EPPlus.DockerSample

JanKallman commented 2 years ago

Fixed in version 6.0.3

gr3ysky commented 2 years ago

Fixed in version 6.0.3

many thanks.

saxx commented 1 year ago

Sorry for commenting on an older, closed isuse - but I'm tracking down an issue with System.Drawing.Common and .NET 7 and as far as I can say, EPPlus 6 still has an (indirect) dependency on System.Drawing.Common (via EPPlus.System.Drawing)? Is this correct?

JanKallman commented 1 year ago

Yes, but it is only used under Linux if libgdiplus is installed. Otherwise the internal generic image handler is used. System.Drawing.Common is still used under Windows.

saxx commented 1 year ago

Ah, thank you, I see.

In my scenario I still use System.Drawing.Common for very basic stuff, but an older version 5.x version that's still "compatible" with Linux. But that results in package downgrade issues with EPPlus, as EPPlus requires System.Drawing.Common >= 7.0 on .NET 7. I think I can work around that, I just wanted to make sure that this is expected behaviour.

JanKallman commented 1 year ago

Yes, System.Drawing.Common 7 is intended for .NET 7, but as it is Windows only, I assume it might cause issues if you have to use it using libgdiplus elsewhere. I assume we could consider downgrade it to System.Drawing.Common 6, but in the end libgdiplus is a dead end and will not be supported in future versions.

saxx commented 1 year ago

Thanks! I did work around the issue by downgrading EPPlus to a 5.x version (I don't need any of the newer features) as this version still uses System.Drawing.Common <= 5.0.x, which allows me to use this version of System.Drawing.Common in my own code (and not getting any package version conflict/fallback issues). Not ideal, but I was not able to think of a better solution?