简体   繁体   中英

How can I hide rows in Excel?

I've got this code to hide certain rows:

private static readonly Double DISPLAYED_HIDDEN_CUTOFF_VAL = 0.01;
private bool _hide;
. . .
_hide = racda.TotalItemPercentageOfItem < DISPLAYED_HIDDEN_CUTOFF_VAL;
if (_hide) 
{
    var hiddenRange = _xlSheet.Range[_xlSheet.Cells[_curDescriptionTopRow, ITEMDESC_COL], _xlSheet.Cells[_curDescriptionTopRow, TOTALS_COL]];
    hiddenRange.EntireRow.Hidden = true;
}

The code is reached when it should be, but it has no effect: those rows are still visible. I want them to be on the spreadsheet, but not visible by default.

How can I accomplish that?

UPDATE

This also did not work:

hiddenRange.Rows.Hidden = true;

...and this, in fact, caused the app to crash:

hiddenRange.Hidden = true;

UPDATE 2

Even these rather sneaky ways of trying to accomplish it did nothing:

//hiddenRange.RowHeight = 0; <= did nothing
hiddenRange.Rows.RowHeight = 0; // <= also does nothing

UPDATE 3

Yes, you're right, MacroMarc (replying to your comment below); this test code does work:

var testRange = _xlSheet.Range[_xlSheet.Cells[2, 1], _xlSheet.Cells[2, 4]];
testRange.EntireRow.Hidden = true;

...so there's something discombobulated in my brain with regards to my code. Maybe it's a timing issue; I'll go over it again, with a fine-toothed comb.

Dicho discombobulatory code is:

    foreach (RawAndCalcdDataAmalgamated racda in 
_rawAndCalcdDataAmalgamatedList)
    {
        _hide = racda.TotalItemPercentageOfItem < 
DISPLAYED_HIDDEN_CUTOFF_VAL;
        AddDescription(racda.ItemDescription);
        AddDataLabels();

        AddMonthData(racda.PackagesMonth1, racda.PurchasesMonth1,
racda.AvgPriceMonth1,
            racda.PercentOfTotalMonth1, MONTH1_COL);
        AddMonthData(racda.PackagesMonth2, racda.PurchasesMonth2, 
racda.AvgPriceMonth2,
            racda.PercentOfTotalMonth2, MONTH2_COL);
        AddMonthData(racda.PackagesMonth3, racda.PurchasesMonth3, 
racda.AvgPriceMonth3,
            racda.PercentOfTotalMonth3, MONTH3_COL);
        AddMonthData(racda.PackagesMonth4, racda.PurchasesMonth4, 
racda.AvgPriceMonth4,
            racda.PercentOfTotalMonth4, MONTH4_COL);
        AddMonthData(racda.PackagesMonth5, racda.PurchasesMonth5, 
racda.AvgPriceMonth5,
            racda.PercentOfTotalMonth5, MONTH5_COL);
        AddMonthData(racda.PackagesMonth6, racda.PurchasesMonth6, 
racda.AvgPriceMonth6,
            racda.PercentOfTotalMonth6, MONTH6_COL);
        AddMonthData(racda.PackagesMonth7, racda.PurchasesMonth7, 
racda.AvgPriceMonth7,
            racda.PercentOfTotalMonth7, MONTH7_COL);
        AddMonthData(racda.PackagesMonth8, racda.PurchasesMonth8, 
racda.AvgPriceMonth8,
            racda.PercentOfTotalMonth8, MONTH8_COL);
        AddMonthData(racda.PackagesMonth9, racda.PurchasesMonth9, 
racda.AvgPriceMonth9,
            racda.PercentOfTotalMonth9, MONTH9_COL);
        AddMonthData(racda.PackagesMonth10, racda.PurchasesMonth10, 
racda.AvgPriceMonth10,
            racda.PercentOfTotalMonth10, MONTH10_COL);
        AddMonthData(racda.PackagesMonth11, racda.PurchasesMonth11, 
racda.AvgPriceMonth11,
            racda.PercentOfTotalMonth11, MONTH11_COL);
        AddMonthData(racda.PackagesMonth12, racda.PurchasesMonth12, 
racda.AvgPriceMonth12,
            racda.PercentOfTotalMonth12, MONTH12_COL);
        AddMonthData(racda.PackagesMonth13, racda.PurchasesMonth13, 
racda.AvgPriceMonth13,
            racda.PercentOfTotalMonth13, MONTH13_COL);

        AddTotalsColData(racda.TotalItemPackages, 
racda.TotalItemPurchases, racda.TotalItemAvgPrice,
            racda.TotalItemPercentageOfItem);

        if (_hide)
        {
            var hiddenRange = 
_xlSheet.Range[_xlSheet.Cells[_curDescriptionTopRow, ITEMDESC_COL], 
_xlSheet.Cells[_curDescriptionTopRow, TOTALS_COL]];
            hiddenRange.EntireRow.Hidden = true;
        }
        _lastRowAdded = _curDescriptionTopRow + 3;
        AddBottomBorder(_lastRowAdded);
        _curDescriptionTopRow = _curDescriptionTopRow + 4;
    }

...or, stripped of extraneous details:

        foreach (RawAndCalcdDataAmalgamated racda in 
_rawAndCalcdDataAmalgamatedList)
        {
            _hide = racda.TotalItemPercentageOfItem < DISPLAYED_HIDDEN_CUTOFF_VAL;

            AddMonthData(racda.PackagesMonth1, racda.PurchasesMonth1, racda.AvgPriceMonth1,
                racda.PercentOfTotalMonth1, MONTH1_COL);
            . . .

            if (_hide)
            {
                var hiddenRange = _xlSheet.Range[_xlSheet.Cells[_curDescriptionTopRow, ITEMDESC_COL], _xlSheet.Cells[_curDescriptionTopRow, TOTALS_COL]];
                hiddenRange.EntireRow.Hidden = true;
            }
            _lastRowAdded = _curDescriptionTopRow + 3;
            _curDescriptionTopRow = _curDescriptionTopRow + 4;
        }

UPDATE 4

It's working now; I had to change the row range from a single one to multiple:

if (_hide)
{
    var hiddenRange = _xlSheet.Range[_xlSheet.Cells[_curDescriptionTopRow, ITEMDESC_COL], _xlSheet.Cells[_lastRowAdded, TOTALS_COL]];
    hiddenRange.EntireRow.Hidden = true;
}

...perhaps it was working as coded before, I just didn't notice it, because what I coded (hiding only one row of every four below the threshold value) was not what I was intending (hiding all four rows below the threshold).

Now the problem is that they (certain rows) are hidden, but I'm not seeing how the user can make those rows visible (which is how the legacy hand-crafted spreadsheet works).

I found out how to do that (manually unhide) here . I don't really need to do that myself, I just need to know the user can do it.

If you don't mind using other libraries, ClosedXML is fantastic when working with Excel and free to use.

https://closedxml.codeplex.com/

Here is an example of how to hide rows in a worksheet:

 var wb = new XLWorkbook();
 var ws = wb.Worksheets.Add("Hide Unhide");
 ws.Columns(1, 3).Hide();
 ws.Rows(1, 3).Hide();
 ws.Column(2).Unhide();
 ws.Row(2).Unhide();
 wb.SaveAs("HideUnhide.xlsx");

Source: https://closedxml.codeplex.com/wikipage?title=Hide%20Unhide%20Row%28s%29%2fColumn%28s%29&referringTitle=Documentation

Would strongly recommend it, it provides lots of useful and nice naming conventions for several functions. Hope that's of some help!

This is how I got it to work, first in a specific way:

private static readonly Double DISPLAYED_HIDDEN_CUTOFF_VAL = 0.01;
private bool _hide;
private Worksheet _xlSheet;
. . .
_hide = racda.TotalItemPercentageOfItem < DISPLAYED_HIDDEN_CUTOFF_VAL;
. . .
if (_hide)
{
    var hiddenRange = _xlSheet.Range[_xlSheet.Cells[_curDescriptionTopRow, ITEMDESC_COL], _xlSheet.Cells[_lastRowAdded, TOTALS_COL]];
    hiddenRange.EntireRow.Hidden = true;
}

...now in a more general/abstract way:

if ([some condition where you want rows to be hidden])
{
    var hiddenRange = yourWorksheet.Range[yourWorksheet.Cells[firstRowToHide, firstColToHide], yourWorksheet.Cells[lastRowToHide, lastColToHide]];
    hiddenRange.EntireRow.Hidden = true;
}

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