Delete Blank Rows and Columns in a Worksheet
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 object.
Use the following Aspose.Cells methods to delete empty rows and columns:
- To delete blank rows, use the Cells.delete_blank_rows() method. Please note, for blank rows that will be deleted, it is not only required that Row.is_blank 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.delete_blank_columns() method.
C# code to delete Blank Rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Open an existing excel file. | |
wb = Workbook(dataDir + "SampleInput.xlsx") | |
# Create a Worksheets object with reference to | |
# The sheets of the Workbook. | |
sheets = wb.worksheets | |
# Get first Worksheet from WorksheetCollection | |
sheet = sheets[0] | |
# Delete the Blank Rows from the worksheet | |
sheet.cells.delete_blank_rows() | |
# Save the excel file. | |
wb.save(dataDir + "mybook.out.xlsx") |
C# code to Delete Blank Columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Open an existing excel file. | |
wb = Workbook(dataDir + "SampleInput.xlsx") | |
# Create a Worksheets object with reference to | |
# The sheets of the Workbook. | |
sheets = wb.worksheets | |
# Get first Worksheet from WorksheetCollection | |
sheet = sheets[0] | |
# Delete the Blank Columns from the worksheet | |
sheet.cells.delete_blank_columns() | |
# Save the excel file. | |
wb.save(dataDir + "mybook.out.xlsx") |