How to apply shape's background fill color in MS Excel spreadsheet in .NET

Hi,

I am upgrading Aspose.cell 8.1.0.0 to Aspose.Cells 16.10.0.0(for .Net) in my project. I am getting below errors in my previous code:

Warning as Error: ‘Aspose.Cells.Drawing.Shape.FillFormat’ is obsolete: '“Use Shape.Fill property instead.”'
Warning as Error: ‘Aspose.Cells.Drawing.Shape.LineFormat’ is obsolete: 'Use Shape.Line property instead.'

But, I am not getting “IsVisible” or “ForeColor” property in Aspose.Cells.Drawing.Shape.Fill.
Similarily, there is no “IsVisible”, “Style”, “ForeColor” or “BackColor” property in Aspose.Cells.Drawing.Shape.Line.

PFA for screenshot of my sample code where I am getting the errors:
Please suggest on this

Hi Imran,


Thank you for contacting Aspose support.

Please find below the details for alternative APIs that you should use with most recent release of the Aspose.Cells for .NET.

  • Please use Shape.IsFilled instead of Shape.FillFormat.IsVisible as the IsFilled property indicates if the fill format is visible or not. Similarly, use the Shape.HasLine property instead of Shape.LineFormat.IsVisible as the new HasLine property indicates if border of the shape is visible or not.
  • The mechanism to set the fill color has been changed and there is no ForeColor property available for the FillFormat class. Instead, the API has exposed the ForegroundColor property for the PatternFill class and Color property for the SolidFill class. You need to set the FillFormat.FillType property the use the appropriate ForegroundColor or Color property accordingly. Same is the case with LineFormat.ForeColor. Please check the sample code at the bottom of this post for your reference.

C#

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
int textboxIndex = worksheet.TextBoxes.Add(2, 1, 160, 200);
Shape shape = worksheet.TextBoxes[textboxIndex];

shape.Fill.FillType = FillType.Solid;
shape.Fill.SolidFill.Color = Color.Red;

shape.Line.FillType = FillType.Solid;
shape.Line.SolidFill.Color = Color.Yellow;

workbook.Save(dir + “output.xlsx”);

Hi,


Thanks for the reply.
I have one more question:

How is the below code handeled in new API? I am not getting Aspose.Cells.Drawing.Shape.Line.Style.

wordsShape.Stroke.LineStyle = ConvertDrawingLineStyle(excelShape.LineFormat.Style);

Hi Imran,


Thank you for writing back.

First of all, please note that Shape.Stroke property is part of Aspose.Words for .NET API therefore I will not be able to help you out, however, I suggest you post your concerns in Aspose.Words support forum for all inquiries related to Aspose.Words APIs. Regarding the LineFormat.Style property from Aspose.Cells APIs, you may alternatively use the LineFormat.DashStyle property as per your provided code snippet.

Hi Babar,


Thanks for the reply.
I am trying to give LineStyle from Aspose.cells to Aspose.Word. Its basically Excel-To-Doc Conversion.
The method ConvertDrawingLineStyle() actually checks for LineStyle in Aspose.Cells and return the corrosponding LineStyle for Aspose.Words
Below is the method:

public ShapeLineStyle ConvertDrawingLineStyle(MsoLineStyle lineStyle)
{
switch (lineStyle)
{
case MsoLineStyle.Single: return ShapeLineStyle.Single;
case MsoLineStyle.ThickBetweenThin: return ShapeLineStyle.Triple;
case MsoLineStyle.ThickThin: return ShapeLineStyle.ThickThin;
case MsoLineStyle.ThinThick: return ShapeLineStyle.ThinThick;
case MsoLineStyle.ThinThin: return ShapeLineStyle.Double;
default: return ShapeLineStyle.Single;
}
}

I got that “Aspose.Cells.Drawing.Shape.LineFormat.DashStyle” has been changed to “Aspose.Cells.Drawing.Shape.Line.DashStyle”(I have corrected it if you will check in my first Post’s ScreenShot). But there is not property I am getting for "Aspose.Cells.Drawing.Shape.LineFormat.Style"in new API. This is Aspose.Cells issue only.

Regards,
Imran Shahid.

Hi Imran,


Thank you for the clarification. Please note, the LineFormat.CompoundType property now returns the MsoLineStyle. Please check the offline documentation for more details as we are currently in the process to update the online API Reference guide as per latest API set. You can find the Aspose.Cells For .NET Documentation.chm file in the Help folder of the Aspose.Cells for .NET installation directory (when installed via MSI installer).

Hi I’m migrating to 17.5 and I cannot find the equivalent of this in Aspose.Cells.net


wordsShape.Stroke.Color = excelShape.LineFormat.ForeColor.IsEmpty ? Color.Black : excelShape.LineFormat.ForeColor;
wordsShape.Stroke.Color2 = excelShape.LineFormat.BackColor;

I need the equivalent of those ForeColor and BackColor properties from Aspose.cells.
The documentation isn’t clear what these properties were replaced with.

I know that excelShape.HasLine is equal to excelShape.LineFormat.IsVisible


Hi Walter Stypula,


Well, the mechanism to set the shape’s shading color has been changed. Shape.LineFormat property was already obsoleted. Now you you may use Shape.Line property instead. The newer API has also exposed the ForegroundColor property for the PatternFill class and Color property for the SolidFill class, both works the same. Similarly you have BackgroundColor for PatternFill class. See the following for your reference:
//for ForeColor
Shape.Line.PatternFill.ForegroundColor
//Or
Shape.Line.SolidFill.Color

//for BackColor
Shape.Line.PatternFill.BackgroundColor


Hope, this helps a bit.

Thank you.

Do I have to worry about PatternFill being null?

I ask because there are many different FillTypes. I don’t want to have to write a switch statement for each FillType available.

I also wouldn’t know how to handle PatternFill being null, as a result needing to use other Properties to achieve the same results we have today with the now obsolete LineFormat.

Hi,


Well, you have to check FillType with some switch case. It is same as MS Excel where we have to set the fill type and then set the property of the fill.

Thank you.