Rudolf Lamprecht Rudolf Lamprecht - 1 month ago 7
C# Question

How to set the rowHeight of a row without affecting the rowHeight of another row with C# and Excel.Interop

I am using a .xltx template file with a logo in the A1 cell and then an empty row with a background colour on A2 (which will act as my column headings row). When the excel document gets generated, I want to set the

rowHeight
of the logo row to a specific height and set the
A2
row to another specific height. However, both rows' height are set exactly the same (the height of the
A2
row).

excelWorksheet = excelApplication.ActiveSheet;
//Getting the image row range
var imageRowRange = ((Worksheet)excelWorksheet).Range["A1", "A1"];
var entireImageRowRange = imageRowRange.EntireRow;
//Setting the image rowHeight
imageRowRange.RowHeight = 48.75;
//Getting the second row which acts as the column heading row
var rowRange = ((Worksheet)excelWorksheet).get_Range("2:1");
var entireRowRange = rowRange.EntireRow;
rowRange.WrapText = true;
if (setCustomHeight())
rowRange.RowHeight = 0.75 * int.Parse(getCustomHeight());
else
entireRowRange.AutoFit();
rowRange.VerticalAlignment = XlVAlign.xlVAlignBottom;
rowRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;


How can I set the first row individually from the second row please?

Answer

you can change get_Range("2:1"); to get_Range("2:2");, so that you only target the second row.

Range 2:1 means all rows from 2 to 1, range 2:2 means all rows from 2 to 2. You can test this by typing the range in Excel's "Name" box in the top left.

excel selected range