打开XML SDK页面页边距 [英] Open XML SDK page margins

查看:145
本文介绍了打开XML SDK页面页边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在由打开的XML SDK生成的Excel电子表格上设置页边距.我没有打开一个已经存在的Excel文档,它是从头开始生成的.我正在使用PageMargins类,但不确定如何将此实例附加到工作表. SDK生产力工具提供了以下代码:

I am trying to set the page margins on an excel spreadsheet that is being generated by open xml sdk. I am not opening an excel document that already exists, it is generated from scratch. I am using the PageMargins class but am not sure how to attach this instance to the worksheet. The SDK productivity tool gives this code:

PageMargins pageMargins1 = worksheet.GetFirstChild<PageMargins>();
pageMargins1.Left = 0.45D;
pageMargins1.Right = 0.45D;
pageMargins1.Top = 0.5D;
pageMargins1.Bottom = 0.5D;

GetFirstChild()函数返回null.我也想做

The GetFirstChild() function returns null. I also tried to do

worksheet.Append(pageMargins1);

但没有运气.

也使用此示例中的代码:如何通过OpenXML SDK将Excel 2007文档的方向更改为横向 如果从头开始创建文档,则无法设置页面方向.您如何添加PageSetup& PageMargin实例到文档?

Also using the code from this example: How change excel 2007 document orientation to landscape by OpenXML sdk to set the page orientation does not work if creating the document from scratch. How do you add a PageSetup & PageMargin instance to the document?

任何人都知道此SDK,并且知道如何使用页边距或页面设置类吗?

Any one have knowledge of this SDK and knows how to use the margins or page setup class?

推荐答案

将事物附加到工作表的顺序很重要.必须将PageMargins附加在PageSetup之前,并且它们都必须位于SheetData之后的末尾.另外,需要设置所有设置.我使用了以下代码:

The order that things are appended to the worksheet is important. PageMargins needs to be appended before PageSetup, and they both need to be at the end after the SheetData. Also, all settings need to be set. I used this code:

PageMargins pageMargins1 = new PageMargins();
pageMargins1.Left = 0.45D;
pageMargins1.Right = 0.45D;
pageMargins1.Top = 0.5D;
pageMargins1.Bottom = 0.5D;
pageMargins1.Header = 0.3D;
pageMargins1.Footer = 0.3D;
worksheetPart.Worksheet.AppendChild(pageMargins1);

PageSetup pageSetup = new PageSetup();
pageSetup.Orientation = OrientationValues.Landscape;
pageSetup.FitToHeight = 2;
pageSetup.HorizontalDpi = 200;
pageSetup.VerticalDpi = 200;
worksheetPart.Worksheet.AppendChild(pageSetup);

与sdk生产率工具一起使用的一个巧妙技巧是将.xlsx文件重命名为.zip,然后提取内容.然后打开/xl/worksheets/sheet.xml并将标记与excel创建的excel文件的标记进行比较.

A neat trick to be used along with the sdk productivity tool is to rename the .xlsx file to .zip, then extract the contents. Then open /xl/worksheets/sheet.xml and compare the markup to the markup of an excel file created by excel.

这篇关于打开XML SDK页面页边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆