event shape3.zip (11.5 KB)
Cause
To draw a shape with a cross line in Aspose.Cells, you can utilize the drawing features available in the library. This involves creating a shape and then overlaying lines to achieve the desired cross effect.
Solution
You can achieve this by first adding a shape (like a rectangle or any other shape) and then drawing two lines across it. Below is a sample code snippet that demonstrates how to do this.
Code
Here’s a simple example in C#:
// Create a new workbook
var workbook = new Workbook();
// Access the first worksheet
var sheet = workbook.Worksheets[0];
// Add a rectangle shape
int shapeIndex = sheet.Shapes.AddAutoShape(ShapeType.Rectangle, 1, 1, 100, 50);
var shape = sheet.Shapes[shapeIndex];
// Set the fill color of the shape
shape.FillFormat.FillType = FillType.Solid;
shape.FillFormat.SolidFillColor.Color = Color.LightBlue;
// Draw a cross line
int lineIndex1 = sheet.Shapes.AddLine(1, 1, 100, 50);
int lineIndex2 = sheet.Shapes.AddLine(1, 50, 100, 1);
// Access the lines and set their properties
var line1 = sheet.Shapes[lineIndex1];
var line2 = sheet.Shapes[lineIndex2];
line1.LineFormat.LineStyle = LineStyle.Single;
line1.LineFormat.LineColor.Color = Color.Red;
line2.LineFormat.LineStyle = LineStyle.Single;
line2.LineFormat.LineColor.Color = Color.Red;
// Save the workbook
workbook.Save("CrossShape.xlsx");
In this code:
- A rectangle shape is created and filled with a light blue color.
- Two lines are drawn to create a cross effect over the rectangle.
- The lines are styled with a red color.
Next Steps
- Adjust the coordinates of the lines if you need to change their position or length.
- Experiment with different shapes and colors to fit your design requirements.
- Save the workbook to view the result in Excel.
If you have any further questions or need additional assistance, feel free to ask!
@tejas.patel
Would you like to provide the expected Excel result file and executable test code? You can compress the code into zip format and upload it here. We will check it soon.
@tejas.patel
Would you like to provide runnable test code? The fragmented code in the example file contains many unknown variables. If you can provide complete and executable test code, it will be very helpful for us to locate the issue. We will check it soon.
@tejas.patel
Here is a sample code, hope it helps.
In addition, for more complex shape combinations, a more convenient and efficient method is to use other tools to generate images and then insert the generated images.
If you encounter any problems, please contact us.
Workbook workbook1 = new Workbook();
ShapeCollection shapes = workbook1.Worksheets[0].Shapes;
Color lineColor1 = Color.FromArgb(193, 193, 193);
Color lineColor2 = Color.FromArgb(242, 242, 242);
Color fillColor1 = Color.FromArgb(230, 243, 247);
Color fillColor2 = Color.FromArgb(255, 233, 236);
int rectW = 16;
int rectH = 43;
Shape shape;
for (int i = 0; i < 16; i++)
{
shape = shapes.AddRectangle(0, 0, 0, 0, rectH, rectW);
shape.X = rectW * i;
//shape.Y = 0;
shape.Line.FillType = FillType.None;
if (i == 2 || i == 9)
{
shape.Fill.SolidFill.Color = fillColor1;
}
else if (i == 3 || i == 10)
{
shape.Fill.SolidFill.Color = fillColor2;
}
else
{
shape.Fill.SolidFill.Color = Color.White;
}
}
shape = shapes.AddLine(0, 0, 0, 0, 0, 256);
shape.Y = 3;
shape.Line.SolidFill.Color = lineColor2;
for (int i = 1; i < 16; i++)
{
shape = shapes.AddLine(0, 0, 0, 0, rectH, 0);
shape.X = rectW * i;
shape.Line.SolidFill.Color = lineColor1;
}
ShapePath shapePath = new ShapePath();
shapePath.MoveTo(15, 0);
shapePath.LineTo(230, 0);
shapePath.LineTo(230, 15);
shapePath.LineTo(0, 15);
shapePath.Close();
shape = shapes.AddFreeform(0, 0, 0, 0, 15, 230, new ShapePath[] { shapePath });
shape.Fill.FillType = FillType.Solid;
shape.Fill.SolidFill.Color = Color.FromArgb(255,255,204);
shape.X = 11;
shape.Y = 17;
Shape eventThirdShape = shapes.Group(shapes.ToArray());
eventThirdShape.X = 100;
eventThirdShape.Y = 100;
workbook1.Save("resultFile.xlsx");