繁体   English   中英

如何在Excel中隐藏行?

[英]How can I hide rows in Excel?

我有以下代码可以隐藏某些行:

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;
}

该代码应在适当的时候到达,但没有效果:这些行仍然可见。 我希望它们出现在电子表格中,但默认情况下不可见。

我该怎么做?

更新

这也行不通:

hiddenRange.Rows.Hidden = true;

...事实上,这导致该应用程序崩溃:

hiddenRange.Hidden = true;

更新2

甚至这些试图实现目标的偷偷摸摸的方式也无济于事:

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

更新3

是的,您说得对,MacroMarc(在下面回复您的评论); 该测试代码可以正常工作:

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

...所以关于我的代码,我的大脑有些混乱。 也许这是一个时机问题; 我将用细齿梳再次检查它。

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;
    }

...或者,去除了多余的细节:

        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;
        }

更新4

现在正在工作; 我不得不将行范围从单个更改为多个:

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

...也许它以前的方式工作,我只是没有注意到,因为我编码的内容(仅在阈值以下的四分之一隐藏了一行)不是我想要的(隐藏阈值的四行全部) )。

现在的问题是,它们(某些行)是隐藏的,但是我看不到用户如何使这些行可见(这是传统的手工电子表格的工作方式)。

我在这里找到了如何做(手动取消隐藏)的方法。 我真的不需要自己做,我只需要知道用户可以做到。

如果您不介意使用其他库,则在使用Excel并免费使用时,ClosedXML很棒。

https://closedxml.codeplex.com/

这是一个如何在工作表中隐藏行的示例:

 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");

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

强烈建议您使用它,它为多个函数提供了许多有用且不错的命名约定。 希望对您有所帮助!

这是我首先以特定的方式使其工作的方式:

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;
}

...现在以更一般/抽象的方式:

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