How to Convert Plain Content on an Excel Sheet to a Table






3.20/5 (2 votes)
A few lines of code showing how to define a range for an entire sheet and then convert that to a table
The Fancypantsification of the Plain Vanilla Sheet
If you have data on a sheet that is stark and blah like so:
...and you prefer to give it a little pizazz, like so:
...all you need is the following few lines of code; all you might need to change in this code is the name of the worksheet (from "_xlWhateverSheet
"), and the name of the source table (from "tablifiedSheet
").
private Worksheet _xlWhateverSheet;
. . .
string tableStyleToUse = "TableStyleMedium9";
string nameOfSourceTable = "tablifiedSheet";
Range entireSheet = _xlWhateverSheet.UsedRange;
_xlWhateverSheet.ListObjects.Add(XlListObjectSourceType.xlSrcRange, entireSheet,
Type.Missing, XlYesNoGuess.xlYes, Type.Missing).Name = nameOfSourceTable;
_xlWhateverSheet.ListObjects[nameOfSourceTable].TableStyle = tableStyleToUse;
Note: You can change the table style to use, too (tableStyleToUse
) if you prefer something different than what is shown above. "TableStyleMedium9
" is the style used by default when you manually create a table in Excel; it is visually pleasing (YMMV) and provides a filter for each column.