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

发布于 2024-12-23 20:17:21 字数 176 浏览 5 评论 0原文

我想知道是否可以将Excel工作表的方向设置为从右到左... 我正在使用 JXL Api。

提前致谢

赏金是从右到左对齐,而不是右对齐

在此处输入图像描述

I wonder if i can set the direction of the excel sheet to right-to-left ...
I am using JXL Api.

thanks in advance

The bounty is about right to left and not right alignment

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纸伞微斜 2024-12-30 20:17:21

目前 JXL 不支持此选项,但如果您愿意编辑和重新构建 JXL,我有一个很好的解决方案。

从右到左的选项在名为 WINDOW2 记录的文件部分中保存和定义。您可以在此处中查看部分中定义的所有选项第 5.110 节窗口2。在 5.110.2 选项标志部分下,您可以看到选项标志的掩码值和从右到左的选项:

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

JXL create 此类中 excel 文件的这一部分 - Window2Record

在构造函数方法中,您可以看到有些值是可配置的,有些是硬编码的:

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

如您所见,选项“显示轮廓符号”被硬编码为 true,因为它的掩码(0x80)始终添加到选项标志中,并且 DisplayZeroValues 可以通过给定 SheetSettings 对象(具有 getter 和 setter...)的值进行配置。

如果您愿意重建项目,您可以通过添加以下行来硬编码从右到左的设置:

选项|= 0x40; // 从右到左的列

到此构造函数,或者如果您希望它可配置,请向 SheetSettings 添加一个新参数(及其 getter 和 setter),并在 Window2Record 中为其添加右侧的 if 子句。

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.

Right-to-left options are saved and defined in a section of the file called WINDOW2 record. You can see all the options defined in the part here in section 5.110 WINDOW2. Under section 5.110.2 Option Flags, you can see the mask value for the option flag and the right-to-left option:

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.

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...)

If you are willing to rebuild the project, you can either hard code your right-to-left settings by adding the line:

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.

雾里花 2024-12-30 20:17:21

这可以手动完成 (来自 Microsoft Office 支持)

Excel 选项对话框中的从右到左选项定向新方向
工作表从右到左。该设置不适用于
当前显示的工作表。您可以将工作表定向为正确
在同一工作簿中从左到右。

  1. 单击“Microsoft Office 按钮”,然后单击“Excel 选项”。 Excel 选项按钮在哪里?
  2. 点击“国际”。
  3. 在“从右到左”和“默认方向”下,单击“从右到左”,然后单击“确定”。
  4. 插入新工作表,或打开新工作簿。
  5. 在 Windows 任务栏(或语言栏,如果可见)中,
    单击语言图标 ,然后单击语言名称
    您要使用的从右到左的语言。
  6. 如果您需要更改文本方向,请单击
    “数据”选项卡上“字体对齐”组中的从右到左按钮。

JXL api 似乎不支持此功能。您可以查看 Apache Poi (javadoc) 或 TeamDev 的 JExcel (javadoc) 更适合这种需求。实现方式将类似于:

Poi 方式:

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

通过 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));
            }
        });

关于 poi:1, 23

顺便说一句,如果您尝试使用 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;

This can be done manually (from Microsoft Office Support)

The Right-to-left option in the Excel Options dialog box orients new
worksheets from right to left. 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. Where is the Excel Options button?
  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.
  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. You can have a look at Apache Poi (javadoc) or TeamDev's JExcel (javadoc) which is more appropriate for this kind of requirement. Implementation will be similar to these:

Poi way:

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

Via 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.

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:

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;
梦在深巷 2024-12-30 20:17:21

要在电子表格创建过程中设置从右到左的方向:

  • 手动创建具有从右到左文本方向的 xls 模板文件
  • 当您希望在 Jexcel 中创建新的电子表格时:

    • 将模板作为工作表打开
    • 调用工作表方法来创建一个新工作表作为工作表模板的副本。这是 Workbook 类中的方法:

      public static WritableWorkbook createWorkbook(java.io.File 文件,
                                                    作业本中)
                                      抛出 java.io.IOException
      

      使用给定文件名创建可写工作簿作为工作簿的副本
      传入。创建后,可写工作簿的内容可以修改

创建后更改方向:

  • 您可以手动创建一个名为 AUTO_OPEN 的 Excel 宏,该宏将在电子表格打开时运行:< /p>

    <预><代码> Application.DefaultSheetDirection = xlRTL
    ActiveSheet.DisplayRightToLeft = True

  • 或者您可以使用 JXL 进行所有处理,关闭文件,然后运行 ​​VBscript(与microsoft.office.interop.excel.dll):

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

    您可以通过Process<从java执行脚本/a>

To set right-to-left direction during Spreadsheet creation:

  • Manually create an xls template file with right-to-left text direction
  • When you wish to create a new spreadsheet in 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:

      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:

        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):

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

    You can execute a script from java via Process

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文