配置設定

配置設定の構成

Microsoft Excelの配置設定

セルの書式設定にMicrosoft Excelを使用したことがある人であれば、Microsoft Excelの配置設定に精通しているでしょう。

上記の図から分かるように、異なる種類の配置オプションがあります:

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

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

Aspose.Cellsの配置設定

Aspose.Cellsは、GetStyle およびSetStyle メソッドを提供しています。これらはCell クラスで使用され、セルの書式設定を取得および設定します。Style クラスには、配置設定を構成するための便利なプロパティが提供されています。

TextAlignmentType 列挙型を使用して任意のテキスト配置タイプを選択します。TextAlignmentType 列挙型の事前定義されたテキスト配置タイプは次のとおりです:

テキスト配置タイプ 説明
Bottom 下部のテキスト配置を表します。
Center 中央のテキスト配置を表します。
CenterAcross 横方向に中央揃えのテキスト配置を表します。
Distributed 分散テキスト配置を表します。
Fill 塗りつぶしのテキスト配置を表します。
General 一般的なテキスト配置を表します。
Justify 両端揃えのテキスト配置を表します。
Left 左揃えのテキスト配置を表します。
Right 右揃えのテキスト配置を表します。
Top 上部のテキスト配置を表します。
JustifiedLow アラビア語のテキストに対して調整されたカシダ長でテキストを配置します。
ThaiDistributed 特にタイ語のテキストを分散配置し、各文字を単語として扱います。

水平、垂直配置、およびインデント

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

// 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");

方向

セル内のテキストの方向(回転)をRotationAngleプロパティで設定します。

// 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");

収縮に合わせる

フィールド内のテキストを折り返すオプションとして、セルの寸法に合わせてテキストサイズを縮小することができます。これはShrinkToFitプロパティをtrueに設定することで行います。

// 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");

セルの結合

Microsoft Excelと同様に、Aspose.Cellsでは複数のセルを1つに結合することができます。Aspose.Cellsにはこのタスクを実行するための2つのアプローチがあります。1つの方法は、Mergeメソッドを呼び出すことです。メソッドは、以下のパラメータを取り、セルを結合します:

  • 最初の行: 結合の開始行。
  • 最初の列: 結合の開始列。
  • 行数: 結合する行数。
  • 列数: 結合する列数。
// 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);

テキストの方向

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

読み取り順は TextDirection プロパティによって設定されます。 Aspose.Cells は TextDirectionType 列挙型で事前定義されたテキストの方向を提供します。

テキスト方向の種類 説明
Context 最初に入力された文字の言語と一貫した読み取り順
LeftToRight 左から右の読み取り順
RightToLeft 右から左の読み取り順
// 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");

高度なトピック