75718

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

Question:

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

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:

<a href="https://i.stack.imgur.com/8MV6B.png" rel="nofollow"><img alt="Range selected in Excel Sheet" class="b-lazy" data-src="https://i.stack.imgur.com/8MV6B.png" data-original="https://i.stack.imgur.com/8MV6B.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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?

Answer1:

you can use following line to achieve a ranke as active cell in 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:

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:

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

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

Recommend

  • Has anyone used the “selection preferences” provided by MKS Yacc?
  • How to call a generic method with type constraints when the parameter doesn't have these constr
  • Programatically modify Eclipse workspace and CDT options from a plugin
  • How to check if a Gmail label has a nested sub-label?
  • Fixed Background Works in Chrome but Not Firefox?
  • write text on image and show it to a imageview
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • How to change placeholder text in an autocomplete activity of android google place?
  • Responsive Form on top of Responsive Image? - Bootstrap
  • EntLib Way to Bind “Null” Value to Parameter
  • How to open html table in xls on click of a button
  • C#: Import/Export Settings into/from a File
  • Django model inheritance, filtering models
  • How can I set a binding to a Combox in a UserControl?
  • Excel's Macro-Recorder usage
  • AJAX Html Editor Extender upload image appearing blank
  • Meteor: Do Something On Email Verification Confirmation
  • Cannot resolve symbol 'MyApi'
  • Repeat a vertical line on every page in Report Builder / SSRS
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Using $this when not in object context
  • Android screen density dpi vs ppi
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Bug in WPF DataGrid
  • Is possible to count alias result on mysql
  • Perl system calls when running as another user using sudo
  • How to handle AllServersUnavailable Exception
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • VBA Convert delimiter text file to Excel
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Data Validation Drop Down Box Arrow Disappearing
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • using HTMLImports.whenReady not working in chrome
  • coudnt use logback because of log4j
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Reading document lines to the user (python)
  • java string with new operator and a literal