仅在特定Excel行中旋转文本

时间:2017-06-15 13:17:16

标签: c# excel rotation office-interop

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

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

结果如下:

Rotated cells

正如您所看到的,虽然我只指定了第一行,但每个单元格都会被旋转。但是,我只想旋转标题:

Rotated headers

我甚至尝试了每列的for循环:

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

但我得到了同样的结果。我该怎么做只改变标题的方向?

Method GetExcelColumnName

2 个答案:

答案 0 :(得分:2)

只需转换整行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;

答案 1 :(得分:0)

对我有用的是:

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

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