javaapache-poireflections

Apache POI: get zoom level by using reflection


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?


Solution

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

    Eg

    m.invoke(sheet, true)