OpenXML页面设置(打印选项、页面边距、方向、页眉页脚)错误



我有一个c#程序,可以生成格式化的Excel报告。由于Office的升级,我将程序转换为使用Microsoft Excel 16对象库。除了pagesetup类之外,一切都很好。新DLL中不再支持Pagesetup类。由于报告的代码非常丰富,我决定在最后使用OpenXML修改Excel文件。

我通过将文件的后缀更改为zip来研究原始的OpenXML。下面是对应于pagesetup类的原始OpenXML。

<printOptions horizontalCentered="1"/>
<pageMargins left="0.5" right="0.5" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
<pageSetup orientation="landscape" r:id="rId1"/>
<headerFooter><oddHeader>&amp;L&amp;"Arial Narrow,Bold"&amp;8 9825 Fairmount Drive SE
&amp;R&amp;"Arial Narrow,Bold"&amp;8Ref. No.: 10-219</oddHeader>
<oddFooter>&amp;C&amp;"Arial Narrow,Regular"&amp;8Page 1 of 2</oddFooter></headerFooter>
<headerFooter>
<oddHeader>&amp;R&amp;8&amp;B&amp;"Arial Narrow"Ref. No.: 10-219
&amp;L&amp;8&amp;B&amp;"Arial Narrow"9825 Fairmount Drive SE</oddHeader>
<oddFooter>&amp;C&amp;8&amp;"Arial Narrow"Page 2 of 2</oddFooter></headerFooter>

然而,在我使用OpenXML函数格式化Excel文件后,Excel告诉我文件中存在OpenXML代码错误。以下是修订文件的摘录:

<x:pageMargins left="0.5" right="0.5" top="0.75" bottom="0.75" header="0.3" footer="0.3" />
<x:printOptions horizontalCentered="1" />
<x:pageSetup orientation="landscape" />
<x:headerFooter differentOddEven="0">
<x:oddHeader>&amp;L&amp;B&amp;"Arial Narrow"&amp;9825 Fairmount Drive SE&amp;R&amp;B&amp;"Arial Narrow"&amp;8Ref. No.: 10-219</x:oddHeader>
<x:oddFooter>&amp;C&amp;"Arial Narrow"&amp;8Page &amp;P of &amp;N</x:oddFooter></x:headerFooter>

它似乎在每个标签的开头都放了一个x:和/x:。下面是我的OpenXML函数。

public void formatLandscapeReport(String fileName, String jobNumber, String Address) {
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, true))
{
WorkbookPart workbookpart = document.WorkbookPart;
IEnumerable<String> worksheetIds = workbookpart.Workbook.Descendants<Sheet>().Select(w => w.Id.Value);
WorksheetPart worksheetpart = null;
foreach (String worksheetId in worksheetIds)
{
worksheetpart = ((WorksheetPart)workbookpart.GetPartById(worksheetId));
PrintOptions po = new PrintOptions();
po.HorizontalCentered = true;
worksheetpart.Worksheet.AppendChild(po);
PageMargins pm = worksheetpart.Worksheet.Descendants<PageMargins>().FirstOrDefault();
if (pm == null)
{
pm = new PageMargins();
worksheetpart.Worksheet.AppendChild(pm);
}
pm.Left = .5D;
pm.Right = .5D;
pm.Top = .75D;
pm.Bottom = .75D;
PageSetup pagesetup = worksheetpart.Worksheet.Descendants<PageSetup>().FirstOrDefault();
if (pagesetup == null)
{
pagesetup = new PageSetup();
worksheetpart.Worksheet.AppendChild(pagesetup);
}
pagesetup.Orientation = OrientationValues.Landscape;
// Header and Footer
HeaderFooter hf = new HeaderFooter();
hf.DifferentOddEven = false;
OddHeader ohdr = new OddHeader();
OddFooter oftr = new OddFooter();
ohdr.Text= "&L&B&"Arial Narrow"&" + Address +"&R&B&"Arial Narrow"&8Ref. No.: " + jobNumber;
oftr.Text = "&C&"Arial Narrow"&8Page &P of &N";
hf.Append(ohdr);
hf.Append(oftr);
worksheetpart.Worksheet.Append(hf);
worksheetpart.Worksheet.Save();
} // foreach
workbookpart.Workbook.Save();
} // using
} //formatLandscapeReport

如果有人能告诉我我做错了什么,我将不胜感激。

谢谢,

卡门

ECMA模式的相关部分如下:

<xsd:sequence>
...
<xsd:element name="printOptions" type="CT_PrintOptions" minOccurs="0" maxOccurs="1"/>
<xsd:element name="pageMargins" type="CT_PageMargins" minOccurs="0" maxOccurs="1"/>
<xsd:element name="pageSetup" type="CT_PageSetup" minOccurs="0" maxOccurs="1"/>
<xsd:element name="headerFooter" type="CT_HeaderFooter" minOccurs="0" maxOccurs="1"/>
....
<xsd:sequence>

请注意,此构造被定义为sequence,因此顺序很重要。

在原始文件中,顺序是正确的,printOptions出现在pageMargins之前,但在不正确的文件中,它们是错误的。

交换订单应该可以解决你的问题,这样问题就会变成"为什么订单是错误的?"。

问题很可能是您的Worksheet已经包含PageMargins项目,因此PrintOptions将添加到它们之后。

要解决此问题,您可以先执行PageMargins代码,然后使用InsertBefore方法在PageMargins:之前插入PrintOptions

PageMargins pm = worksheetpart.Worksheet.Descendants<PageMargins>().FirstOrDefault();
if (pm == null)
{
pm = new PageMargins();
worksheetpart.Worksheet.AppendChild(pm);
}
pm.Left = .5D;
pm.Right = .5D;
pm.Top = .75D;
pm.Bottom = .75D;
worksheetpart = ((WorksheetPart)workbookpart.GetPartById(worksheetId));
PrintOptions po = new PrintOptions();
po.HorizontalCentered = true;
//the PrintOptions must be before the PageMargins
worksheetpart.Worksheet.InsertAfter(po, pm);

x:部分只是一个名称空间。我不认为这会是你问题的原因。

相关内容

  • 没有找到相关文章

最新更新