Apache POI: get zoom level by using reflection

42 views Asked by At

I want to get the current zoom level of the target sheet. By checking the docs of Apache POI, there's no getter method for the zoom level. I thought of invoking the similar methods of setting the zoom level. However I run into this problem below

java.lang.IllegalArgumentException: wrong number of arguments

This is my code below:

private static long getZoomLevel(XSSFSheet sheet) throws Exception {
Method m = XSSFSheet.class.getDeclaredMethod("getDefaultSheetView", boolean.class);
m.setAccessible(true);
CTSheetView sheetView = (CTSheetView)m.invoke(sheet)
return sheetView.getZoomScale();
}

public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetExtensions:

private CTSheetView getDefaultSheetView(final boolean create) {
....
}

Any idea what causes the error?

1

There are 1 answers

1
lance-java On BEST ANSWER

You are invoking the method with zero args, you need to pass the boolean parameter to the method

Eg

m.invoke(sheet, true)