简体   繁体   中英

Unable to set the Hidden property of the Range class c# excel interop

I am trying to set Hide=false on an Excel Range object and I keep getting the "Unable to set the Hidden property of the Range class" Exception. My sheet is not protected and I have no merged cells in the range.

I am doing it like this:

    reportSheet.Unprotect("foo");
    int maxUsedRow = reportSheet.UsedRange.Rows.Count;
    int maxUsedColumn = reportSheet.UsedRange.Columns.Count;
    reportSheet.Cells.Range["A5", reportSheet.Cells[maxUsedRow, maxUsedColumn]].Hidden = false;

Interestingly: 1. Never anywhere in the code, am I explicitly setting the Hidden property to true, and yet that is what results after the programmatic manipulation. 2. I do not get this error if I do the following, but it also does not work.

    int rowNum = 5;
    reportSheet.Rows[rowNum].Hidden = false;

Any ideas?


I was trying to recreate this behavior in a new project and I realized something in the existing code must have been setting the hidden property. I debugged through it and found out it was the FormulaHidden property. Could this be overriding the Hidden property and preventing it from being reset programmatically because it superceeds Hidden?

I believe you want the EntireRow and EntireColumn Property. As @StephenRoss correctly indicated, you can't really hide a range, per se. You can hide/unhide rows and columns, but it's not like you can hide Cell B34. As such, I don't think Hidden functions on any entity that is not a row or column.

Here is an example of how you would unhide every row and column from A5 to the end of the data, per your code snippet:

int maxUsedRow = reportSheet.UsedRange.Rows.Count;
int maxUsedColumn = reportSheet.UsedRange.Columns.Count;

Excelx.Range r = reportSheet.Cells[maxUsedRow, maxUsedColumn];
reportSheet.Range["A5", r.Address].EntireColumn.Hidden = false;
reportSheet.Range["A5", r.Address].EntireRow.Hidden = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM