Rotate text only in specific Excel row

浪尽此生 提交于 2019-12-11 00:00:58

问题


I'd like to rotate headers in an Excel file using Microsoft.Office.Interop. To achieve this, I'm using the following code:

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

The result looks like this:

As you can see, every cell gets rotated although I'm only specifying the first row. However, I just want the headers to be rotated:

I even tried it with a for loop for every column:

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

But I get the same result. What should I do to only change the orientation of the headers?

(Method GetExcelColumnName)


回答1:


Just convert the entire row 1.

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

fwiw, in VBA this might be best handled with application.intersect of rows(1) and the .usedrange. From your code it looks like that would be,

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;



回答2:


What worked for me was:

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

TextRotation for vertical text: 90 or 180 (upwards, downwards)



来源:https://stackoverflow.com/questions/44568593/rotate-text-only-in-specific-excel-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!