开发者

Excel Interop right to left document

开发者 https://www.devze.com 2023-02-02 03:12 出处:网络
I created a new Excel file with C#. When I open the document all the worksheets are align right to left.开发者_如何学运维

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?


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;


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;
} 


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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号