Click here to Skip to main content
Click here to Skip to main content

Tip: Format an Excel Range as a Table Programatically

By , 26 Aug 2011
 
Sometimes, you have a range of cells and you want to choose the Format As Table button to format the cells as a nice table.
 
Here's how to do that using Excel interop:
 
public void FormatAsTable(Excel.Range SourceRange, string TableName, string TableStyleName)
{
    SourceRange.Worksheet.ListObjects.Add(XlListObjectSourceType.xlSrcRange,
    SourceRange, System.Type.Missing, XlYesNoGuess.xlYes, System.Type.Missing).Name =
        TableName;
    SourceRange.Select();
    SourceRange.Worksheet.ListObjects[TableName].TableStyle = TableStyleName;
}
 
That's it! To apply a table style, e.g. TableStyleMedium15, to a range of cells, you say:
 
Excel.Range SourceRange = (Excel.Range)oSheet.get_Range("A6","X10"); // or whatever range you want here
FormatAsTable(SourceRange, "Table1", "TableStyleMedium15");
 
The "Table1" is just a random name for the table; it's arbitrary, but every table you format must be a unique range. The table style names you can find out by recording a macro, applying the formatting by hand, and then reading off of the VBA module what style name Excel filled in once you've stopped the recording.

License

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

About the Author

Brian C Hart
Software Developer (Senior) Corrugated Technologies, Inc.
United States United States
Member
From Fridley, Minnesota and I like computer programming! When I got started, I was working mostly with Windows GUI programming in C/C++. Then later on I worked with COM/DCOM for a school internship. I used COM/DCOM to write an ad hoc cluster server and job-running environment for a cluster of 24 Windows-based high-end visualization workstations. I moved on to C# and have been working in C# and Windows Forms ever since. I have yet to embrace Silverlight Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGood TipmemberIce_LS002 Aug '12 - 6:43 
GeneralRe: Good TipmemberBrian C Hart2 Aug '12 - 7:00 
GeneralReason for my vote of 5 My Vote 5memberAbdul Quader Mamun17 Dec '10 - 5:11 
GeneralGood tipsmemberAbdul Quader Mamun16 Dec '10 - 8:36 
GeneralExcellent tip, firm 5!memberDrABELL17 Dec '10 - 16:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 26 Aug 2011
Article Copyright 2010 by Brian C Hart
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid