仅旋转特定 Excel 行中的文本

dii*_*___ 4 c# excel rotation office-interop

我想使用 旋转 Excel 文件中的标题Microsoft.Office.Interop。为了实现这一目标,我使用以下代码:

worksheet.Range["A1:" + worksheet.UsedRange.Columns.Count + "1"].Style.Orientation
    = Excel.XlOrientation.xlUpwards;
Run Code Online (Sandbox Code Playgroud)

结果如下:

旋转细胞

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

旋转标题

我什至尝试for对每一列进行循环:

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

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

方法GetExcelColumnName

小智 5

只需转换整个第 1 行即可。

worksheet.Range["1:1"].Style.Orientation = Excel.XlOrientation.xlUpwards;
worksheet.Rows["1"].Style.Orientation = Excel.XlOrientation.xlUpwards;
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

  • 有一个 [Style.Orientation](https://msdn.microsoft.com/en-us/library/office/ff839229.aspx) 属性和一个 [Range.Orientation](https://msdn.microsoft.com/ en-us/library/office/ff198186.aspx) 财产;你需要后者。 (2认同)