Insertar, eliminar filas y columnas

Introducción

Ya sea creando una nueva hoja de cálculo desde cero o trabajando en una hoja de cálculo existente, es posible que necesitemos agregar filas o columnas adicionales para acomodar más datos. Inversamente, es posible que también necesitemos eliminar filas o columnas de posiciones específicas en la hoja de cálculo. Para cumplir con estos requisitos, Aspose.Cells proporciona un conjunto muy simple de clases y métodos, que se discuten a continuación.

Gestión de filas y columnas

Aspose.Cells proporciona una clase, Workbook, que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección Worksheets que permite acceder a cada hoja de cálculo en un archivo de Excel. Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet proporciona una colección Cells que representa todas las celdas en la hoja de cálculo.

La colección Cells proporciona varios métodos para gestionar filas y columnas en una hoja de cálculo. Algunos de estos se discuten a continuación.

Insertar una fila

Inserte una fila en la hoja de cálculo en cualquier ubicación llamando al método InsertRow de la colección Cells. El método InsertRow toma el índice de la fila donde se insertará la nueva fila.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Inserting a column into the worksheet at 2nd position
worksheet.GetCells().InsertColumn(1);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();

Insertar Múltiples Filas

Para insertar varias filas en una hoja de cálculo, llame al método InsertRows de la colección Cells. El método InsertRows toma dos parámetros:

  • Índice de la fila, el índice de la fila desde donde se insertarán las nuevas filas.
  • Número de filas, el número total de filas que se deben insertar.
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Inserting 10 rows into the worksheet starting from 3rd row
worksheet.GetCells().InsertRows(2, 10);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();

Eliminar Múltiples Filas

Para eliminar varias filas de una hoja de cálculo, llame al método DeleteRows de la colección Cells. El método DeleteRows toma dos parámetros:

  • Índice de fila, el índice de la fila desde donde se eliminarán las filas.
  • Número de filas, el número total de filas que deben ser eliminadas.
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Deleting 10 rows from the worksheet starting from 3rd row
worksheet.GetCells().DeleteRows(2, 10);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();

Insertar una columna

Los desarrolladores también pueden insertar una columna en la hoja de cálculo en cualquier ubicación llamando al método InsertColumn de la colección Cells. El método InsertColumn toma el índice de la columna donde se insertará la nueva columna.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Inserting a column into the worksheet at 2nd position
worksheet.GetCells().InsertColumn(1);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();

Eliminar una columna

Para eliminar una columna de la hoja de cálculo en cualquier ubicación, llame al método DeleteColumn de la colección Cells. El método DeleteColumn toma el índice de la columna a eliminar.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Deleting a column from the worksheet at 2nd position
worksheet.GetCells().DeleteColumn(1);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();