On Windows 11 Home and net6.0 printing an xlsx file works like a charm. But on net8.0 this same code isn't compiling.
Hi,
This is because FlexCelPrintDocument is based in PrintDocument (PrintDocument Class (System.Drawing.Printing) | Microsoft Learn ) and PrintDocument only works in Windows. So your app must have a target of "net8.0-windows" which is true by default. But, as .NET 8 only accepts Windows10 or newer, we can't compile FlexCel just targetting Net8.0-windows.
So the target needs to be "net8.0-windows10.0.22621.0" or other newer version you want ("net8.0-windows10" doesn't work).
Open your project .csproj file in a text editor, search for TargetFramework and make it:
TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
Instead of just
TargetFramework>net8.0-windows</TargetFramework>
After that it will work.
Than you very much for your reply!
It was really helpful. The only thing is that I use this library in the Avalonia UI project (v11.1.3).
Problem is that the solution has only single desktop project for windows/macos/linux.
I expected that that targeting plain net8.0 in code I just could do OperatingSystem.IsWindows() and write code accordingly.
Will this be possible in a newer versions of this great library?
The problem is that there isn't a crossplatform way to print in .NET. PrintDocument (the class from which FlexCelPrintDocument derives) is winforms only. There are open issues to support printing from .NET Maui, but as far as I know, it is not yet possible. Once (and if) there is an universal way to print form .NET, we will for sure support it.
Now, just to make it clear: You can have a single project for windows/macos/linux and support only printing in Windows via FlexCelPrintDocument. That's what FlexCel.dll does itself: it is a single project, but the windows version supports printing.
You (and we) cannot just do a "if OperatingSystem.IsWindows()" because you need to link different stuff. (Basically link GDI+ in windows)
But you can do have different dependencies in different platforms, either by having all the "core" in a single library and then having different projects with only the cross platform part, of by doing what FlexCel itself is doing. If you open FlexCel-DotNetCore.csproj you will see that the target platforms are:
<TargetFrameworks>net8.0;net8.0-windows;net8.0-maccatalyst;net8.0-ios;net8.0-macos;net8.0-android</TargetFrameworks>
And then we define a GDIPLUS
symbol only for windows:
<PropertyGroup Condition="$(TargetFramework.Contains('-windows'))">
<DefineConstants>$(DefineConstants);GDIPLUS</DefineConstants>
</PropertyGroup>
Then in the code, we have the code that is gdiplus specific under #IF(GDIPLUS)
defines. So instead of using if OperationSystem.IsWindows
you need to use some #if MY_WINDOWS_DEFINE
and put the printing code inside.