Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate text only in specific Excel row

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:

Rotated cells

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:

Rotated headers

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)

like image 976
diiN__________ Avatar asked Oct 19 '25 14:10

diiN__________


1 Answers

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;