Delete Blank Rows and Columns in a Worksheet with C++
Contents
[
Hide
]
It is possible to delete all blank rows and columns from a worksheet. This is useful when, for example, generating a PDF file from a Microsoft Excel file and want to convert only rows and columns that contain data or related objects.
Use the following Aspose.Cells methods to delete empty rows and columns:
- To delete blank rows, use the Cells.DeleteBlankRows() method. Please note, for blank rows that will be deleted, it is not only required that Row.IsBlank should be true, but also there should be no visible comment defined for any cell in those rows, and no pivot table whose range intersects with them.
- To delete blank columns, use the Cells.DeleteBlankColumns() method.
C++ code to delete Blank Rows
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Open an existing Excel file
U16String inputFilePath = srcDir + u"SampleInput.xlsx";
Workbook workbook(inputFilePath);
// Create a Worksheets object with reference to the sheets of the Workbook
WorksheetCollection sheets = workbook.GetWorksheets();
// Get the first Worksheet from WorksheetCollection
Worksheet sheet = sheets.Get(0);
// Delete the Blank Rows from the worksheet
sheet.GetCells().DeleteBlankRows();
// Save the Excel file
U16String outputFilePath = outDir + u"mybook.out.xlsx";
workbook.Save(outputFilePath);
std::cout << "Blank rows deleted and workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
C++ code to Delete Blank Columns
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"SampleInput.xlsx";
// Create a smart pointer to a new Workbook instance
std::unique_ptr<Workbook> wb = std::make_unique<Workbook>(inputFilePath);
// Create a Worksheets object with reference to the sheets of the Workbook
WorksheetCollection sheets = wb->GetWorksheets();
// Get the first Worksheet from WorksheetCollection
Worksheet sheet = sheets.Get(0);
// Delete the blank columns from the worksheet
sheet.GetCells().DeleteBlankColumns();
// Save the excel file
U16String outputFilePath = srcDir + u"mybook.out.xlsx";
wb->Save(outputFilePath);
std::cout << "Blank columns deleted successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}