Inserting and Deleting Rows and Columns of Excel file with C++
Introduction
Whether creating a new worksheet from scratch or working on an existing worksheet, we may need to add extra rows or columns to accommodate more data. Inversely, we may also need to delete rows or columns from specified positions in the worksheet. To fulfill these requirements, Aspose.Cells provides a very simple set of classes and methods, discussed below.
Manage Rows and Columns
Aspose.Cells provides a class Workbook, that represents a Microsoft Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a GetCells() collection that represents all cells in the worksheet.
The GetCells() collection provides several methods for managing rows and columns in a worksheet. Some of these are discussed below.
Insert Rows and Columns
How to Insert a Row
Insert a row into the worksheet at any location by calling the InsertRow method of the GetCells() collection. The InsertRow method takes the index of the row where the new row will be inserted.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Instantiating a Workbook object
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Inserting a row into the worksheet at 3rd position
worksheet.GetCells().InsertRow(2);
// Path of output excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Saving the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Row inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
How to Insert Multiple Rows
To insert multiple rows into a worksheet, call the InsertRows method of the GetCells() collection. The InsertRows method takes two parameters:
- Row index, the index of the row from where the new rows will be inserted.
- Number of rows, the total number of rows that need to be inserted.
#include <iostream>
#include <fstream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Create workbook from file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Insert 10 rows into the worksheet starting from 3rd row
worksheet.GetCells().InsertRows(2, 10);
// Path of output excel file
U16String outputFilePath = srcDir + u"output.out.xls";
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Rows inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
How to Insert a Row with Formatting
To insert a row with formatting options, use the InsertRows overload that takes InsertOptions as a parameter. Set the CopyFormatType property of InsertOptions class with CopyFormatType Enumeration. The CopyFormatType Enumeration has three members as listed below.
- SameAsAbove: Formats the row same as the above row.
- SameAsBelow: Formats the row same as below row.
- Clear: Clears the formatting.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output Excel file
U16String outputFilePath = outDir + u"InsertingARowWithFormatting_out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Setting Formatting options
InsertOptions insertOptions;
insertOptions.SetCopyFormatType(CopyFormatType::SameAsAbove);
// Inserting a row into the worksheet at 3rd position
worksheet.GetCells().InsertRows(2, 1, insertOptions);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Row inserted successfully with formatting!" << std::endl;
Aspose::Cells::Cleanup();
}
How to Insert a Column
Developers can also insert a column into the worksheet at any location by calling the InsertColumn method of the GetCells() collection. The InsertColumn method takes the index of the column where the new column will be inserted.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output Excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Insert a column into the worksheet at 2nd position
worksheet.GetCells().InsertColumn(1);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Column inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Delete Rows and Columns
How to Delete Multiple Rows
To delete multiple rows from a worksheet, call the DeleteRows method of the GetCells() collection. The DeleteRows method takes two parameters:
- Row index, the index of the row from where the rows will be deleted.
- Number of rows, the total number of rows that need to be deleted.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Delete 10 rows from the worksheet starting from 3rd row
worksheet.GetCells().DeleteRows(2, 10);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xlsx";
workbook.Save(outputFilePath);
std::cout << "Rows deleted successfully and file saved!" << std::endl;
Aspose::Cells::Cleanup();
}
How to Delete a Column
To delete a column from the worksheet at any location, call the DeleteColumn method of the GetCells() collection. The DeleteColumn method takes the index of the column to delete.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook from file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Delete a column from the worksheet at 5th position (index 4)
worksheet.GetCells().DeleteColumn(4);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xlsx";
workbook.Save(outputFilePath);
std::cout << "Column deleted successfully and file saved!" << std::endl;
Aspose::Cells::Cleanup();
}