How to set row height when using WrapText?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It 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 set row height when using WrapText programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
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?
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to set row height when using WrapText in XlsIO.
You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!