アライメント設定

配置設定の構成

Microsoft Excelでの配置設定

Microsoft Excel を使用してセルをフォーマットしたことがある人は、Microsoft Excel の配置設定に慣れているでしょう。

上の図からわかるように、さまざまな種類の配置オプションがあります。

  • テキストの配置 (水平および垂直)
  • インデント。
  • オリエンテーション。
  • テキスト コントロール。
  • テキスト方向。

これらの配置設定はすべて Aspose.Cells で完全にサポートされており、以下で詳しく説明します。

Aspose.Cellsのアライメント設定

Aspose.Cells提供GetStyleスタイルの設定のメソッドCellセルのフォーマットを取得および設定するために使用されるクラス。のスタイルクラスは、配置設定を構成するための便利なプロパティを提供します。

を使用して、任意のテキスト配置タイプを選択します。TextAlignmentType列挙。で定義済みのテキスト配置タイプTextAlignmentType列挙は次のとおりです。

テキスト配置タイプ 説明
下のテキストの配置を表します
中心 テキストの中央揃えを表します
センターアクロス テキストの中央揃えを表します
分散 分散されたテキストの配置を表します
塗りつぶし 塗りつぶしテキストの配置を表します
全般的 一般的なテキストの配置を表します
正当化する テキストの両端揃えを表します
テキストの左揃えを表します
テキストの右揃えを表します
テキストの上揃えを表します
JustifiedLow アラビア語テキストの kashida の長さを調整して、テキストを揃えます。
タイ語分散 各文字が単語として扱われるため、特にタイ語のテキストを配布します。

水平方向、垂直方向の配置とインデント

使用HorizontalAlignmentテキストを水平方向に揃えるプロパティVerticalAlignmentテキストを垂直方向に配置するプロパティ。 セル内のテキストのインデント レベルを設定することができます。インデントレベル財産 tt は、水平方向の配置が左または右の場合にのみ有効です。

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.getStyle();
//Set text left horizontal alignment
style.setHorizontalAlignment(TextAlignmentType.RIGHT);
//Set indent
style.setIndentLevel(4);
//Set text top vertical alignment
style.setVerticalAlignment(TextAlignmentType.TOP);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

オリエンテーション

でセル内のテキストの向き (回転) を設定します。回転角度財産。

// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.getStyle();
// Setting the rotation of the text (inside the cell) to 25
style.setRotationAngle(25);
cell.setStyle(style);
//Accessing the "A2" cell from the worksheet
cell = worksheet.getCells().get("A2");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A2" cell
style = cell.getStyle();
// Setting the orientation of the text from top to bottom
style.setRotationAngle(255);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

テキスト コントロール

次のセクションでは、テキストの折り返し、サイズに合わせて縮小、およびその他の書式設定オプションを設定して、テキストを制御する方法について説明します。

テキストの折り返し

セル内でテキストを折り返すと、読みやすくなります。セルの高さは、テキストが切り取られたり、隣接するセルにあふれたりするのではなく、すべてのテキストに合わせて調整されます。でテキストの折り返しをオンまたはオフに設定しますIsTextWrapped財産。

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality.");
// Gets style
Style style = cell.getStyle();
// Wrap Cell's Text wrap
style.setTextWrapped( true);
//Set style.
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

縮小してフィット

フィールド内のテキストを折り返すオプションの 1 つは、セルの寸法に合わせてテキスト サイズを縮小することです。これは、縮小して合わせる財産。に真実.

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality.");
// Gets style in the "A1" cell
Style style = cell.getStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.setShrinkToFit(true);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

合併 Cells

Microsoft Excel と同様に、Aspose.Cells は複数のセルを 1 つに結合することをサポートしています。 Aspose.Cells は、このタスクに対する 2 つのアプローチを提供します。 1 つの方法は、[マージ](https://reference.aspose.com/cells/java/com.aspose.cells/cells#merge(int,%20int,%20int,%20int)) 方法。このメソッドは、次のパラメーターを使用してセルをマージします。

  • 最初の行: マージを開始する最初の行。
  • 最初の列: マージを開始する最初の列。
  • 行数: 結合する行数。
  • 列数: マージする列の数。
// Create a Cells object ot fetch all the cells.
Cells cells = worksheet.getCells();
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.merge(5, 2, 2, 3);
// Input data into C6 Cell.
worksheet.getCells().get(5, 2).putValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
Style style = worksheet.getCells().get(5, 2).getStyle();
// Create a Font object
Font font = style.getFont();
// Set the name.
font.setName("Times New Roman");
// Set the font size.
font.setSize(18);
// Set the font color
font.setColor(Color.getBlue());
// Bold the text
font.setBold(true);
// Make it italic
font.setItalic(true);
// Set the backgrond color of C6 Cell to Red
style.setForegroundColor(Color.getRed());
style.setPattern(BackgroundType.SOLID);
// Apply the Style to C6 Cell.
cells.get(5, 2).setStyle(style);

テキスト方向

セル内のテキストの読み上げ順序を設定することができます。読み順とは、文字や単語などが表示される視覚的な順序です。たとえば、英語は左から右への言語ですが、アラビア語は右から左への言語です。

読み上げ順序はテキスト方向財産。 Aspose.Cells は、TextDirectionType列挙。

テキストの方向の種類 説明
コンテクスト 最初に入力された文字の言語と一致する読み順
左から右へ 左から右の読み順
右から左に 右から左の読み順
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality.");
// Gets style in the "A1" cell
Style style = cell.getStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.setTextDirection(TextDirectionType.LEFT_TO_RIGHT);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

先行トピック