データの整形

セル内のデータの整形手法

ワークシートセルが適切にフォーマットされていると、ユーザーがセルの内容(データ)を読みやすくなります。セルやその内容をフォーマットする方法はたくさんあります。最も簡単な方法は、デザイナースプレッドシートを作成する際に、Microsoft Excelを使用してセルをフォーマットすることです。デザイナースプレッドシートが作成されたら、Aspose.Cellsを使用してスプレッドシートを開くことができます。スプレッドシートとともにすべてのフォーマット設定が保存されます。セルやその内容をフォーマットする別の方法は、Aspose.Cells APIを使用することです。このトピックでは、Aspose.Cells APIを使用してセルやその内容をフォーマットする2つのアプローチを説明します。

セルの書式設定

開発者はAspose.Cellsの柔軟なAPIを使用してセルとその内容をフォーマットすることができます。Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供しています。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetCollectionが含まれています。ワークシートはWorksheetクラスによって表されます。WorksheetクラスはCellsコレクションを提供します。Cellsコレクション内の各アイテムはCellクラスのオブジェクトを表します。

Aspose.Cellsは、セルのフォーマットスタイルを設定するためにCellクラスのStyleプロパティを提供し、同様の目的で使用されるStyleクラスも提供しています。セルに背景色や前景色、ボーダー、フォント、水平および垂直の配置、インデントレベル、テキストの方向、回転角など、さまざまな種類のフォーマットスタイルを適用します。

setStyleメソッドの使用

異なるセルに異なるフォーマットスタイルを適用する場合は、CellクラスのsetStyleメソッドを使用すると良いです。以下は、セルにさまざまなフォーマット設定を適用するためにsetStyleメソッドを使用する例です。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(FormattingCellsUsingsetStyleMethod.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Accessing the "A1" cell from the worksheet
Cell cell = cells.get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello Aspose!");
Style style = cell.getStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the "A1" cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Setting the cell to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "output.xls");

スタイルオブジェクトの使用

異なるセルに同じフォーマットスタイルを適用する場合、Styleオブジェクトを使用します。

  1. WorkbookクラスのcreateStyleメソッドを呼び出して、WorkbookクラスのStylesコレクションにStyleオブジェクトを追加します。
  2. Stylesコレクションから新しく追加されたStyleオブジェクトにアクセスします。
  3. Styleオブジェクトの所定のプロパティを設定して、所望のフォーマット設定を適用します。
  4. 設定済みのStyleオブジェクトを任意のセルのStyleプロパティに割り当てます。

このアプローチは、アプリケーションの効率を大幅に改善し、メモリも節約できます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormattingCellsUsingStyleObject.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Accessing the "A1" cell from the worksheet
Cell cell = cells.get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello Aspose!");
// Adding a new Style to the styles collection of the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the "A1" cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Setting the cell to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "FCUsingStyleObject_out.xls");

グラデーション塗りつぶし効果の適用

セルに所望のグラデーション塗りつぶし効果を適用するには、StyleオブジェクトのsetTwoColorGradientメソッドを使用します。

コード例

以下のコードを実行することで、以下の出力が得られます。

グラデーション塗りつぶし効果の適用

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ApplyGradientFillEffects.class) + "data/";
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet (default) in the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Input a value into B3 cell
worksheet.getCells().get(2, 1).putValue("test");
// Get the Style of the cell
Style style = worksheet.getCells().get("B3").getStyle();
// Set Gradient pattern on
style.setGradient(true);
// Specify two color gradient fill effects
style.setTwoColorGradient(Color.fromArgb(255, 255, 255), Color.fromArgb(79, 129, 189),
GradientStyleType.HORIZONTAL, 1);
// Set the color of the text in the cell
style.getFont().setColor(Color.getRed());
// Specify horizontal and vertical alignment settings
style.setHorizontalAlignment(TextAlignmentType.CENTER);
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Apply the style to the cell
worksheet.getCells().get("B3").setStyle(style);
// Set the third row height in pixels
worksheet.getCells().setRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.getCells().merge(2, 1, 1, 2);
// Save the Excel file
workbook.save(dataDir + "ApplyGradientFillEffects_out.xlsx");

配置設定の構成

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

Microsoft Excelの配置設定

todo:image_alt_text

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

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

配置設定の構成

Aspose.Cellsは、Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetCollectionが含まれています。ワークシートはWorksheetクラスによって表されます。

WorksheetクラスはCellsコレクションを提供します。Cellsコレクションの各アイテムはCellクラスのオブジェクトを表します。

Aspose.CellsはCellクラスでsetStyleメソッドを提供し、セルの書式設定に使用されます。Styleクラスはフォント設定を構成するための便利なプロパティを提供します。

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

テキスト配置タイプ 説明
Bottom 下部のテキスト配置を表します。
Center 中央のテキスト配置を表します。
CenterAcross 横方向に中央揃えのテキスト配置を表します。
Distributed 分散テキスト配置を表します。
Fill 塗りつぶしのテキスト配置を表します。
General 一般的なテキスト配置を表します。
Justify 両端揃えのテキスト配置を表します。
Left 左揃えのテキスト配置を表します。
Right 右揃えのテキスト配置を表します。
Top 上部のテキスト配置を表します。

水平配置

StyleオブジェクトのsetHorizontalAlignmentメソッドを使用して、テキストを水平に配置します。

以下の出力は、以下の例コードを実行することで得られます。

テキストの水平配置

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(TextAlignmentHorizontal.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "TAHorizontal_out.xls");

垂直配置

StyleオブジェクトのsetVerticalAlignmentメソッドを使用して、テキストを垂直に配置します。

垂直配置を中央に設定した場合に得られる出力は次のとおりです。

テキストの垂直配置

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(TextAlignmentVertical.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the vertical alignment of the text in a cell
Style style1 = cell.getStyle();
style1.setVerticalAlignment(TextAlignmentType.CENTER);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "TAVertical_out.xls");

インデント

StyleオブジェクトのsetIndentLevelメソッドを使用して、セル内のテキストのインデントレベルを設定できます。

IndentLevelを2に設定した場合の出力は次のとおりです。

インデントレベルが2に調整されました

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(Indentation.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the vertical alignment of the text in a cell
Style style1 = cell.getStyle();
style1.setIndentLevel(2);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "Indentation_out.xls");

方向

StyleオブジェクトのsetRotationAngleメソッドを使用して、セル内のテキストの向き(回転)を設定します。

回転角度を25に設定した場合の出力は次のとおりです。

回転角度を25に設定しました

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(Orientation.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the rotation of the text (inside the cell) to 25
Style style1 = cell.getStyle();
style1.setRotationAngle(25);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "Orientation_out.xls");

テキストコントロール

次のセクションでは、テキストの折り返し、収縮に合わせるなど、テキストの制御方法について説明します。

テキストの折り返し

セル内のテキストを折り返すと、セルの高さがすべてのテキストに合わせて調整されるため、テキストを切り捨てたり、隣接するセルにこぼれることなく、読みやすくなります。

テキストの折り返しを有効または無効にするには、StyleオブジェクトのsetTextWrappedメソッドを使用します。

テキストの折り返しが有効になっていると、次の出力が得られます。

セル内でテキストが折り返されます

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(WrapText.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Enabling the text to be wrapped within the cell
Style style1 = cell.getStyle();
style1.setTextWrapped(true);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "WrapText_out.xls");

収縮に合わせる

フィールド内のテキストを折り返すオプションは、セルの寸法に合わせてテキストのサイズを縮小することです。これは、StyleオブジェクトのIsTextWrappedプロパティをtrueに設定することで実現されます。

テキストをセルにフィットさせると、次の出力が得られます。

セルの境界内に収縮したテキスト

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ShrinkingToFit.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Shrinking the text to fit according to the dimensions of the cell
Style style1 = cell.getStyle();
style1.setShrinkToFit(true);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "ShrinkingToFit_out.xls");

セルの結合

Microsoft Excelと同様に、Aspose.Cellsでは複数のセルを1つに結合する機能がサポートされています。

最初の行の3つのセルを結合して大きな1つのセルを作成した場合、次の出力が得られます。

3つのセルをまとめて大きなセルを作成しました

todo:image_alt_text

CellsコレクションのMergeメソッドを使用してセルを結合します。Mergeメソッドは次のパラメータを受け取ります:

  • 最初の行、結合を開始する最初の行。
  • 最初の列、結合を開始する最初の列。
  • 行数、結合する行数。
  • 列数、結合する列数。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(MergingCellsInWorksheet.class) + "data/";
// Create a Workbook.
Workbook wbk = new Workbook();
// Create a Worksheet and get the first sheet.
Worksheet worksheet = wbk.getWorksheets().get(0);
// Create a Cells object to 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).setValue("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);
// Save the Workbook.
wbk.save(dataDir + "mergingcells_out.xls");
wbk.save(dataDir + "mergingcells_out.xlsx");
wbk.save(dataDir + "mergingcells_out.ods");
// Print message
System.out.println("Process completed successfully");

テキストの方向

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

読み順は、StyleオブジェクトのTextDirectionプロパティで設定します。Aspose.Cellsは、TextDirectionType列挙型で事前定義のテキスト方向タイプを提供しています。

テキスト方向の種類 説明
Context 最初に入力された文字の言語と一貫した読み取り順
LeftToRight 左から右の読み取り順
RightToLeft 右から左の読み取り順
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ChangeTextDirection.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the text direction from right to left
Style style1 = cell.getStyle();
style1.setTextDirection(TextDirectionType.RIGHT_TO_LEFT);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "ChangeTextDirection_out.xls");

テキストの読み順を右から左に設定した場合、次の出力が得られます。

テキストの読み順を右から左に設定

todo:image_alt_text

セル内の選択された文字のフォーマット設定

フォント設定の取り扱いでは、セルの書式設定について説明しましたが、セル全体の内容のみを書式設定する方法しか説明していません。選択された文字のみを書式設定したい場合はどうすればよいでしょうか?

Aspose.Cellsはこの機能をサポートしています。このトピックでは、この機能の使用方法について説明します。

選択された文字のフォーマット設定

Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。WorkbookクラスにはExcelファイル内の各ワークシートにアクセスできるWorksheetコレクションが含まれています。ワークシートはWorksheetクラスによって表されます。WorksheetクラスはCellsコレクションを提供します。Cellsコレクション内の各アイテムはCellクラスのオブジェクトを表します。

Cellクラスには、セル内の文字の範囲を選択するために使用される以下のパラメータを受け取るcharactersメソッドがあります。

  • 開始インデックス:選択を開始する文字のインデックス。
  • 文字数、選択する文字数。

出力ファイルでは、A1"セルでは、単語’Visit’はデフォルトのフォントでフォーマットされ、‘Aspose!‘は太字で青色にフォーマットされています。

選択された文字のフォーマット設定

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Path to source file
String dataDir = Utils.getSharedDataDir(FormattingSelectedCharacters.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose!");
Font font = cell.characters(6, 7).getFont();
// Setting the font of selected characters to bold
font.setBold(true);
// Setting the font color of selected characters to blue
font.setColor(Color.getBlue());
// Saving the Excel file
workbook.save(dataDir + "FSCharacters_out.xls");

シートのアクティブ化およびワークシート内のセルのアクティブ化またはセル範囲の選択

時々、特定のワークシートをアクティブ化して、誰かがMicrosoft Excelでファイルを開いたときに最初に表示させる必要があります。また、スクロールバーがアクティブセルにスクロールしてそれが明確に見えるように特定のセルをアクティブ化する必要がある場合もあります。Aspose.Cellsは上記のすべてのタスクを行うことができます。

アクティブシートはブック内で作業しているシートです。アクティブシートのタブの名前はデフォルトで太字になっています。一方、アクティブセルは選択され、データの入力が開始されるセルです。一度に1つのセルのみがアクティブです。アクティブセルは他のセルに対して目立つように太い境界で囲まれています。Aspose.Cellsでは、ワークシート内のセル範囲を選択することもできます。

シートのアクティブ化およびセルのアクティブ化

Aspose.Cellsはこれらのタスクに特定のAPIを提供しています。たとえば、WorksheetCollection.setActiveSheetIndexメソッドはアクティブシートを設定するのに便利です。同様に、Worksheet.setActiveCellメソッドはワークシート内のアクティブセルを設定して取得するのに使用されます。

Microsoft Excelでファイルが開かれたときに、水平および垂直スクロールバーを選択されたデータの良い表示を与えるために行インデックスと列インデックス位置にスクロールすることを希望する場合は、Worksheet.setFirstVisibleRowおよびWorksheet.setFirstVisibleColumnプロパティを使用してください。

次の例は、ワークシートをアクティブ化して、そこで特定のセルをアクティブ化し、スクロールバーをスクロールして2行目と2列目を最初の可視行および列として表示する方法を示しています。

B2セルをアクティブセルとして設定

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(MakeCellActive.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).setValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Set B2 cell as an active cell in the worksheet.
worksheet1.setActiveCell("B2");
// Set the B column as the first visible column in the worksheet.
worksheet1.setFirstVisibleColumn(1);
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.setFirstVisibleRow(1);
// Save the Excel file.
workbook.save(dataDir + "MakeCellActive_out.xls");

ワークシート内のセル範囲の選択

Aspose.Cellsは、Worksheet.selectRange(int startRow, int startColumn, int totalRows, int totalColumns, bool removeOthers)メソッドを提供しています。最後のパラメータ - removeOthers - をtrueに設定すると、シート内の他のセルセレクションが削除されます。

次の例は、アクティブなワークシートでセル範囲を選択する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SelectRangeofCellsinWorksheet.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).setValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Select range of cells(A1:E10) in the worksheet.
worksheet1.selectRange(0, 0, 10, 5, true);
// Save the Excel file.
workbook.save(dataDir + "SROfCInWorksheet_out.xlsx");

行と列の書式設定

Excelアプリケーションの最も広く使用されている機能であるスプレッドシート内の行と列の書式設定は、レポートに見栄えを与えるための可能性のある機能です。Aspose.Cells APIも、そのデータモデルを通じてこの機能を提供します。それはフォントとその属性、テキストの配置、背景色/前景色、罫線、数値および日付のリテラルの表示形式など、すべてのスタイリング関連機能を主に担当するStyleクラスを公開しています。Aspose.Cells APIが提供するもう一つの便利なクラスは、StyleFlagであり、フォントの再利用を可能にします。 

この記事では、Aspose.Cells for Java APIを使用して行と列に書式を適用する方法について説明します。 

行および列の書式設定

Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetCollectionが含まれています。ワークシートはWorksheetクラスによって表されます。WorksheetクラスはCellsコレクションを提供します。CellsコレクションはRowsコレクションを提供します。

行の書式設定

Rowsコレクションの各アイテムはRowオブジェクトを表します。Rowオブジェクトでは、行に書式設定を適用するためのapplyStyleメソッドが提供されています。

同じ書式を行に適用するには、Styleオブジェクトを使用してください:

  1. createStyleメソッドを呼び出してWorkbookクラスにStyleオブジェクトを追加してください。
  2. スタイルオブジェクトのプロパティを設定してフォーマット設定を適用します。
  3. 構成されたスタイルオブジェクトを行オブジェクトのapplyStyleメソッドに割り当てます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormattingARow.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Adding a new Style to the styles collection of the Excel object Accessing the newly added Style to the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border of the cell
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBottomBorder(true);
styleFlag.setFontColor(true);
// Accessing a row from the Rows collection
Row row = cells.getRows().get(0);
// Assigning the Style object to the Style property of the row
row.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(dataDir + "FormattingARow_out.xls");

列のフォーマット設定

CellsコレクションにはColumnsコレクションが提供されています。Columnsコレクション内の各項目はColumnオブジェクトを表します。Rowオブジェクトと同様に、Columnオブジェクトは行と同様に列のフォーマット設定を行うために使用されるapplyStyleメソッドを提供します。ColumnオブジェクトのapplyStyleメソッドを使用して、列のフォーマット設定を行います。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormattingAColumn.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Adding a new Style to the styles collection of the Excel object Accessing the newly added Style to the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border of the cell
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBottomBorder(true);
styleFlag.setFontColor(true);
// Accessing a column from the Columns collection
Column column = cells.getColumns().get(0);
// Applying the style to the column
column.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(dataDir + "FormattingAColumn_out.xls");

行と列の数字および日付の表示形式を設定する

行または列全体の数字および日付の表示形式を設定する必要がある場合、上述の手順とほぼ同じ手順になりますが、テキストコンテンツのパラメータを設定する代わりに、Style.NumberまたはStyle.Customを使用して数字や日付のフォーマットを設定します。Style.Numberプロパティは整数型で、組み込みの数値および日付のフォーマットを参照し、一方、Style.Customプロパティは文字列型で有効なパターンを受け入れます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SettingDisplayFormat.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles collection of the Workbook object
Style style = workbook.createStyle();
// Setting the Number property to 4 which corresponds to the pattern #,##0.00
style.setNumber(4);
// Creating an object of StyleFlag
StyleFlag flag = new StyleFlag();
// Setting NumberFormat property to true so that only this aspect takes effect from Style object
flag.setNumberFormat(true);
// Applying style to the first row of the worksheet
worksheet.getCells().getRows().get(0).applyStyle(style, flag);
// Re-initializing the Style object
style = workbook.createStyle();
// Setting the Custom property to the pattern d-mmm-yy
style.setCustom("d-mmm-yy");
// Applying style to the first column of the worksheet
worksheet.getCells().getColumns().get(0).applyStyle(style, flag);
// Saving spreadsheet on disc
workbook.save(dataDir + "SDisplayFormat_out.xlsx");