仅在特定 Excel 行中旋转文本

Rotate text only in specific Excel row

我想使用 Microsoft.Office.Interop 在 Excel 文件中旋转 headers。为此,我使用了以下代码:

worksheet.Range["A1:" + worksheet.UsedRange.Columns.Count + "1"].Style.Orientation
    = Excel.XlOrientation.xlUpwards;

结果如下所示:

如您所见,每个 单元格都会旋转,尽管我只指定了第一行。但是,我只想旋转 headers:

我什至尝试对每一列使用 for 循环:

for (int counter = 1; counter <= worksheet.UsedRange.Columns.Count; counter++)
    worksheet.Range[GetExcelColumnName(counter) + "1"].Style.Orientation
        = Excel.XlOrientation.xlUpwards;

但我得到了相同的结果。我应该怎么做才能只改变 headers 的方向?

(Method GetExcelColumnName)

只需转换整行 1。

worksheet.Range["1:1"].Style.Orientation = Excel.XlOrientation.xlUpwards;
worksheet.Rows["1"].Style.Orientation = Excel.XlOrientation.xlUpwards;

fwiw,在 VBA 中,最好用 application.intersect of rows(1) 和 .usedrange 来处理。从您的代码看来,

Excel.Intersect(worksheet.Range["1:1"], worksheet.UsedRange).Style.Orientation = Excel.XlOrientation.xlUpwards;
/* just the cells, not the style */
Excel.Intersect(worksheet.Range["1:1"], worksheet.UsedRange).Cells.Orientation = Excel.XlOrientation.xlUpwards;

对我有用的是:

ws.SelectedRange[1, 1, 1, 15].Style.TextRotation = 180;

垂直文本的 TextRotation:90 或 180(向上、向下)