leora
leora

Reputation: 196489

Does ClosedXML support setting a worksheet's zoom level?

I am using closedXML to generate an Excel file from C# and I am trying to see if there is anyway to set the zoom level of a worksheet. I can't find this in any of the documentation or from googling?

Does anybody know how to do this?

Upvotes: 4

Views: 5480

Answers (4)

Raidri
Raidri

Reputation: 17550

Update for version 0.87+: https://stackoverflow.com/a/52755386/2610249


No, ClosedXML does not support setting the zoom. The option that johny links to is only for scaling of the pages when printing.

There is a feature request on the ClosedXML page, but no answer from the developer.

Upvotes: 2

Métoule
Métoule

Reputation: 14472

It's now possible, starting with ClosedXML version 0.87.0, via the IXLSheetView.ZoomScale property.

using Excel = ClosedXML.Excel;

var wb = new Excel.XLWorkbook();
Excel.IXLWorksheet ws = wb.AddWorksheet("zoom");
Excel.IXLSheetView view = ws.SheetView;

/* Value can be set between 10 and 400 */
view.ZoomScale = 85;

You can check the IXLSheetView source code for more information.

Upvotes: 7

Francois Botha
Francois Botha

Reputation: 4839

A pull request for this has been logged at https://github.com/ClosedXML/ClosedXML/pull/180

Upvotes: 0

Simon White
Simon White

Reputation: 146

As previously answered, you can't, but I've found a way around it which works well:

Create a template Excel file in advance, with all sheets' zoom levels set to how you want them.

When you create your workbook, instead of:

    public XLWorkbook CreateWorkbook()
    {
        XLWorkbook workbook = new XLWorkbook();
        IXLWorksheet worksheet = workbook.AddWorksheet("First sheet");

        // ...

        return workbook;
    }

do this:

    public XLWorkbook CreateWorkbookWithZoom()
    {
        XLWorkbook workbook = new XLWorkbook(@"C:\your template file.xlsx");
        IXLWorksheet worksheet = workbook.Worksheet(1);
        worksheet.Name = "First sheet";

        // ...

        return workbook;
    }

where C:\your template file.xlsx is the path of your template file.

I think you can also handle having a variable number of sheets, by copying existing (blank) worksheets instead of creating new ones. You can get creative with having different blank template worksheets to choose from, if you need to set the zoom level dynamically.

Upvotes: 0

Related Questions