在工作表内旋转带有形状的文本
Contents
[
Hide
]
可能的使用场景
您可以使用 Microsoft Excel 在任何形状内添加文本。如果您使用非常旧的 Microsoft Excel 2003 添加形状,那么文本将不会随形状一起旋转。但是,如果您使用 Microsoft Excel 的较新版本(例如 2007、2010、2013 或 2016 等)添加形状,则文本将随形状一起旋转。您可以控制文本是否应随形状旋转或不使用ShapeTextAlignment.RotateTextWithShape财产。它的默认值为真的这意味着文本将随形状旋转,但如果您将其设置为错误的那么文字将不会随形状旋转。
在工作表内旋转带有形状的文本
下面的示例代码加载示例 Excel 文件具有三角形形状,其文本随形状旋转。如果您在 Microsoft Excel 中打开示例 Excel 文件并旋转三角形,文本也会随之旋转。然后代码设置ShapeTextAlignment.RotateTextWithShape财产作为错误的并将其另存为输出Excel文件.如果现在在 Microsoft Excel 中打开输出 Excel 文件并旋转三角形,文本将不会随之旋转。请参阅以下屏幕截图,显示代码对示例 Excel 文件的影响以供参考。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |