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.
Brian C. Hart, Ph.D., is a strategic engagement leader on a mission to leverage space technology to protect U.S. interests and assets against adversaries. Throughout Dr. Hart's career, he has enjoyed: Working closely with business executives to provide strategic direction and leadership, translating customer and competitive intelligence into compelling capture strategies and solutions, and mentoring teams to enhance individual and company capabilities while fostering an engaging and accountable environment, being involved in STEAM initiatives and education to develop greater awareness in the community, and serving the armed forces with the U.S. Navy and U.S. Army National Guard. He is excited to begin developing his career in Jacobs's Critical Mission Systems business unit, supporting NORAD and the U.S. Space Force.