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.

C#/VB.NET: Convert Excel to XPS

2022-08-26 08:43:00 Written by Koohji

XPS (XML Paper Specification) is a specification for a page description language and a fixed-document format developed by Microsoft. It defines the layout of a document and the visual appearance of each page. Sometimes you may need to convert an Excel document to XPS for distribution, archiving or printing purposes, and this article will demonstrate how to accomplish this task programmatically 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

Convert Excel to XPS

Spire.XLS for .NET allows you to convert Excel (.xls/ .xlsx) to XPS with only three lines of code. The detailed steps are as follows.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.LoadFromFile() method.
  • Convert the Excel document to XPS using Workbook.SaveToFile(String, FileFormat) method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ExceltoXPS
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a Workbook object.
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile(@"E:\Files\\sample0.xlsx", ExcelVersion.Version2010);

            //Convert the document to XPS
            workbook.SaveToFile("result.xps", FileFormat.XPS);
        }
    }
}

C#/VB.NET: Convert Excel to XPS

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.

Sometimes, we create a spreadsheet that contains a wonderful chart, we may still want to adjust the size and position of the chart in order to make the chart mostly matches the Excel page. In this article, I'll introduce you how to resize a chart to a suitable scale and how to move a chart to a desired position in C#, VB.NET via Spire.XLS.

Within the class of Spire.Xls.Chart, we can set the parameters of XlsShape.LeftColum and XlsShape.TopRow to move a chart to any location on a worksheet, while the size of the chart can be changed by setting the parameters of XlsShape.Width and XlsShape.Height. More details would be as follows:

Test File:

How to Resize and Move Excel Charts in C#, VB.NET

Code Snippet for Resize and Move Chart

Step 1: Create a new instance of workbook and load the test file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx", ExcelVersion.Version2010);

Step 2: Get the chart from the first worksheet.

Worksheet sheet = workbook.Worksheets[0];
Chart chart = sheet.Charts[0];

Step 3: Set position of the chart.

chart.LeftColumn = 1;
chart.TopRow = 7;

Step 4: Resize the chart.

chart.Width = 400;
chart.Height = 250;

Step 5: Save the changes to a new file.

workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);

Result:

How to Resize and Move Excel Charts in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
namespace ResizeandMoveExcel
{
    class Program
    {

        static void Main(string[] args)
        {

            Workbook workbook = new Workbook();
            workbook.LoadFromFile("sample.xlsx", ExcelVersion.Version2010);

            Worksheet sheet = workbook.Worksheets[0];
            Chart chart = sheet.Charts[0];

            chart.LeftColumn = 1;
            chart.TopRow = 7;

            chart.Width = 400;
            chart.Height = 250;

            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace ResizeandMoveExcel
	Class Program

		Private Shared Sub Main(args As String())

			Dim workbook As New Workbook()
			workbook.LoadFromFile("sample.xlsx", ExcelVersion.Version2010)

			Dim sheet As Worksheet = workbook.Worksheets(0)
			Dim chart As Chart = sheet.Charts(0)

			chart.LeftColumn = 1
			chart.TopRow = 7

			chart.Width = 400
			chart.Height = 250

			workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
		End Sub
	End Class
End Namespace
page 257