简体   繁体   English

如何在Excel中隐藏行?

[英]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 更新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 更新3

Yes, you're right, MacroMarc (replying to your comment below); 是的,您说得对,MacroMarc(在下面回复您的评论); 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: Dicho组合代码为:

    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 更新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. 如果您不介意使用其他库,则在使用Excel并免费使用时,ClosedXML很棒。

https://closedxml.codeplex.com/ 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 来源: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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