none
EPPlus: Throwing exception Can't set color when patterntype is not set RRS feed

  • Question

  • I am trying to add background color through the EPplus library. I have used a task.run to do it. Below is my code:

    suppose Value has the cell reference like AX590 so if I use code like:

    ws.Cells["AX590"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    ws.Cells["AX590"].Style.Fill.BackgroundColor.SetColor(Color.Red);

    the above code throwing error when i execute the line in task.run as asynchronously.

    Actual below code which is throwing error. Error message is Can't set color when patterntype is not set

    public async Task<int> GroupWiseFormatting(ExcelWorksheet ws)
    {
        //ReferenceRange range = null;
        // The Progress<T> constructor captures UI context,
        // so the lambda will be run on the UI thread.
        IProgress<string> progress = new Progress<string>(value =>
        {
           ws.Cells[value].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
           ws.Cells[value].Style.Fill.BackgroundColor.SetColor(Color.Red);
    
        });
    
        await Task.Run(() =>
        {
           foreach (var gk in _FormattingInfo.Where(c => c.GroupKeyCoordinate.Count() != 0))
           {
             var GroupKeyCoordinate = gk.GroupKeyCoordinate;
             foreach (var gkc in GroupKeyCoordinate)
             {
               if (!string.IsNullOrEmpty(gkc))
               {
                 progress.Report(gkc);
               }
             }
           }
          });
    
          return 0;
         }
    am i getting error because back color code execute in the context of Task library ?


    How to overcome this issue with task. please guide me. thanks


    • Moved by CoolDadTx Tuesday, January 8, 2019 2:46 PM Third party product
    Tuesday, January 8, 2019 2:20 PM

Answers

  • This forum is for C#-specific questions only. Since your question involves the use of a third party library you'll need to post your question in their forums. If this library comes from NuGet then NuGet will have a link to the project site where you can get feedback.

    Without any further knowledge I'm going to guess that the code requires that it be called on the thread that created it (either COM based or a UI). Therefore you cannot do what you're trying to do async (or at least on a thread that isn't the creating thread). The library author should be able to help you out though.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Sudip_inn Friday, January 11, 2019 7:38 AM
    Tuesday, January 8, 2019 2:45 PM