I have looked at the following thread on the same topic:
But our spreadsheets are much more complicated, and they cannot be formatted with the simple SetRowHeight() and SetColumnWidth(). Is there any other way to hide all unused rows/columns?
@sanshi
If blank cells have no settings at all and are considered unused, you can do it as follows.
const int MAX_EXCEL_ROWS = 1048576;
const int MAX_EXCEL_COLUMNS = 16384;
Workbook workbook = new Workbook("test.xlsx");
Worksheet sheet = workbook.Worksheets[0];
// Get the last used row and column indexes
int lastRow = sheet.Cells.MaxDataRow;
int lastColumn = sheet.Cells.MaxDataColumn;
Cells cells = workbook.Worksheets[0].Cells;
cells.IsDefaultRowHidden = true;
cells.StandardWidth = 0;
@ xinya.zhu Thanks for the reply. I’m already doing the loops to hide rows and columns. Just like to see if there are any other ways.
By setting cells.StandardWidth to 0, is it the same way as the aforementioned thread? Then I have to go through data cells to set width and height accordingly.
Kindly try using the suggested APIs to complete your task. If you encounter any issues, please provide more details along with a sample Excel file (zipped before attaching) and the expected output Excel file for reference. Additionally, sharing your current code would be helpful. This information will allow us to better understand your situation and potentially offer an improved code snippet.
Please try the following codes to reduce row and column object:
int lastRow = cells.MaxDataRow;
int lastColumn = cells.MaxDataColumn;
for (int i = 0; i <= lastRow; i++)
{
Row row = cells.Rows[i];
}
cells.IsDefaultRowHidden = true;
for (IEnumerator ie = cells.Rows.GetEnumerator(true, false);ie.MoveNext();)
{
Row row = ie.Current as Row;
if (row.Index <= lastRow)
break;
row.IsHidden = true;
}
for (int i = 0; i <= lastColumn; i++)
{
Column column = cells.Columns[i];
}
// cells.IsDefaultColumnHidden = true;// we will try to support this property in the next version
cells.StandardWidth = 0;
for (IEnumerator ie = cells.Columns.GetEnumerator(); ie.MoveNext();)
{
Column column = ie.Current as Column;
if (column.Index <= lastColumn)
continue;
column.IsHidden = true;
}
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.