NPOI GetZoom of Worksheet

525 Views Asked by At

I am using NPOI library to read in an existing workbook, and create a new one. When creating an HSSFSheet. I see that there is a method called SetZoom. What I don't see is a GetZoom method, or a Zoom property, in order to tell what that Zoom level is on the existing Worksheet. Any ideas?

2

There are 2 best solutions below

3
On

This isn't going to be a satisfying answer, because I'm not sure there is one. I looked through the NPOI source code (SetZoom() in line 1161) as well as the original java source code from which is was ported (SetZoom() in line 1083). As you can see, the SetZoom() method simply creates an SCLRecord:

SCLRecord sclRecord = new SCLRecord();
          sclRecord.Numerator = ((short)numerator);
          sclRecord.Denominator = ((short)denominator);
          Sheet.SetSCLRecord(sclRecord);

And digging further (in the original java):

public void setSCLRecord(SCLRecord sclRecord) {
    int oldRecordLoc = findFirstRecordLocBySid(SCLRecord.sid);
    if (oldRecordLoc == -1) {
        // Insert it after the window record
        int windowRecordLoc = findFirstRecordLocBySid(WindowTwoRecord.sid);
        _records.add(windowRecordLoc+1, sclRecord);
    } else {
        _records.set(oldRecordLoc, sclRecord);
    }
}

Where we can see that it saves this information into a private field of ISheet (InternalSheet in java):

private List<RecordBase> _records;

Thus, this information is not publicly accessible. If the Zoom is not set, it defaults to 100%, but I'm not aware of any way to access the Zoom of an existing sheet, as there doesn't seem to be any way to do this in NPOI short of modifying the internal source code yourself. Even so, this would only give you the zoom level that you set, as opposed to the existing one already on the worksheet.

It might be possible in EPPlus if you access the worksheet.View.ZoomScale, but I am currently unable to test this at the moment.

1
On

Thanks to C. Helling, I was able to deduce that the Zoom was stored in the Window2. It, however, is stored as integer (eg - 65 for 65%, 150 for 150%). Setting the zoom of the new worksheet based off the zoom of the old sheet, is pretty easy:

destWS.SetZoom(sourceWS.Sheet.WindowTwo.NormalZoom, 100)