简体   繁体   English

可以使用Apache POI XSSF设置活动范围吗?

[英]Is it possible to set the active range with Apache POI XSSF?

I am using Apache POI XSSF to read and write Excel Sheets. 我正在使用Apache POI XSSF来读写Excel工作表。

I know that I can set the active cell on a worksheet by using Sheet.setActiveCell(CellAddress address) . 我知道我可以使用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工作表中选择的范围

When I save a sheet with multiple cells selected using Excel those are selected upon opening the saved file. 当我保存具有使用Excel选择的多个单元格的工作表时,在打开保存的文件时会选择那些单元格。 Is there a way to do this with POI XSSF? 有没有办法用POI XSSF做到这一点?

you can use following line to achieve a ranke as active cell in excel: 您可以使用以下行在excel中将行列设置为活动单元格:

    sheet.setActiveCell("A1:B2");

Hope it helps. 希望能帮助到你。

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: 从3.16开始,不建议使用setActiveCell(String)方法,并且您不想使用不建议使用的方法,建议您创建自己的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();
    }
}

and use ist like: 并像这样使用ist:

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

Not the cleanest and best way, but works without warnings. 这不是最干净,最好的方法,但是可以在没有警告的情况下工作。

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

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