仅在特定的Excel行中旋转文本 [英] Rotate text only in specific Excel row
问题描述
我想使用Microsoft.Office.Interop
旋转Excel文件中的标题.为此,我使用以下代码:
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;
结果如下:
如您所见,尽管我仅指定第一行,但每个单元都会旋转.但是,我只想旋转标题:
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:
我甚至对每列都使用for
循环进行了尝试:
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?
推荐答案
只需转换整行1.
worksheet.Range["1:1"].Style.Orientation = Excel.XlOrientation.xlUpwards;
worksheet.Rows["1"].Style.Orientation = Excel.XlOrientation.xlUpwards;
首先,在VBA中,最好使用row(1)和.usedrange的application.intersect处理.从您的代码来看,应该是这样,
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;
这篇关于仅在特定的Excel行中旋转文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!