Excel Interop right to left document
Asked Answered
T

4

7

I created a new Excel file with C#.

When I open the document all the worksheets are align right to left.

How can I align the worksheet/workbook/window to display left to right grammatically?

Thumb answered 4/1, 2011 at 13:50 Comment(0)
S
11
Sub leftToRight()
    Application.DefaultSheetDirection = xlLTR
    ActiveSheet.DisplayRightToLeft = False
End Sub

You can also change the setting through Tools->Options->International. Note that you need to set/unset the Checkbox "View current sheet right-to-left" to change currently open sheets.

Edit: Sorry I accidentally interpreted your question as VBA.

Here is a c# Solution:

Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
xlApp.Workbooks.Add(System.Type.Missing);  
Excel.Worksheet active = (Excel.Worksheet)xlApp.ActiveSheet;

xlApp.DefaultSheetDirection = (int)Excel.Constants.xlLTR; //or xlRTL
active.DisplayRightToLeft = false;
Shaina answered 4/1, 2011 at 13:58 Comment(1)
the only remark i must add is that my class don't contain a 'Constants' property or field. so i used the built in enum of the interop dll calld xlDirectionThumb
T
7

I would like to introduce my implementation of this feature after i used marg concept and changed it to the right syntax for me:

public void SetWorksheetDirection(Application excel, bool isRTL)
{
    Worksheet active = (Worksheet)excel.ActiveSheet;

    if (isRTL)
        excel.DefaultSheetDirection = (int)XlDirection.xlToRight;
    else
        excel.DefaultSheetDirection = (int)XlDirection.xlToLeft;

    active.DisplayRightToLeft = isRTL;
} 
Thumb answered 9/1, 2011 at 15:43 Comment(0)
T
0

If you just wanted to organize some of the columns, you could use a line like this:

oWS.Range["A1:B2000"].HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
Traceetracer answered 28/12, 2013 at 21:29 Comment(0)
R
0

Do this one time to change the default direction:

  • Alt+F11 to open the VBA editor
  • Ctrl+G to open the Immediate window
  • in the Immediate window type Application.DefaultSheetDirection = xlLTR and press Enter
  • Alt+Q to close the VBA editor
  • create a new workbook to test it
Revulsive answered 22/11, 2016 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.