Wrap/Unwrap Text in Excel Cells in C#, VB.NET

Wrap text and unwrap text in excel cells are indispensable for developers. Wrap text is specially required when the excel text exceeds the column width or excel data cannot be shown completely. While unwrap text enables the cell data to be displayed in orderly rows and columns. In this section, a solution will be introduced for you to quickly wrap or unwrap text in excel with C#, VB.NET via a .NET Excel component.

Spire.XLS for .NET, as a standalone excel component, can help both enterprises and individuals to operate a wide range of tasks in Excel document such as convert excel to other formats, encrypt/decrypt excel, generate different kinds of charts and tables etc. Using this Excel component, you can easily wrap and unwrap through two lines of core code.

Suppose you want to wrap text in A1, you can set up the A1 cell text and wrap properties by Worksheet. Range["A1"].Text and Worksheet.Range["A1"].Style.WrapText = true. Of course if you want to unwrap text in A1, you can set the wrap property to be false. Now, please download Spire.XLS for .NET and preview the effect of your text wrap and unwrap task as below picture.

Wrap and Unwrap Excel Data

using Spire.Xls;

namespace wrap
    class Program
        static void Main(string[] args)
            //Create a new workbook;
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            //Wrap the excel text;
            sheet.Range["C1"].Text = "e-iceblue is in facebook and welcome to like us";
            sheet.Range["C1"].Style.WrapText = true;
            sheet.Range["D1"].Text = "e-iceblue is in twitter and welcome to follow us";
            sheet.Range["D1"].Style.WrapText=  true;

            //Unwrap the excel text;
            sheet.Range["C2"].Text = "http://www.facebook.com/pages/e-iceblue/139657096082266";
            sheet.Range["C2"].Style.WrapText= false;
            sheet.Range["D2"].Text = "https://twitter.com/eiceblue";
            sheet.Range["D2"].Style.WrapText = false;

            //set the text color of Range["C1:D1"]
            sheet.Range["C1:D1"].Style.Font.Size = 15;
            sheet.Range["C1:D1"].Style.Font.Color = Color.Blue;
            //set the text color of Range["C2:D2"]
            sheet.Range["C2:D2"].Style.Font.Size = 15;
            sheet.Range["C2:D2"].Style.Font.Color = Color.DeepSkyBlue;

            // Save the project;
            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);

Imports Spire.Xls

Namespace wrap
    Class Program
        Private Shared Sub Main(ByVal args() As String)
            'Create a new workbook;
            Dim workbook As Workbook = New Workbook
            Dim sheet As Worksheet = workbook.Worksheets(0)
            'Wrap the excel text;
            sheet.Range("C1").Text = "e-iceblue is in facebook and welcome to like us"
            sheet.Range("C1").Style.WrapText = true
            sheet.Range("D1").Text = "e-iceblue is in twitter and welcome to follow us"
            sheet.Range("D1").Style.WrapText = true
            'Unwrap the excel text;
            sheet.Range("C2").Text = "http://www.facebook.com/pages/e-iceblue/139657096082266"
            sheet.Range("C2").Style.WrapText = false
            sheet.Range("D2").Text = "https://twitter.com/eiceblue"
            sheet.Range("D2").Style.WrapText = false
            'set the text color of Range["C1:D1"]
            sheet.Range("C1:D1").Style.Font.Size = 15
            sheet.Range("C1:D1").Style.Font.Color = Color.Blue
            'set the text color of Range["C2:D2"]
            sheet.Range("C2:D2").Style.Font.Size = 15
            sheet.Range("C2:D2").Style.Font.Color = Color.DeepSkyBlue
            ' Save the project;
            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
        End Sub
    End Class
End Namespace