我有这段代码隐藏某些行:
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;
即使这些相当偷偷摸摸的尝试完成它的方法也没有做任何事情:
//hiddenRange.RowHeight = 0; <= did nothing
hiddenRange.Rows.RowHeight = 0; // <= also does nothing
是的,你是对的,MacroMarc(回复你下面的评论);这个测试代码确实有效:
var testRange = _xlSheet.Range[_xlSheet.Cells[2, 1], _xlSheet.Cells[2, 4]];
testRange.EntireRow.Hidden = true;
...所以我的大脑中有一些与我的代码有关的东西。也许这是一个时间问题;我会用一把细齿梳子再次检查它。
Dicho discombobulatory代码是: 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;
}
现在正在运作;我不得不将行范围从单个行更改为多个:
if (_hide)
{
var hiddenRange = _xlSheet.Range[_xlSheet.Cells[_curDescriptionTopRow, ITEMDESC_COL], _xlSheet.Cells[_lastRowAdded, TOTALS_COL]];
hiddenRange.EntireRow.Hidden = true;
}
...也许它之前正在使用作为编码,我只是没注意到它,因为我编码的内容(只隐藏了每四个低于阈值的一行)不是我的意思打算(将所有四行隐藏在阈值以下)。
现在的问题是它们(某些行)是隐藏的,但我没有看到用户如何使这些行可见(这是传统手工制作的电子表格的工作原理)。
我发现了如何做到这一点(手动取消隐藏)here。我自己并不需要这样做,我只需要知道用户可以做到这一点。
答案 0 :(得分:1)
如果您不介意使用其他库,那么在使用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");
强烈推荐它,它为多个函数提供了许多有用且好的命名约定。希望有所帮助!
答案 1 :(得分:1)
这是我开始工作的方式,首先是以特定的方式:
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;
}