Wrap Text in Excel using C#

Follow this article to Wrap text in Excel using C#. In this article, you will get the resources to set the development environment for writing the application, a list of programming steps, and a runnable sample code demonstrating how to wrap around text in Excel using C#. We will also discuss various options to wrap text in selected cells only.

Steps to Apply Wrap Text in Excel using C#

  1. Set the IDE to use Aspose.Cells for .NET to wrap text
  2. Create a Workbook, access a worksheet, and access a cell
  3. Put some text in this cell that does not require wrapping
  4. Access another cell and put sample text for wrapping
  5. Access the style of this cell and set the IsTextWrapped flag to true
  6. Set style back to the cell with the updated flag
  7. Call the AutoFitRows() method for the worksheet and save the output file

These steps summarize how to do word wrap in Excel using C#. It requires accessing the style property of the target cell and setting the IsTextWrapped value to true. Next, call the AutoFitRows() for the worksheet, which will cause the text wrapping in all the cells whose style has the IsTextWrapped property value ’true'.

Code to Auto Wrap Text in Excel using C#

This sample code demonstrates how to fit text in Excel using C#. You can call different overloaded methods for the AutoFitRows that allow the use of the AutoFitterOptions class object containing the AutoFitWrappedTextType. The default value of AutoFitWrappedTextType is ‘Default’ which wraps text similar to Excel however you may use option ‘Paragraph’ that wraps text according to the longest paragraph.

This article has taught us how to apply text wrapping in Excel using C#. To apply conditional formatting, refer to the article on how to apply conditional formatting in Excel in C#.

 English