【问题标题】:How can I hide rows in Excel?如何在 Excel 中隐藏行?
【发布时间】:2019-03-20 19:29:33
【问题描述】:

我有这个代码来隐藏某些行:

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

...也许它在之前按编码工作,我只是没有注意到它,因为我编码的内容(仅隐藏每四个低于阈值的一行)不是我的是有意的(隐藏阈值以下的所有四行)。

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

我发现了如何做到这一点(手动取消隐藏)here。我真的不需要自己做,我只需要知道用户可以做到。

【问题讨论】:

  • 原始代码应该可以工作。您是否仔细检查了范围内的变量等?如果您在某些行/列值中硬编码,它会起作用吗?
  • 好的,退出更新 3。
  • 所以我想你需要找出决定行号和列号的变量发生了什么:_curDescriptionTopRowITEMDESC_COL
  • 我现在知道了;你把我引向了正确的方向。它现在实际上隐藏了一排,但我不想只隐藏一排;我每次都想要四个(它们都是相关的)。

标签: c# excel office-interop excel-interop


【解决方案1】:

如果您不介意使用其他库,ClosedXML 非常适合与 Excel 一起使用并且可以免费使用。

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

强烈推荐它,它为多个函数提供了许多有用且漂亮的命名约定。希望对您有所帮助!

【讨论】:

  • 我会为我的下一个项目研究这个,但我几乎完成了当前的项目并且不想更改我现有的所有代码。不过,感谢您的提示!
【解决方案2】:

这就是我让它工作的方式,首先是一种特定的方式:

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

【讨论】:

  • 这是这样做的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2013-01-17
  • 1970-01-01
  • 2016-10-16
相关资源
最近更新 更多