简体   繁体   English

如何使用JXL创建具有从右到左对齐的Excel工作表

[英]How to create excel sheet with right-to-left alignment using JXL

I wonder if i can set the direction of the excel sheet to right-to-left ... I am using JXL Api. 我想知道我是否可以将excel表的方向设置为从右到左......我正在使用JXL Api。

thanks in advance 提前致谢

The bounty is about right to left and not right alignment 赏金是从右到左,而不是右对齐

在此输入图像描述

Currently JXL does not support this option, but I have a good solution that will work if you are willing to edit and re-build JXL. 目前JXL不支持此选项,但如果您愿意编辑和重新构建JXL,我有一个很好的解决方案。

Right-to-left options are saved and defined in a section of the file called WINDOW2 record. 从右到左选项被保存并在名为WINDOW2 record的文件的一部分中定义。 You can see all the options defined in the part here in section 5.110 WINDOW2. 你可以看到在零件中定义的所有选项这里在部分窗口2 5.110。 Under section 5.110.2 Option Flags, you can see the mask value for the option flag and the right-to-left option: 在5.110.2选项标志下,您可以看到选项标志和从右到左选项的掩码值:

6 | 0040H | 0 = Columns from left to right | 1 = Columns from right to left

JXL create This part of the excel file in this class - Window2Record . JXL create这个类中的excel文件的一部分--Window2Record

In the constructor method you can see that some of the values are configurable and some are hard-coded: 在构造函数方法中,您可以看到一些值是可配置的,一些是硬编码的:

  public Window2Record(SheetSettings settings)
  {
    super(Type.WINDOW2);

    int options = 0;

    options |= 0x0; // display formula values, not formulas

    if (settings.getShowGridLines())
    {
      options |= 0x02;
    }

    options |= 0x04; // display row and column headings

    options |= 0x0; // panes should be not frozen

    if (settings.getDisplayZeroValues())
    {
      options |= 0x10;
    }

    options |= 0x20; // default header

    options |= 0x80; // display outline symbols

    // Handle the freeze panes
    if (settings.getHorizontalFreeze() != 0 ||
        settings.getVerticalFreeze() != 0)
    {
      options |= 0x08;
      options |= 0x100;
    }
...

As you can see options "display outline symbols" is hard coded to be true, since it's mask (0x80) always added to the options flag, and DisplayZeroValues is configurable by the values of the given SheetSettings object (that has getter and setters...) 如您所见,选项“显示轮廓符号”被硬编码为真,因为它的掩码(0x80)总是添加到选项标志,而DisplayZeroValues可以通过给定的SheetSettings对象(具有getter和setter)的值进行配置。 。)

If you are willing to rebuild the project, you can either hard code your right-to-left settings by adding the line: 如果您愿意重建项目,可以通过添加以下行来硬编码从右到左的设置:

options |= 0x40; options | = 0x40; // Columns from right to left //从右到左的列

to this constructor, or if you want it configurable, add a new parameter to the SheetSettings (and a getter and a setter for it) and in the Window2Record add the right if clause for it. 对于此构造函数,或者如果您希望它可配置,请向SheetSettings(以及它的getter和setter)添加一个新参数,并在Window2Record中为它添加正确的if子句。

This can be done manually (from Microsoft Office Support) 这可以手动完成(从Microsoft Office支持)

The Right-to-left option in the Excel Options dialog box orients new worksheets from right to left. “Excel选项”对话框中的“从右到左”选项从右到左定向新工作表。 The setting does not apply to the worksheet currently displayed. 该设置不适用于当前显示的工作表。 You can have worksheets oriented right to left and left to right in the same workbook. 您可以在同一工作簿中将工作表从右到左,从左到右。

  1. Click the Microsoft Office Button, and then click Excel Options. 单击“Microsoft Office按钮”,然后单击“Excel选项”。 Where is the Excel Options button? Excel选项按钮在哪里?
  2. Click International. 单击国际。
  3. Under Right-to-left and Default direction, click Right-to-left, and then click OK. 在“从右到左”和“默认”方向下,单击“从右到左”,然后单击“确定”。
  4. Insert a new worksheet, or open a new workbook. 插入新工作表,或打开新工作簿。
  5. In the Windows taskbar (or on the Language bar, if it is visible), click the Language icon , and then click the name of the right-to-left language that you want to use. 在Windows任务栏中(或在语言栏上,如果可见),单击“语言”图标,然后单击要使用的从右向左语言的名称。
  6. If you need to change the direction of the text, click the Right-to-left button on the Data tab in the Font Alignment group. 如果需要更改文本的方向,请单击“字体对齐”组中“数据”选项卡上的“从右到左”按钮。

It seems JXL api doesn't support this feature. 似乎JXL api不支持此功能。 You can have a look at Apache Poi ( javadoc ) or TeamDev's JExcel ( javadoc ) which is more appropriate for this kind of requirement. 您可以查看Apache Poijavadoc )或TeamDev的JExceljavadoc ),它更适合此类要求。 Implementation will be similar to these: 实施将类似于:

Poi way: Poi方式:

XSSFSheet sheet = workbook.createSheet();
sheet.getCTWorksheet().getSheetViews().getSheetViewArray(0).setRightToLeft(true);

Via Jexcel: 通过Jexcel:

 final Application application = ...;
        application.getOleMessageLoop().doInvokeAndWait(new Runnable() {
            public void run() {
                _Application app = application.getPeer();
                app.setDefaultSheetDirection(new Int32(LocaleID.LOCALE_USER_DEFAULT), new Int32(Constants.xlRTL));
            }
        });

About poi: 1 , 2 , 3 . 关于POI: 123

Btw, if you try the use setExcelRegionalSettings(IL) or setExcelRegionalSettings(IL-JM) is not going to work because these are the only supported countries by JXL: 顺便说一下,如果你尝试使用setExcelRegionalSettings(IL)setExcelRegionalSettings(IL-JM)是行不通的,因为这些是JXL唯一支持的国家:

public static final jxl.biff.CountryCode USA;
public static final jxl.biff.CountryCode CANADA;
public static final jxl.biff.CountryCode GREECE;
public static final jxl.biff.CountryCode NETHERLANDS;
public static final jxl.biff.CountryCode BELGIUM;
public static final jxl.biff.CountryCode FRANCE;
public static final jxl.biff.CountryCode SPAIN;
public static final jxl.biff.CountryCode ITALY;
public static final jxl.biff.CountryCode SWITZERLAND;
public static final jxl.biff.CountryCode UK;
public static final jxl.biff.CountryCode DENMARK;
public static final jxl.biff.CountryCode SWEDEN;
public static final jxl.biff.CountryCode NORWAY;
public static final jxl.biff.CountryCode GERMANY;
public static final jxl.biff.CountryCode PHILIPPINES;
public static final jxl.biff.CountryCode CHINA;
public static final jxl.biff.CountryCode INDIA;
public static final jxl.biff.CountryCode UNKNOWN;

To set right-to-left direction during Spreadsheet creation: 在电子表格创建期间设置从右到左的方向:

  • Manually create an xls template file with right-to-left text direction 手动创建具有从右到左文本方向的xls模板文件
  • When you wish to create a new spreadsheet in Jexcel: 当您希望在Jexcel中创建新的电子表格时:

    • open the template as a worksheet 将模板作为工作表打开
    • invoke the worksheet method to create a new worksheet as a copy of the worksheet template. 调用工作表方法以创建新工作表作为工作表模板的副本。 Here's the method in Workbook class: 这是Workbook类中的方法:

       public static WritableWorkbook createWorkbook(java.io.File file, Workbook in) throws java.io.IOException 

      Creates a writable workbook with the given filename as a copy of the workbook passed in. Once created, the contents of the writable workbook may be modified 创建一个具有给定文件名的可写工作簿作为传入的工作簿的副本。一旦创建,可写工作簿的内容可能会被修改

To change direction after creation: 要在创建后改变方向:

  • you can manually create an excel macro called AUTO_OPEN that will run whenever the spreadsheet is open: 你可以手动创建一个名为AUTO_OPEN的excel宏,它将在电子表格打开时运行:

      Application.DefaultSheetDirection = xlRTL ActiveSheet.DisplayRightToLeft = True 
  • or you can do all your processing with JXL, close the file and then run a VBscript (interfacing with microsoft.office.interop.excel.dll): 或者您可以使用JXL完成所有处理,关闭文件然后运行VBscript(与microsoft.office.interop.excel.dll连接):

      Set xl = CreateObject("Excel.application") xl.Application.Workbooks.Open "yourworkbookpath\\yourworkbook.xls" xl.DefaultSheetDirection = xlRTL 

    You can execute a script from java via Process 您可以通过Process从java执行脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM