ワークシート内の結合セルを検出

デモンストレーション

この例では、MergeTrialというテンプレートのMicrosoft Excelファイルを使用しています。そのシートには複数の結合セル領域も含まれています。

テンプレートファイル

todo:image_alt_text

Aspose.Cellsは、すべての結合セルを取得するために使用されるCells.getMergedCells()メソッドを提供します。

下記のコードを実行すると、シートの内容をクリアし、すべてのセル領域のマージを解除してファイルを再保存します。

出力ファイル

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

関連記事