Infoga, ta bort rader och kolumner

Introduktion

Oavsett om en ny arbetsbok skapas från grunden eller om man arbetar i en befintlig arbetsbok kan det vara nödvändigt att lägga till extra rader eller kolumner för att rymma mer data. Tvärtom kan det också vara nödvändigt att ta bort rader eller kolumner från specificerade positioner i arbetsboken. För att uppfylla dessa krav tillhandahåller Aspose.Cells en mycket enkel uppsättning klasser och metoder, som diskuteras nedan.

Hantera rader och kolumner

Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Microsoft Excel-fil. Klassen Workbook innehåller en samling Worksheets som möjliggör åtkomst till varje kalkylblad i en Excel-fil. Ett kalkylblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller en samling Cells som representerar alla celler på kalkylarket.

Samlingen Cells tillhandahåller flera metoder för att hantera rader och kolumner i ett kalkylblad. Några av dessa diskuteras nedan.

Infoga en rad

Infoga en rad i kalkylarket på valfri plats genom att anropa metoden InsertRow i samlingen Cells. Metoden InsertRow tar indexet för raden där den nya raden ska infogas.

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();

Infoga flera rader

För att infoga flera rader i ett kalkylblad, anropa metoden InsertRows i samlingen Cells. Metoden InsertRows tar två parametrar:

  • Radindex, index för raden från vilken de nya raderna ska infogas.
  • Antal rader, det totala antalet rader som ska infogas.
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();

Ta bort flera rader

För att ta bort flera rader från ett kalkylblad, anropa metoden DeleteRows i samlingen Cells. Metoden DeleteRows tar två parametrar:

  • Radindex, index för raden från vilken raderna ska tas bort.
  • Antal rader, det totala antalet rader som ska tas bort.
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();

Infoga en kolumn

Utvecklare kan också infoga en kolumn i kalkylarket på valfri plats genom att anropa metoden InsertColumn i samlingen Cells. Metoden InsertColumn tar indexet för kolumnen där den nya kolumnen ska infogas.

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();

Ta bort en kolumn

För att ta bort en kolumn från kalkylarket på valfri plats, anropa metoden DeleteColumn i samlingen Cells. Metoden DeleteColumn tar indexet för kolumnen som ska tas bort.

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();