Free Support Forum - aspose.com

Adding a line shape to excel

Hi,

Can you please let me know how to add line shape in 7.0.2 version of aspose cells for java.

From the documentaion I found the following

//Instantiate a new Workbook.
Workbook workbook = new Workbook();

//Get the first worksheet in the book.
Worksheet worksheet = workbook.getWorksheets().get(0);

//Add a new line to the worksheet.
LineShape line1 = worksheet.getShapes().addLine(5, 0, 1, 0, 0, 250);


But there is no method addLine in ShapeCollection.

Thanks!!

Hi,

Please use the following code. Please see the output file generated by this code.

Java


Workbook workbook = new Workbook();


Worksheet worksheet = workbook.getWorksheets().get(0);


worksheet.getShapes().addAutoShape(AutoShapeType.LINE, 0, 10, 0, 10, 150, 150);


workbook.save(“F:\Shak-Data-RW\Downloads\LineShape.xlsx”);

Thanks,


But how can I access that Line shape after adding? I need to apply some other properties line rotation angle, width, arrow type to that line.

I tried to use the following, but it throws exception at line 1; saying it can’t be cast to LineShape.
com.aspose.cells.LineShape vLine = (LineShape) shapes.addAutoShape(AutoShapeType.LINE, 0, 10, 0, 10, 150, 150);
vLine.setRotationAngle(90);
vLine.setWidth(2);
vLine.setPlacement(PlacementType.FREE_FLOATING);


Hi,


Please use addShape() instead. See the following sample code:
e.g
Sample code:

Worksheet worksheet = workbook.getWorksheets().get(0);
ShapeCollection shapes = worksheet.getShapes();
Shape shape = shapes.addShape(MsoDrawingType.LINE, 0, 10, 0, 10, 150, 150);
LineShape vLine = (LineShape)shape;
vLine.setRotationAngle(90);
vLine.setWidth(2);
vLine.setPlacement(PlacementType.FREE_FLOATING);

Thank you.