简体   繁体   English

使用C#和OpenXML将缩放属性设置为适合Excel

[英]Set scaling property to fit to in Excel using C# with OpenXML

I've made a asp.net page that creates an Excel workbook. 我创建了一个创建Excel工作簿的asp.net页面。 I have set the orientation scaling property of the page setup height and width of the workbook like so: 我已设置页面设​​置高度和工作簿宽度的方向缩放属性,如下所示:

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 you download the Excel document and open the PageSetup scaling has been set to 1 page wide and 3 pages tall which is what I want but the "Fit to" radio button isn't selected so it's not actually using these setting. 但是,当您下载Excel文档并打开PageSetup时,缩放设置为1页宽,3页高,这是我想要的,但未选择“适合”单选按钮,因此它实际上并未使用这些设置。

I figured there is some property I need to set to true along the way. 我想在途中我需要设置一些属性为true。 Like a FitToPage or something the thing is that I don't know which one. 就像FitToPage或其他东西一样,我不知道哪一个。 Anyone know? 谁知道?

NOTE: Please keep in mind that I am using DocumentFormat.OpenXml and not Microsoft.Office.Interop.Excel. 注意:请记住我使用的是DocumentFormat.OpenXml而不是Microsoft.Office.Interop.Excel。

The OpenXML SDK provides a class called PageSetupProperties which provides a property called FitToPage . OpenXML SDK提供了一个名为PageSetupProperties的类,它提供了一个名为FitToPage的属性。 Set this property to true in order to get the radio button selected: 将此属性设置为true以便选择单选按钮:

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);

must be set FitToPage, xml code see bellow: 必须设置FitToPage,xml代码见下:

sheet1.xml
...
<sheetPr>
    <pageSetUpPr fitToPage="1"/>
</sheetPr>
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM