Version 1.2 Update in Workbooks for .NET – Part II

In part two of this tutorial series, we continue our overview of the changes in Workbooks for .NET v1.2 by examining different examples for ColumnRange, RowRange, RowHeight and CellRange

Learn more about Xeed Workbooks for .NET

We continue our overview of the changes in Workbooks for .NET v1.2.

Setting the Width on ColumnRange

A Width property was added to the ColumnRange class to set the width of the columns covered by the ColumnRange. Using a ColumnRange allows us to define a range of consecutive Columns.

The ColumnRange class has the following properties:

  • StartingElement: the first WorksheetElement of the range
  • EndingElement: the last WorksheetElement of the range
  • Width: the Width of all Columns in the range *NEW*
  • Elements: the list of all elements in the range (read-only)
  • Count: the number of items in the target range (read-only)
  • Style: the Style object for a worksheet element (read-only)
// Defining a ColumnRange and setting the width of those columns
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
	var firstSheet = document.Worksheets[ 0 ];

	ColumnRange myRange = firstSheet.Columns[ "C", "G" ];
	myRange.Width = 50;

	// ...
}

Creating RowRanges and setting the Height on RowRange

The ability to define a range of consecutive rows can now be done with the RowRange class. A Height property is available to set the height of the rows covered by the RowRange.

The RowRange class has the following properties:

  • StartingElement: the first WorksheetElement of the range
  • EndingElement: the last WorksheetElement of the range
  • Height: the Height of all Rows in the range
  • Elements: the list of all elements in the range (read-only)
  • Count: the number of items in the target range (read-only)
  • Style: the Style object for a worksheet element (read-only)
// Defining a RowRange and setting the height of those rows
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
	var firstSheet = document.Worksheets[ 0 ];

	RowRange myRange = firstSheet.Rows[ 10, 25 ];
	myRange.Height = 50;

	// ...
}

Using AutoFit for the RowHeight

The Height can be adjusted automatically based on the highest cell in a row by using the AutoFit() method available on the Row class.

The AutoFit() method has the following parameters:

  • minimumHeight: the minimum desired Height when AutoFitting a Row, 0 point by default.
  • maximumHeight: the maximum desired Height when AutoFitting a Row, 409 points by default.
  • startColumnId: the id of the Column used as the starting point for the AutoFit's range, 0 or "A" by default, which is the first Column in a Row.
  • endColumnId: the id of the Column used as the ending point of the AutoFit's range, 16383 or "XFD" by default, which is the last possible Column in a Row.

The AutoFit() method has 2 overloads, both have the same parameter names, but one uses an integer for startColumnId and endColumnId to specify the column id by its index, and the other uses string values instead to specify the column id by its letter (for example, id 0 is the same as id “A”).

// Using AutoFit for the row height
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
	var firstSheet = document.Worksheets[ 0 ];

	// Using all default values
	firstSheet.Rows[ "D" ].AutoFit();

	// Using the column IDs as a number index
	firstSheet.Rows[ "E" ].AutoFit(30, 50, 4, 4);

	// Using the column IDs as a letter index this time
	firstSheet.Rows[ "F" ].AutoFit(30, 50, "E", "E");

	// save the modifications
	document.Save();
}

Modifying the Style on CellRange

Style property was added on the CellRange class to add the ability to specify a style on a range of cells.

// Setting a Style on a range of cells
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
	var firstSheet = document.Worksheets[ 0 ];

	// Specify the cell range
	CellRange myRange = firstSheet.Cells[ "A2, L2" ];

	// Specify the style
	myRange.Style.Alignment.Vertical = VerticalAlignment.Center;
	myRange.Style.Alignment.Horizontal = HorizontalAlignment.Center;
	myRange.Style.Font = new Font() { Bold = true, Underline = true, Size = 18d };

	// save the modifications
	document.Save();
}

Modifying the Style on ColumnRange

Style property was added on the ColumnRange class to add the ability to specify a style on a range of columns.

// Setting a Style on a range of columns
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
	var firstSheet = document.Worksheets[ 0 ];

	// Specify the column range
	ColumnRange myRange = firstSheet.Columns[ "C", "G" ];

	// Specify the style
	myRange.Style.Font.Size = 14d;
	myRange.Style.Font.Name = "Broadway";
	myRange.Style.Font.Color = System.Drawing.Color.Blue;
	myRange.Style.Alignment.Vertical = VerticalAlignment.Center;
	myRange.Style.Alignment.Horizontal = HorizontalAlignment.Center;

	// save the modifications
	document.Save();
}

Modifying the Style on RowRange

Style property was added on the RowRange class to add the ability to specify a style on a range of rows.

// Setting a Style on a range of rows
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
	var firstSheet = document.Worksheets[ 0 ];

	// Specify the row range
	RowRange myRange = firstSheet.Rows[ 10, 25 ];

	// Specify the style
	myRange.Style.Font.Size = 12d;
	myRange.Style.Font.Name = "Broadway";
	myRange.Style.Font.Color = System.Drawing.Color.Green;
	myRange.Style.Alignment.Vertical = VerticalAlignment.Center;
	myRange.Style.Alignment.Horizontal = HorizontalAlignment.Left;

	// save the modifications
	document.Save();
}

More to come in Part III, stay tuned!

For more information, please refer to the documentation.