Insertar, eliminar filas y columnas
Introducción
Ya sea creando una nueva hoja de trabajo desde cero o trabajando en una hoja de trabajo existente, es posible que necesitemos agregar filas o columnas adicionales para acomodar más datos. A la inversa, es posible que también necesitemos eliminar filas o columnas de posiciones específicas en la hoja de trabajo. Para cumplir con estos requisitos, Aspose.Cells proporciona un conjunto de clases y métodos muy simple, que se analiza a continuación.
Gestión de filas y columnas
Aspose.Cells proporciona una clase,ILibro de trabajo , que representa un archivo de Excel Microsoft. ÉlILibro de trabajo la clase contiene unIHojas de trabajo colección que permite el acceso a cada hoja de trabajo en un archivo de Excel. Una hoja de trabajo está representada por elIHoja de trabajo clase. ÉlIHoja de trabajo la clase proporciona unaICélulascolección que representa todas las celdas de la hoja de trabajo.
ÉlICélulasLa colección proporciona varios métodos para administrar filas y columnas en una hoja de cálculo. Algunos de éstos se discuten a continuación.
Insertar una fila
Inserte una fila en la hoja de trabajo en cualquier lugar llamando alInsertar fila metodo de laICélulas recopilación. ÉlInsertar filaEl método toma el índice de la fila donde se insertará la nueva fila.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Inserting a row into the worksheet at 3rd position | |
worksheet->GetICells()->InsertRow(2); | |
//Save the Excel file. | |
workbook->Save(outputInsertingDeletingRowsAndColumns); | |
Insertar varias filas
Para insertar varias filas en una hoja de trabajo, llame alInsertar filas metodo de laICélulas recopilación. ÉlInsertar filasEl método toma dos parámetros:
- Índice de 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 deben insertarse.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet->GetICells()->InsertRows(2, 10); | |
//Save the Excel file. | |
workbook->Save(outputInsertingDeletingRowsAndColumns); |
Eliminación de varias filas
Para eliminar varias filas de una hoja de trabajo, llame alEliminar filas metodo de laICélulas recopilación. ÉlEliminar filasEl método 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 eliminarse.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet->GetICells()->DeleteRows(2, 10); | |
//Save the Excel file. | |
workbook->Save(outputInsertingDeletingRowsAndColumns); |
Insertar una columna
Los desarrolladores también pueden insertar una columna en la hoja de trabajo en cualquier ubicación llamando alInsertarColumna metodo de laICélulas recopilación.InsertarColumnaEl método toma el índice de la columna donde se insertará la nueva columna.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Inserting a column into the worksheet at 2nd position | |
worksheet->GetICells()->InsertColumn(1); | |
//Save the Excel file. | |
workbook->Save(outputInsertingDeletingRowsAndColumns); |
Eliminar una columna
Para eliminar una columna de la hoja de cálculo en cualquier lugar, llame alEliminarColumna metodo de laICélulas recopilación. ÉlEliminarColumnaEl método toma el índice de la columna a eliminar.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleDeleteColumn = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputDeleteColumn = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleDeleteColumn); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Deleting a column from the worksheet at 2nd position | |
worksheet->GetICells()->DeleteColumn(4); | |
//Save the Excel file. | |
workbook->Save(outputDeleteColumn); |