是否可以使用 Apache POI XSSF 设置活动范围? [英] Is it possible to set the active range with Apache POI XSSF?

查看:36
本文介绍了是否可以使用 Apache POI XSSF 设置活动范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache POI XSSF 来读取和写入 Excel 表格.

I am using Apache POI XSSF to read and write Excel Sheets.

我知道我可以使用 Sheet.setActiveCell(CellAddress address) 在工作表上设置活动单元格.

I know that I can set the active cell on a worksheet by using Sheet.setActiveCell(CellAddress address).

但是,我想将其设置为包含多个单元格的范围,如下图所示:

However, I'd like to set it to a Range containing more than one cell on the sheet, as illustrated by the picture below:

当我保存使用 Excel 选择多个单元格的工作表时,这些是在打开保存的文件时选择的.有没有办法用 POI XSSF 做到这一点?

When I save a sheet with multiple cells selected using Excel those are selected upon opening the saved file. Is there a way to do this with POI XSSF?

推荐答案

您可以使用以下行在excel中将ranke作为活动单元格:

you can use following line to achieve a ranke as active cell in excel:

    sheet.setActiveCell("A1:B2");

希望有帮助.

从 3.16 开始,不推荐使用 setActiveCell(String) 方法,您不想使用不推荐使用的方法,我建议您创建自己的 CellAddress:

As from 3.16 onwards the setActiveCell(String) method is deprecated and you do not want to use a deprecated method I would suggest to create your own CellAddress:

public class CellRangeAddress extends CellAddress {

    private CellAddress start;
    private CellAddress end;

    public CellRangeAddress(final CellAddress start, final CellAddress end) {
        super(start);
        this.start = start;
        this.end = end;
    }


    @Override
    public String formatAsString() {
        if (end != null) {
            return start.formatAsString() + ":" + end.formatAsString();
        }
        return super.formatAsString();
    }
}

并使用 is like:

and use ist like:

sheet.setActiveCell(new CellRangeAddress(new CellAddress("A1"), new CellAddress("B2")));

不是最干净和最好的方法,但可以在没有警告的情况下工作.

Not the cleanest and best way, but works without warnings.

这篇关于是否可以使用 Apache POI XSSF 设置活动范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆