ワークシート内の図形と一緒にテキストを回転する
可能な使用シナリオ
Microsoft Excelを使用して、任意の図形にテキストを追加できます。Microsoft Excel 2003以前を使用して図形を追加すると、テキストは図形と一緒に回転しません。ただし、Microsoft Excel 2007、2010、2013または2016などの新しいバージョンを使用して図形を追加すると、テキストは図形と一緒に回転します。テキストが図形と一緒に回転するかどうかをShapeTextAlignment.RotateTextWithShapeプロパティを使用して制御できます。デフォルト値はtrueで、これはテキストが図形と一緒に回転することを意味しますが、falseに設定すると、テキストは図形と一緒に回転しません。
ワークシート内の図形と一緒にテキストを回転する
次のサンプルコードは、図形とそのテキストが一緒に回転する三角形のサンプルExcelファイルをロードします。サンプルExcelファイルをMicrosoft Excelで開き、三角形の図形を回転させると、テキストもそれに従って回転します。次に、ShapeTextAlignment.RotateTextWithShapeプロパティをfalseとして設定し、output Excel fileとして保存します。これで、Microsoft Excelで出力Excelファイルを開き、三角形の図形を回転させると、テキストはそれに従って回転しません。参考のために、サンプルExcelファイルへのコードの効果を示すスクリーンショットを参照してください。
サンプルコード
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
//Load sample Excel file. | |
Workbook wb = new Workbook(srcDir + "sampleRotateTextWithShapeInsideWorksheet.xlsx"); | |
//Access first worksheet. | |
Worksheet ws = wb.getWorksheets().get(0); | |
//Access cell B4 and add message inside it. | |
Cell b4 = ws.getCells().get("B4"); | |
b4.putValue("Text is not rotating with shape because RotateTextWithShape is false."); | |
//Access first shape. | |
Shape sh = ws.getShapes().get(0); | |
//Access shape text alignment. | |
ShapeTextAlignment shapeTextAlignment = sh.getTextBody().getTextAlignment(); | |
//Do not rotate text with shape by setting RotateTextWithShape as false. | |
shapeTextAlignment.setRotateTextWithShape(false); | |
//Save the output Excel file. | |
wb.save(outDir + "outputRotateTextWithShapeInsideWorksheet.xlsx"); |