Click here to Skip to main content
15,887,485 members
Articles / Productivity Apps and Services / Microsoft Office
Tip/Trick

How to Hide a Range of Rows in Excel with C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
12 Nov 2015CPOL 20.5K   2  
Hiding Excel Ranges (span of rows and columns) using Excel Interop (C#)

Hide Boring or Seldom-Used Data

In Excel, you can sweep data under the virtual carpet by making it hidden (never fear - the stalwart user can unhide them via the Home Tab, Cells section, Format > Hide & Unhide - after first selecting the previous and following rows).

Anyway, hiding a span of rows and columns you specify couldn't be a heck of a lot easier. Here's how you do it, in two easy steps:

  1. Name your worksheet:
    C#
    private Worksheet _xlSheet;
  2. Now, name a range, including the first row and column to hide and then the last row and column to hide, like so:
    C#
    var hiddenRange = yourWorksheet.Range[_xlSheet.Cells[42, 1], _xlSheet.Cells[999, 13]];
    hiddenRange.EntireRow.Hidden = true;

    This assumes the first row you want to hide is 42, etc. Obviously, you will want to change these hardcoded values.

As an example, here's some actual code, using constants and variables instead of hardcoded vals, which responds to a boolean whose value indicates whether the range should be hidden or not:

C#
private bool _hide;
private int _curTopRow;
private static readonly int ITEMDESC_COL = 1;
private static readonly int TOTALS_COL = 16;
. . .
if (_hide)
{
    var hiddenRange = _xlSheet.Range[_xlSheet.Cells[_curTopRow, ITEMDESC_COL], 
    				_xlSheet.Cells[_curTopRow+3, TOTALS_COL]];
    hiddenRange.EntireRow.Hidden = true;
}

Note that you need to reference the Microsoft.Office.Interop.Excel assembly.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
-- There are no messages in this forum --