在工作表中检测合并的 Cells
Contents
[
Hide
]
在 Microsoft Excel 中,可以将多个单元格合并为一个。这通常用于创建复杂的表格或创建包含跨越多列的标题的单元格。
Aspose.Cells 允许您识别工作表中的合并单元格区域。您也可以取消合并它们。本文提供了使用 Aspose.Cells 执行任务的最简单的代码行。
本文提供了有关如何在工作表中查找并取消合并合并单元格的简要说明。
示范
此示例使用名为 Microsoft 的 Excel 文件模板合并试验.它在工作表中有一些合并的单元格区域,也称为合并试验。
模板文件
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"); |