在工作表内旋转文本与形状

可能的使用场景

您可以在Microsoft Excel中的任何形状中添加文本。如果您使用非常旧的Microsoft Excel 2003添加形状,则文本不会随形状旋转。但是,如果您使用较新的版本的Microsoft Excel,例如2007、2010、2013或2016等添加形状,则文本将随形状旋转。您可以使用ShapeTextAlignment.RotateTextWithShape属性控制文本是否应随形状旋转。其默认值为true,这意味着文本将随形状旋转,但如果将其设置为false,则文本将不会随形状旋转。

在工作表内旋转文本与形状

以下示例代码加载包含三角形形状及其文本随形状旋转的示例Excel文件(64716896.xlsx)。如果在Microsoft Excel中打开示例Excel文件并旋转三角形形状,则文本也将随之旋转。然后将ShapeTextAlignment.RotateTextWithShape属性设置为false并保存为输出Excel文件。现在,在Microsoft Excel中打开输出Excel文件并旋转三角形形状,文本将不会随之旋转。请参阅以下屏幕截图,以了解代码对示例Excel文件的影响。

todo:image_alt_text

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Load sample Excel file.
Workbook wb = new Workbook(sourceDir + "sampleRotateTextWithShapeInsideWorksheet.xlsx");
//Access first worksheet.
Worksheet ws = wb.Worksheets[0];
//Access cell B4 and add message inside it.
Cell b4 = ws.Cells["B4"];
b4.PutValue("Text is not rotating with shape because RotateTextWithShape is false.");
//Access first shape.
Shape sh = ws.Shapes[0];
//Access shape text alignment.
Aspose.Cells.Drawing.Texts.ShapeTextAlignment shapeTextAlignment = sh.TextBody.TextAlignment;
//Do not rotate text with shape by setting RotateTextWithShape as false.
shapeTextAlignment.RotateTextWithShape = false;
//Save the output Excel file.
wb.Save(outputDir + "outputRotateTextWithShapeInsideWorksheet.xlsx");