Cell Text Alignment Excel Addin C#

Cell Text Alignment Excel Addin C#

Alignment:

Refers to the placement of text within a cell. Followings are the basic alignments which can be applied over a cell content:

  • Left : puts text horizontally left in a cell
  • Right : puts text horizontally right in a cell
  • Center : puts text horizontally center in a cell
  • Distributed : puts text vertically distributed in a cell
  • Justify : puts text vertically justify in a cell
  • Top : puts text vertically top in a cell
  • Middle: puts text vertically center in a cell
  • Bottom : puts text vertically bottom in a cell

Let’s put some example to understand each alignment, I am taking following source to understand alignment:

Based on above input, we will put code to check each output

Left Align:

private void btnLeft_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Align text
	oRange.HorizontalAlignment = excel.XlHAlign.xlHAlignLeft;

}

Output:

Right Align:

private void btnRight_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Align text
	oRange.Cells.HorizontalAlignment = excel.XlHAlign.xlHAlignRight;
}

Output:

Center Align:

private void btnCenter_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Align text
	oRange.Cells.HorizontalAlignment = excel.XlHAlign.xlHAlignCenter;
}

Output:

Top Align:

private void btnTopAlign_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Align text
	oRange.Cells.VerticalAlignment = excel.XlVAlign.xlVAlignTop;
}

Output:

Middle Align:

private void btnMiddle_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Align text
	oRange.Cells.VerticalAlignment = excel.XlVAlign.xlVAlignCenter; //Middle
}

Output:

Bottom Align:

private void btnBottom_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Align text
	oRange.Cells.VerticalAlignment = excel.XlVAlign.xlVAlignBottom;
}

Output:

Distributed Align:

private void btnDistributed_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Align text
	oRange.Cells.VerticalAlignment = excel.XlVAlign.xlVAlignDistributed;
}

Output:

Justify Alignment:

private void btnJustify_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Align text
	oRange.Cells.VerticalAlignment = excel.XlVAlign.xlVAlignJustify;
}

Output:

Please leave your valuable comments!!!

Next: Cells text Orientation using VSTO C#

Leave a Reply

Your email address will not be published.