Default column width in Excel Addin c#

Default column width in Excel Addin c#

Column Width:

Excel offers command to customize column width manually. In this article we will offer a customized input box to the user to input desired column width also, we will grab existing column width and populate in input box as default.

Prerequisites:

  • Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
  • Create Excel Addin in C# code style (Visual Studio Tools for Office)
  • Create a ribbon designer and put button

Code example:

private void btnDefaulWidth_Click(object sender, RibbonControlEventArgs e)
{
	//retain active row selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//retain current column width and populate
	double currentColumnWidth = oRange.Columns.ColumnWidth;
	//Show popup to grab input height from using
	var userValue = Globals.ThisAddIn.Application.InputBox("Enter column width:", "Adjust column width", currentColumnWidth);
	//declare variable to get through out
	double oValue;
	//check if user has given valid input
	if (double.TryParse(userValue.ToString(), out oValue))
	{
		//Set rowheight property by converting user value
		oRange.ColumnWidth = Convert.ToDouble(userValue);
	}
}

Output:

Please leave your valuable comments!!!

Next : Hide/Unhide rows in Excel C#

Leave a Reply

Your email address will not be published. Required fields are marked *