EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

System.Drawing not supported in core 2.2 api

Open hhgm opened this issue 5 years ago • 16 comments

I am writing a .net core 2.2 API, where I want to export to Excel at some point. I have created the export functionality as plugins, and all that works fine. I am struggling a bit with EPPlus, as it consistently reports that "System.Drawing is not supported on this platform.".

Stack Trace: at System.Drawing.Imaging.ImageFormat.get_Jpeg() at OfficeOpenXml.Drawing.ExcelPicture..ctor(ExcelDrawings drawings, XmlNode node) at OfficeOpenXml.Drawing.ExcelDrawing.GetDrawing(ExcelDrawings drawings, XmlNode node) at OfficeOpenXml.Drawing.ExcelDrawings.AddDrawings() at OfficeOpenXml.ExcelWorksheet.Save() at OfficeOpenXml.ExcelWorkbook.Save() at OfficeOpenXml.ExcelPackage.Save() at ExportToExcel.Plugin.Process(Project project) in C:\Users\moeller.hans-henrik\source\Workspaces\EquipmentList\Main\Plugins\ExportToExcel\Plugin.cs:line 93

I have tracked this issue to the fact that I have a picture (company logo) in my excel file. Removing that, makes everything good. I have attached the excel file for reference, and below is the code I execute. The package.Save; call triggers the exception. Clean.xlsx

    public Stream Process(Project project)
    {
        try
        {
            var ms = new MemoryStream();
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            using (var template = new FileStream(TemplatePath, FileMode.Open, FileAccess.Read))
            {
                using (var package = new ExcelPackage(ms, template))
                {
                    int row = 10;
                    var sheet = package.Workbook.Worksheets.First();

                    foreach (MainItem mainitem in project.MainItems)
                    {
                        TagCode tag = new TagCode(mainitem.Tag, mainitem.Function);

                        sheet.Cells[row, 1].Value = mainitem.CustomerTag;
                        sheet.Cells[row, 2].Value = tag.Tag;
                        sheet.Cells[row, 3].Value = tag.Plant;
                        sheet.Cells[row, 4].Value = $"{tag.Sys}{tag.Device}";
                        sheet.Cells[row, 5].Value = tag.Part;
                        sheet.Cells[row, 6].Value = mainitem.Function;

                        sheet.Cells[row, 7].Value = mainitem.Description;
                        sheet.Cells[row, 8].Value = mainitem.PidNumber;

                        //location
                        sheet.Cells[row, 25].Value = mainitem.Level;
                        sheet.Cells[row, 26].Value = mainitem.Grid;

                        sheet.Cells[row, 10].Value = mainitem.Revision;
                        row++;

                        foreach (DetailItem item in mainitem.DetailItems)
                        {
                            TagCode detailtag = new TagCode(item.Tag, item.Function);
                            sheet.Cells[row, 1].Value = item.CustomerTag;
                            sheet.Cells[row, 2].Value = detailtag.Tag;
                            sheet.Cells[row, 3].Value = detailtag.Plant;
                            sheet.Cells[row, 4].Value = $"{detailtag.Sys}{detailtag.Device}";
                            sheet.Cells[row, 5].Value = detailtag.Part;
                            sheet.Cells[row, 6].Value = item.Function;

                            sheet.Cells[row, 7].Value = item.Description;
                            sheet.Cells[row, 8].Value = mainitem.PidNumber;

                            //location
                            sheet.Cells[row, 25].Value = mainitem.Level;
                            sheet.Cells[row, 26].Value = mainitem.Grid;

                            sheet.Cells[row, 10].Value = item.Revision;
                            row++;
                        }
                    }

                    package.Save();
                }
            }

            return ms;
        }
        catch (Exception ex)
        {
            int i = 0;
        }

        return null;
    }

hhgm avatar Mar 11 '19 10:03 hhgm

I have the same problem. Using net core 2.2 and I get this error when calling any method that relies on System.Drawing - for example:

worksheet.Column(i).AutoFit();

If I comment out the above code, it works just fine. Have not tried downgrading to 2.1. This is being called from within an Azure Function on App Service Plan - which does support System.Drawing. Investigating further if could be specific to Function App.

JamesReate avatar Apr 03 '19 02:04 JamesReate

Any updates on this? Seeing the same thing with EPPlus on Azure Functions as well, using .net core 2.2.

aderderian avatar Apr 26 '19 17:04 aderderian

I had to switch to use OpenFaas async functions and install libgdiplus on the ubuntu linux container - just defined it in the dockerfile. Azure functions will not work for system.drawing

JamesReate avatar Apr 26 '19 17:04 JamesReate

Even more if you don't use any System.Drawing functions you cannot save the workbook. also the same error on sheet.Delete()

gazbg avatar May 23 '19 09:05 gazbg

same issue on UWP & Xamarin

tipa avatar May 25 '19 09:05 tipa

I'm having the issue too with Azure functions (v1 and v2). It seems a lot of GDI/Win32 api calls are blocked: https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#win32ksys-user32gdi32-restrictions The workaround for me now, is to not to use AutoFitColumns(). Hopefully a solution can be provided in the near future.

MichaelBar1975 avatar Jun 04 '19 09:06 MichaelBar1975

Instead of azure functions we moved the code to app services webapi project. I presume webjobs would work as well.

gazbg avatar Jun 04 '19 09:06 gazbg

This also effects ExcelChart. Makes it impossible to update the series for the chart to the size of the dataset that I'm inserting. Anyone have a work around so that I don't have to access the chart directly and yet have it keep the range and make it update when the new columns are inserted like excel itself does?

Could I drop down to the xml and update the range somehow?

JohnGalt1717 avatar Oct 01 '19 19:10 JohnGalt1717

Update: This won't work at all in Azure App Services nor Functions anymore. No charts, no autosizecolumns nothing.

really need to replace all of the System.Drawing code with something else that doesn't use GDI.

JohnGalt1717 avatar Oct 04 '19 16:10 JohnGalt1717

I downgraded my Application to Core 2.0 and It worked... Unless you are using some feature of 2.2 this could be a temporary solution

riteshbxr avatar Nov 12 '19 08:11 riteshbxr

@riteshbxr Yuk. 2.0 isn't even supported by MS anymore. Only 2.1 (even 2.2 is about to go away)

JohnGalt1717 avatar Nov 12 '19 13:11 JohnGalt1717

There is an unofficial port available as a NuGet package, which uses CoreCompat instead of System.Drawing. Just replace EPPlus.Core by this and it should work fine.

vollkorntomate avatar Dec 11 '19 08:12 vollkorntomate

Don't t bother with Az Function, switch to web jobs. I moved the epplus part to a webjob and it works fine.

On Wed, 11 Dec 2019, 09:26 vollkorntomate, [email protected] wrote:

There is an unofficial port available as a NuGet package https://www.nuget.org/packages/EPPlus.Core.CoreCompat/, which uses CoreCompat instead of System.Drawing. Just replace EPPlus.Core by this and it should work fine.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/JanKallman/EPPlus/issues/427?email_source=notifications&email_token=ALM2NI4IEGOHO2AFOB3RM3TQYCP2XA5CNFSM4G5A6MU2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGSI3MQ#issuecomment-564432306, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALM2NI3VMHGWXABST2JAAPLQYCP2XANCNFSM4G5A6MUQ .

gazbg avatar Dec 12 '19 20:12 gazbg

I think the problem is here: https://github.com/JanKallman/EPPlus/blob/4dacf27661b24d92e8ba3d03d51dd5468845e6c1/EPPlus/EPPlus.MultiTarget.csproj#L297

The reference for System.Drawing.Common is only included when the target framework is "netcoreapp2.1", not 2.2. I think it just needs an additional section targeting 2.2.

stripeyjumper avatar Dec 13 '19 15:12 stripeyjumper

@stripeyjumper The problem is that until .net core 3, you couldn't use anything but netcoreapp2.0 for Azure Functions. Has the code been updated to handle netcoreapp3 ?

JohnGalt1717 avatar Dec 13 '19 21:12 JohnGalt1717

Azure Functions 3 has been released. Anyone test this to see if it's working now?

JohnGalt1717 avatar Jan 30 '20 21:01 JohnGalt1717