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

查看:159
本文介绍了是否可以使用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中将行列作为活动单元格:

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

并像这样使用ist:

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天全站免登陆