如何隐藏 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;
}
...也许它在之前按编码工作,我只是没有注意到它,因为我编码的内容(仅隐藏每四行中的一行低于阈值) 不是我想要的(将所有四行隐藏在阈值以下)。
现在的问题是它们(某些行)被隐藏了,但我没有看到用户如何使这些行可见(这是传统手工制作的电子表格的工作方式)。
我找到了如何做到这一点(手动取消隐藏)here。我真的不需要自己做,我只需要知道用户可以做到。
如果您不介意使用其他库,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");
强烈推荐它,它为多个函数提供了许多有用且漂亮的命名约定。希望对您有所帮助!
这就是我让它工作的方式,首先是一种特定的方式:
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;
}
...现在更 general/abstract 的方式:
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;
}
我有这个代码来隐藏某些行:
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。我真的不需要自己做,我只需要知道用户可以做到。
如果您不介意使用其他库,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");
强烈推荐它,它为多个函数提供了许多有用且漂亮的命名约定。希望对您有所帮助!
这就是我让它工作的方式,首先是一种特定的方式:
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;
}
...现在更 general/abstract 的方式:
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;
}