ワークシートでマージされた Cells を検出する
Contents
[
Hide
]
Microsoft Excel では、複数のセルを 1 つに結合できます。これは、複雑なテーブルを作成したり、複数の列にまたがる見出しを保持するセルを作成したりするためによく使用されます。
Aspose.Cells を使用すると、ワークシート内の結合されたセル領域を識別できます。それらをマージ解除することもできます。この記事では、Aspose.Cells を使用してタスクを実行するための最も単純なコード行を提供します。
この記事では、ワークシート内の結合セルを検索して結合解除する方法について簡潔に説明します。
デモンストレーション
この例では、テンプレート Microsoft という Excel ファイルを使用します。マージトライアルMerge Trial とも呼ばれるシートにいくつかの結合されたセル領域があります。
テンプレートファイル
Aspose.Cells はCells.getMergedCells結合されたセル領域の ArrayList を取得するために使用されるメソッド。
以下のコードが実行されると、シートの内容がクリアされ、ファイルを再度保存する前にすべてのセル領域が結合解除されます。
出力ファイル
コード例
次のサンプル コードを参照して、ワークシート内の結合されたセル領域を特定し、結合を解除する方法を見つけてください。
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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DetectMergedCells.class); | |
// Instantiate a new Workbook | |
Workbook wkBook = new Workbook(dataDir + "MergeTrial.xls"); | |
// Get a worksheet in the workbook | |
Worksheet wkSheet = wkBook.getWorksheets().get("Merge Trial"); | |
// Clear its contents | |
wkSheet.getCells().clearContents(0, 0, wkSheet.getCells().getMaxDataRow(), | |
wkSheet.getCells().getMaxDataColumn()); | |
// Get all merged cell aeras | |
CellArea[] areas = wkSheet.getCells().getMergedAreas(); | |
// Define some variables | |
int frow, fcol, erow, ecol; | |
// Loop through the arraylist and get each cellarea to unmerge it | |
for (int i = areas.length - 1; i > -1; i--) | |
{ | |
frow = areas[i].StartRow; | |
fcol = areas[i].StartColumn; | |
erow = areas[i].EndRow; | |
ecol = areas[i].EndColumn; | |
wkSheet.getCells().unMerge(frow, fcol, erow, ecol); | |
} | |
// Save the excel file | |
wkBook.save(dataDir + "output_MergeTrial.xls"); |