Articles in this section
Category / Section

How to set row height when using WrapText?

3 mins read

 

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can perform row/column manipulations in C# and VB.NET. This article explains how to set row height when WrapText is applied.

When a long text is entered in Excel cells, applying WrapText to that cell will fit the row height in Microsoft Excel. However, in XlsIO the row height will not be updated if WrapText is applied programmatically. To fit the row to the content, it is recommended to invoke the AutoFitRows() method, explicitly. This will return the updated row height. Let’s see how to do this.

Steps to set row height when using WrapText, programmatically:

Step 1: Create a new C# console application project.

Create C# console application

Create a new C# console application

Step 2: Install Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install XlsIO NuGet package

Install XlsIO NuGet package

 

Step 3: Include the following namespaces in the Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

Step 4: Use the following code snippet to set row height when using WrapText in C# and VB.NET.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the application object
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Excel2016;
 
    //Create a new workbook
    IWorkbook workbook = application.Workbooks.Create(1);
 
    //Access the first worksheet
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //Set a sample text in cell "A1"
    worksheet["A1"].Text = "This cell contains sample text";
 
    //Set wrap text
    worksheet["A1:C8"].WrapText = true;
 
    //Set text in cells "A3" and "C1"
    worksheet["A3"].Text = "This cell contains sample text This cell contains sample text";
    worksheet["C1"].Text = "This cell contains sample text This cell contains sample text";
                
    //Set auto-fit rows to update the row heights
    worksheet["A1:C8"].AutofitRows();
 
    //Get the updated row height
    double rowHeight = worksheet["A1"].RowHeight;
 
    //Save the workbook
    workbook.SaveAs("Output.xlsx");
}
 

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the application object
    Dim application As IApplication = excelEngine.Excel
    application.DefaultVersion = ExcelVersion.Excel2016
 
    'Create a new workbook
    Dim workbook As IWorkbook = application.Workbooks.Create(1)
 
    'Access the first worksheet
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Set a sample text in cell "A1"
    worksheet("A1").Text = "This cell contains sample text"
 
    'Set wrap text
    worksheet("A1:C8").WrapText = True
 
    'Set text in cells "A3" and "C1"
    worksheet("A3").Text = "This cell contains sample text This cell contains sample text"
    worksheet("C1").Text = "This cell contains sample text This cell contains sample text"
 
    'Set auto-fit rows to update the row heights
    worksheet("A1:C8").AutofitRows()
 
    'Get the updated row height
    Dim rowHeight As Double = worksheet("A1").RowHeight
 
    'Save the workbook
    workbook.SaveAs("Output.xlsx")
End Using

 

A complete working example of how to update row height when using WrapText in C# and VB.NET can be downloaded from Autofit wrapped text.zip.

Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.

See Also:

How to convert Excel to PDF in C#, VB.NET

How to Export and Save Excel Chart as Image

How to set column width and row height?

Insert new row with previous row format in Excel using C#

Delete Excel rows and columns in C#, VB.NET

How to set Freeze panes in Excel using C#, VB.NET?

Sort Excel pivot table data by row and column fields in C# using XlsIO

Is it possible to AutoFit a row that contains a cell merged with cells in other rows?

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied