In Excel, the content of a cell may look incomplete when it exceeds the length of the cell. Under the circumstance, you can apply wrap text in the cell to ensure that all content can be viewed at once. This article will demonstrate how to programmatically wrap or unwrap text in Excel cells using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Wrap or Unwrap Text in Excel Cells
The detailed steps are as follows.
- Create a Workbook object.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Get a specified cell using Worksheet.Range[] property.
- Get the style of the specified cell using CellRange.Style property.
- Wrap text in the specified cell by setting the CellStyle.WrapText property to true. Or set the property to false to unwrap text in the specified cell.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; namespace WrapText { class Program { static void Main(string[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Load a sample Excel file workbook.LoadFromFile(@"D:\Files\sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Wrap text in cell B3 sheet.Range["B3"].Style.WrapText = true; //Unwrap text in cell B7 sheet.Range["B7"].Style.WrapText = false; //Save the result file workbook.SaveToFile("WarpText.xlsx", ExcelVersion.Version2010); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.