MS Excel provides you with many features for working with Excel files. One of the basic operations that you can do is to insert or delete rows and columns. You might find yourself in situations where you need to insert or delete rows and columns while manipulating Excel files programmatically. In light of that, this article will teach you how to insert and delete rows and columns in Excel worksheets using C++.
- C++ API to Insert and Delete Rows and Columns in Excel
- Insert Rows in an Excel Worksheet using C++
- Insert Columns in an Excel Worksheet using C++
- Delete Rows from an Excel Worksheet using C++
- Delete Columns from an Excel Worksheet using C++
- Get a Free License
C++ API to Insert and Delete Rows and Columns in Excel
Aspose.Cells for C++ is a native C++ library that allows you to create, read and update Excel files without requiring Microsoft Excel to be installed. The API also supports inserting and deleting rows and columns in Excel worksheets. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Cells.Cpp
Insert Rows in an Excel Worksheet using C++
The following is the image of the sample file that we will manipulate in this article.
The following are the steps to insert rows in an Excel worksheet.
- Firstly, load the Excel file using the IWorkbook class.
- Get the instance of the worksheet where you want to insert the rows using IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Insert the rows using the IWorksheet->GetICells()->InsertRows ( Aspose::Cells::Systems::Int32 rowIndex, Aspose::Cells::Systems::Int32 totalRows) method.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to insert rows in an Excel worksheet using C++.
Insert Columns in an Excel Worksheet using C++
The following are the steps to insert columns in an Excel worksheet.
- Firstly, load the Excel file using the IWorkbook class.
- Get the instance of the worksheet where you want to insert the columns using IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Insert the columns using the IWorksheet->GetICells()->InsertColumns ( Aspose::Cells::Systems::Int32 columnIndex, Aspose::Cells::Systems::Int32 totalColumns) method.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to insert columns in an Excel worksheet using C++.
Delete Rows from an Excel Worksheet using C++
The following are the steps to delete rows from an Excel worksheet.
- Firstly, load the Excel file using the IWorkbook class.
- Get the instance of the worksheet from where you want to delete the rows using IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Delete the rows using the IWorksheet->GetICells()->DeleteRows ( Aspose::Cells::Systems::Int32 rowIndex, Aspose::Cells::Systems::Int32 totalRows, bool updateReference) method.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to delete rows from an Excel worksheet using C++.
Delete Columns from an Excel Worksheet using C++
The following are the steps to delete columns from an Excel worksheet.
- Firstly, load the Excel file using the IWorkbook class.
- Get the instance of the worksheet from where you want to delete the columns using IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Delete the columns using the IWorksheet->GetICells()->DeleteColumns ( Aspose::Cells::Systems::Int32 columnIndex, Aspose::Cells::Systems::Int32 totalColumns, bool updateReference) method.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to delete columns from an Excel worksheet using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to insert and delete rows and columns in an Excel worksheet using C++. Aspose.Cells for C++ is a vast API that provides a bunch of additional features for automating your Excel-related tasks. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.