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");
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.
Dr. Brian Hart obtained his Ph.D. in Astrophysics from the University of California, Irvine in 2008. Under Professor David Buote, Dr. Heart researched the structure and evolution of the universe. Dr. Hart is currently employed as a Astrodynamicist / Space Data Scientist with Point Solutions Group in Colorado Springs, CO supporting Space Operations Command, United States Space Force. Dr. Hart is a Veteran of the U.S. Army and the U.S. Navy, having most recently served at Fort George G. Meade, MD as a Naval Officer with a Cyber Warfare Engineer designator. Dr. Hart has previously held positions at Jacobs Engineering supporting Cheyenne Mountain/Space Force supporting tests, with USSPACECOM/J58 supporting operators using predictive AI/ML with Rhombus Power, and with SAIC supporting the Horizon 2 program at STARCOM. Dr. Hart is well known to the community due to his over 150 technical publications and public speaking events. Originally from Minneapolis/Saint Paul, Minnesota, Dr. Hart lives in Colorado Springs with his Black Lab, Bruce, and likes bowling, winter sports, exploring, and swimming. Dr. Hart has a new movie coming out soon, which is a documentary called "Galaxy Clusters: Giants of the Universe," about his outer space research. The movie showcases the Chandra X-ray Observatory, one of NASA’s four great observatories and the world’s most powerful telescopes for detecting X-rays. The movie has been accepted for screening at the USAFA Planetarium and will highlight the need of updating and maintaining X-ray telescopes for scientific advancement.