Set scaling property to match Excel in C # using OpenXML

I created an asp.net page that creates an Excel workbook. I set the orientation scaling property for page height and width for this book like this:

DocumentFormat.OpenXml.Spreadsheet.PageSetup pgOr = new DocumentFormat.OpenXml.Spreadsheet.PageSetup(); pgOr.Orientation = OrientationValues.Landscape; pgOr.FitToHeight = 3; pgOr.FitToWidth = 1; newWorksheetPart.Worksheet.AppendChild(pgOr); 

However, when loading the Excel document and opening the PageSetup scaling, the value is set to 1 page wide and 3 pages high, which I want, but the "Fit to" switch is not selected, so it does not actually use these settings.

I realized that there is some property that I need to set to true. Like FitToPage or something that I don’t know which one. Somebody knows?

NOTE. Keep in mind that I'm using DocumentFormat.OpenXml and not Microsoft.Office.Interop.Excel.

+4
source share
1 answer

The OpenKML SDK provides a class called PageSetupProperties that provides a property called FitToPage . Set this property to true to select the radio button:

 SheetProperties sp = new SheetProperties(new PageSetupProperties()); Worksheet ws = newWorksheetPart.Worksheet; ws.SheetProperties = sp; // Set the FitToPage property to true ws.SheetProperties.PageSetupProperties.FitToPage = BooleanValue.FromBoolean(true); DocumentFormat.OpenXml.Spreadsheet.PageSetup pgOr = new DocumentFormat.OpenXml.Spreadsheet.PageSetup(); pgOr.Orientation = DocumentFormat.OpenXml.Spreadsheet.OrientationValues.Landscape; pgOr.FitToHeight = 3; pgOr.FitToWidth = 1; ws.AppendChild(pgOr); 
+5
source

Source: https://habr.com/ru/post/1401672/


All Articles