如何使用 C# 和 Excel.Interop 设置一行的行高而不影响另一行的行高

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

我正在使用一个 .xltx 模板文件,在 A1 单元格中带有一个徽标,然后在 A2 中使用一个带有背景颜色的空行(它将作为我的列标题行)。生成 excel 文档后,我想将徽标行的 rowHeight 设置为特定高度,并将 A2 行设置为另一个特定高度。但是,两行的高度设置完全相同(A2 行的高度)。

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;

如何从第二行单独设置第一行?

您可以将 get_Range("2:1"); 更改为 get_Range("2:2");,这样您就可以只定位第二行。

范围 2:1 表示从 2 到 1 的所有行,范围 2:2 表示从 2 到 2 的所有行。您可以通过在 Excel 的 "Name" 框在左上角。