News Category

C#VB.NET: Set Row Height and Column Width in Excel

2023-08-17 01:28:00 Written by  support iceblue
Rate this item
(0 votes)

When creating a spreadsheet, you can adjust the layout and appearance of it by setting the row height and column width. Microsoft Excel provides users with various methods to modify column width and row height, such as dragging the boundaries of columns or rows to the desired size, or entering specific values in the column width box or row height box. However, it is crucial for developers to understand how to achieve this functionality through programming. In this article, we will show you how to set row height and column width in Excel by 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 DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Set the Row Height in Excel

Spire.XLS for .NET supports users to set the row height programmatically by calling Worksheet.SetRowHeight() method. The following are detailed steps.

  • Create an object of Workbook class.
  • Load a sample file using Workbook.LoadFromFile() method.
  • Get the first sheet from this file by using Workbook.Worksheets[] property.
  • Set the height of the first row by calling Worksheet.SetRowHeight() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
namespace SetExcelRow
{
    class Program
    {

        static void Main(string[] args)
        {
            //Create an object of  Workbook class
            Workbook workbook = new Workbook();

            //Load a sample file from disk
            workbook.LoadFromFile(@"sample.xlsx");

            //Get the first worksheet from the sample file
            Worksheet sheet = workbook.Worksheets[0];

            //Set the row height of the first row
            sheet.SetRowHeight(1, 25);

            //Save the result file
            workbook.SaveToFile("SetRow.xlsx", ExcelVersion.Version2013);
            workbook.Dispose();
        }
    }
}

C#VB.NET: Set Row Height and Column Width in Excel

Set the Column Width in Excel

What's more, Spire.XLS for .NET also enable users to set the column width in Excel programmatically by calling Worksheet.SetColumnWidth() method. The following are detailed steps.

  • Create an object of Workbook class.
  • Load a sample file using Workbook.LoadFromFile() method.
  • Get the first sheet from this file by using Workbook.Worksheets[] property.
  • Set the width of the fourth column by calling Worksheet.SetColumnWidth() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
namespace SetExcelColumn
{
    class Program
    {

        static void Main(string[] args)
        {
            //Create an object of  Workbook class
            Workbook workbook = new Workbook();

            //Load a sample file from disk
            workbook.LoadFromFile(@"sample.xlsx");

            //Get the first worksheet from the sample file
            Worksheet sheet = workbook.Worksheets[0];

            //Set the column width of the fourth column
            sheet.SetColumnWidth(4, 15);

            //Save the result file
            workbook.SaveToFile("SetColumn.xlsx", ExcelVersion.Version2013);
            workbook.Dispose();
        }
    }
}

C#VB.NET: Set Row Height and Column Width in Excel

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.

Additional Info

  • tutorial_title:
Last modified on Thursday, 17 August 2023 02:01