仅在特定的Excel行中旋转文本

我想使用Microsoft.Office.Interop在Excel文件中旋转标题。 为了达到这个目的,我使用下面的代码:

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

结果如下所示:

旋转的细胞

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

旋转头

我甚至用每个列的for循环来尝试它:

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

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

( 方法GetExcelColumnName

只需转换整个行1。

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

在VBA中,这可能是最好的处理行(1)和.usedrange的application.intersect。 从你的代码看起来像这样,

 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; 
Interesting Posts