Draw a line shape with arrow icon

alcLine.zip (13.0 KB)

@tejas.patel
Here is the sample code, hope it helps.
All the drawing you need can be done with ShapePath. Just think of a ShapePath object as a separate canvas.

For more basic shape insertions, please refer to: Insert Pictures and Shapes of Excel files.|Documentation

//Notice: A ShapePath object is treated as a separate canvas where the image is drawn relative to the origin of the canvas.

Workbook workbook = new Workbook();

int w = 230, h = 30;
//Inserts a curve with an arrowhead and only one turning point.
//shape1
ShapePath shapePath = new ShapePath();
shapePath.MoveTo(0, 0);
shapePath.LineTo(230, 0);
shapePath.LineTo(230, 30);
Shape shape = workbook.Worksheets[0].Shapes.AddFreeform(1, 0, 1, 0, h, w, new ShapePath[] { shapePath });
shape.Line.FillType = FillType.Solid;
shape.Line.SolidFill.Color = Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;//Select the type of arrow you want
  
//shape2
shape = workbook.Worksheets[0].Shapes.AddAutoShape(AutoShapeType.BentConnector2, 5, 0, 1, 0, h, w);
shape.Line.FillType = FillType.Solid;
shape.Line.SolidFill.Color = Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;

//Three ways to insert a straight arrow 
//line1
shape = workbook.Worksheets[0].Shapes.AddAutoShape(AutoShapeType.Line, 8, 0, 1, 0, 0, w);
shape.Line.FillType = FillType.Solid;
shape.Line.SolidFill.Color = Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;

//line2
shape = workbook.Worksheets[0].Shapes.AddLine(9, 0, 1, 0, 0, w);
shape.Line.FillType = FillType.Solid;
shape.Line.SolidFill.Color = Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;

//line3
shapePath = new ShapePath();
shapePath.MoveTo(0, 0);
shapePath.LineTo(230, 0);
shape = workbook.Worksheets[0].Shapes.AddFreeform(10, 0, 1, 0, 0, w, new ShapePath[] { shapePath });
shape.Line.FillType = FillType.Solid;
shape.Line.SolidFill.Color = Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;


//Inserts a curve with an arrowhead and two turning points.
shapePath = new ShapePath();
shapePath.MoveTo(0, 0);
shapePath.LineTo(0, 5);
shapePath.LineTo(230, 5);
shapePath.LineTo(230, 30);
shape = workbook.Worksheets[0].Shapes.AddFreeform(11, 0, 1, 0, h, w, new ShapePath[] { shapePath });
shape.Line.FillType = FillType.Solid;
shape.Line.SolidFill.Color = Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;

@duojie.yang
i can’t draw a arrow head using above code

here is my sample code

chartYPosition = (chartYPosition / rowHeight) * rowHeightPixels; // chart y
int height = 10;
int width = (int)Math.Abs(alcLineWidth);
int X = (int)(xx1 + alcLineX);
int Y = (int)(Top + chartYPosition + 9);										

ShapePath shapePath = new ShapePath();
shapePath.MoveTo(0, 0);
shapePath.LineTo(width, 0);
shapePath.Close();

Shape shape = worksheet.Shapes.AddFreeform(0, 0, 0, 0, 0, 0, new ShapePath[] { shapePath });
shape.Placement = PlacementType.MoveAndSize; //Set the placement type as the Shapes will move and resize with cells.
shape.WidthPt = alcLineWidth * widthInPoints;
shape.HeightPt = height;
shape.X = X;
shape.Y = Y;
shape.Line.FillType = FillType.Solid;
shape.Line.SolidFill.Color = ColorTranslator.FromOle(alcLineColor); // Set the line color
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.Arrow;

@tejas.patel,

I tested the following sample code and it works absolutely fine. All arrow heads of the shapes are “ArrowStealth” except the last line arrow head which is “Arrow” as I changed it. Please find attached the output Excel file (containing the shapes) for your reference.
e.g.,
Sample code:

Workbook workbook = new Workbook();

int w = 230, h = 30;
//Inserts a curve with an arrowhead and only one turning point.
//shape1
ShapePath shapePath = new ShapePath();
shapePath.MoveTo(0, 0);
shapePath.LineTo(230, 0);
shapePath.LineTo(230, 30);
Aspose.Cells.Drawing.Shape shape = workbook.Worksheets[0].Shapes.AddFreeform(1, 0, 1, 0, h, w, new ShapePath[] { shapePath });
shape.Line.FillType = Aspose.Cells.Drawing.FillType.Solid;
shape.Line.SolidFill.Color = System.Drawing.Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;//Select the type of arrow you want
  
//shape2
shape = workbook.Worksheets[0].Shapes.AddAutoShape(AutoShapeType.BentConnector2, 5, 0, 1, 0, h, w);
shape.Line.FillType = Aspose.Cells.Drawing.FillType.Solid;
shape.Line.SolidFill.Color = System.Drawing.Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;

//Three ways to insert a straight arrow 
//line1
shape = workbook.Worksheets[0].Shapes.AddAutoShape(AutoShapeType.Line, 8, 0, 1, 0, 0, w);
shape.Line.FillType = Aspose.Cells.Drawing.FillType.Solid;
shape.Line.SolidFill.Color = System.Drawing.Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;

//line2
shape = workbook.Worksheets[0].Shapes.AddLine(9, 0, 1, 0, 0, w);
shape.Line.FillType = Aspose.Cells.Drawing.FillType.Solid;
shape.Line.SolidFill.Color = System.Drawing.Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;

//line3
shapePath = new ShapePath();
shapePath.MoveTo(0, 0);
shapePath.LineTo(230, 0);
shape = workbook.Worksheets[0].Shapes.AddFreeform(10, 0, 1, 0, 0, w, new ShapePath[] { shapePath });
shape.Line.FillType = Aspose.Cells.Drawing.FillType.Solid;
shape.Line.SolidFill.Color = System.Drawing.Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowStealth;


//Inserts a curve with an arrowhead and two turning points.
shapePath = new ShapePath();
shapePath.MoveTo(0, 0);
shapePath.LineTo(0, 5);
shapePath.LineTo(230, 5);
shapePath.LineTo(230, 30);
shape = workbook.Worksheets[0].Shapes.AddFreeform(11, 0, 1, 0, h, w, new ShapePath[] { shapePath });
shape.Line.FillType = Aspose.Cells.Drawing.FillType.Solid;
shape.Line.SolidFill.Color = System.Drawing.Color.Blue;
shape.Line.EndArrowheadStyle = MsoArrowheadStyle.Arrow;

// Save the workbook
workbook.Save("e:\\test2\\out1.xlsx");

out1.zip (7.3 KB)

I am using latest version/fix: Aspose.Cells v25.7 (please try it). If you still find the issue with Aspose.Cells v25.7, kindly do provide complete runnable sample code (same as above) with sample Excel file(s). We will check it soon.

1 Like

@amjad.sahi @duojie.yang
Thank you

@tejas.patel,

You are welcome. Should you have further queries or comments, please feel free to write us back.