合并和取消合并 Cells

在工作表中合并 Cells。

使用 Microsoft Excel

以下步骤描述了如何使用 Microsoft Excel 合并工作表中的单元格。

  1. 将所需数据复制到范围内最左上角的单元格中。
  2. 选择要合并的单元格。
  3. 要合并行或列中的单元格并将单元格内容居中,请单击合并和居中上的图标格式化工具栏。

使用 Aspose.Cells

Cells类有一些对任务有用的方法。例如,方法合并() 将单元格合并为指定单元格范围内的单个单元格。

执行下面的代码后会生成以下输出。

单元格 (C6:E7) 已合并

待办事项:图片_替代_文本

代码示例

以下示例显示如何合并工作表中的单元格 (C6:E7)。

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

取消合并(拆分)合并 Cells

使用 Microsoft Excel

以下步骤描述了如何使用 Microsoft Excel 拆分合并的单元格。

  1. 选择合并的单元格。 当单元格合并后,合并和居中被选中的格式化工具栏。
  2. 点击合并和居中格式化工具栏。

使用 Aspose.Cells

Cells类有一个名为取消合并() 将细胞分裂成它们的原始状态。该方法使用合并的单元格区域中的单元格引用取消合并单元格。

代码示例

以下示例显示如何拆分合并的单元格 (C6)。该示例使用在上一个示例中创建的文件并拆分合并的单元格。

// 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(UnMergingCellsInWorksheet.class) + "data/";
// Create a Workbook.
Workbook wbk = new Workbook(dataDir + "mergingcells.xls");
// 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();
// Unmerge the cells.
cells.unMerge(5, 2, 2, 3);
// Save the file.
wbk.save(dataDir + "UnMergingCellsInWorksheet_out.xls");
// Print message
System.out.println("Process completed successfully");

相关文章