Fusión y desfusión Cells

Fusionando Cells en una hoja de trabajo.

Usando Microsoft Excel

Los siguientes pasos describen cómo combinar celdas en la hoja de trabajo usando Microsoft Excel.

  1. Copie los datos que desee en la celda superior izquierda dentro del rango.
  2. Seleccione las celdas que desea fusionar.
  3. Para combinar celdas en una fila o columna y centrar el contenido de la celda, haga clic enFusionar y centro icono en elFormateo barra de herramientas.

Usando Aspose.Cells

ÉlCells class tiene algunos métodos útiles para la tarea. Por ejemplo, el métodounir() combina las celdas en una sola celda dentro de un rango específico de celdas.

El siguiente resultado se genera después de ejecutar el siguiente código.

Las celdas (C6:E7) se han fusionado

todo:imagen_alternativa_texto

Ejemplo de código

El siguiente ejemplo muestra cómo fusionar celdas (C6:E7) en una hoja de trabajo.

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

Dejar de fusionar (Dividir) Fusionado Cells

Usando Microsoft Excel

Los siguientes pasos describen cómo dividir celdas combinadas usando Microsoft Excel.

  1. Seleccione la celda combinada. Cuando las células se han combinado,Fusionar y centro se selecciona en elFormateo barra de herramientas.
  2. Hacer clicFusionar y centro sobre elFormateo barra de herramientas.

Usando Aspose.Cells

ÉlCells la clase tiene un método llamadodesfusionar() que divide las células en su estado original. El método separa las celdas usando la referencia de la celda en el rango de celdas combinadas.

Ejemplo de código

El siguiente ejemplo muestra cómo dividir las celdas combinadas (C6). El ejemplo usa el archivo creado en el ejemplo anterior y divide las celdas combinadas.

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

Artículos relacionados