OpenXML设置pageSetup后导致Excel文件损坏

3
我有一个使用DevExpress从我的程序创建的Excel文件。我需要在此文件中添加水平分页,但由于我的DevExpress版本不支持它,所以我使用OpenXML在单独的类中检索生成的Excel文件,以便添加水平分页。
在DevExpress生成后,我的文件看起来像这样: enter image description here 因此,它有6个页面。我想要这个而不是: enter image description here 为了打印每个标签在单独的工作表上。
因此,我使用OpenXML的PageSetup定义了我的Excel文件的宽度和高度:
private void InsertPageBreaks()
{
    //uint columnIndex = 17U;
    uint rowIndex = 42;

    SpreadsheetDocument sd = SpreadsheetDocument.Open("c:\\temp\\ExcelExport1.xlsx", true);
    try
    {

        WorkbookPart workbookPart = sd.WorkbookPart;
        WorksheetPart worksheetPart = workbookPart.WorksheetParts.Last();

        // Uncomment the following line to insert row page breaks.
        InsertHorizontalPageBreak(rowIndex, worksheetPart);

        PageSetup pageSetup = new PageSetup() {FitToHeight = 2, FitToWidth = 1};
        worksheetPart.Worksheet.AppendChild(pageSetup);

    }
    finally
    {
        if (sd != null)
            ((IDisposable)sd).Dispose();
    }
}

但是在处理完之后,当我尝试打开文件时,出现了一个错误:“抱歉,我们发现一些内容存在问题[...]”。

你们有任何想法可以帮助我吗?

非常感谢!

2个回答

1
OpenXML SDK提供了一个名为PageSetupProperties的类,其中提供了一个名为FitToPage的属性。
将此属性设置为true,以选择单选按钮:
SheetProperties _oPageSetupProperties = new SheetProperties(new PageSetupProperties());
newWorksheetPart.Worksheet.SheetProperties = _oPageSetupProperties;

// Set the FitToPage property to true
newWorksheetPart.Worksheet.SheetProperties.PageSetupProperties.FitToPage = BooleanValue.FromBoolean(true);

//set fitto width and height
PageSetup pageSetup = new PageSetup();
pageSetup.Orientation = OrientationValues.Landscape;
pageSetup.FitToWidth = 1;
pageSetup.FitToHeight = 50;
newWorksheetPart.Worksheet.AppendChild(pageSetup);

1
很难在没有看到文件的情况下确定,但我相信这是由于元素顺序的原因。 ECMA-376标准定义了Excel文档的模式,其中之一是Worksheet定义(第3900页)。该定义太大,无法在此处完整粘贴,但关于PageSetup的部分如下:
<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:element name = "rowBreaks" type="CT_PageBreak" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "colBreaks" type="CT_PageBreak" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "customProperties" type="CT_CustomProperties" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "cellWatches" type="CT_CellWatches" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "ignoredErrors" type="CT_IgnoredErrors" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "smartTags" type="CT_SmartTags" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "drawing" type="CT_Drawing" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "drawingHF" type="CT_DrawingHF" minOccurs="2222 0" maxOccurs="1"/>
<xsd:element name = "picture" type="CT_SheetBackgroundPicture" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "oleObjects" type="CT_OleObjects" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "controls" type="CT_Controls" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "webPublishItems" type="CT_WebPublishItems" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "tableParts" type="CT_TableParts" minOccurs="0" maxOccurs="1"/>
<xsd:element name = "extLst" type="CT_ExtensionList" minOccurs="0" maxOccurs="1"/>

作为序列定义的Worksheet,顺序很重要。从您的截图中看,似乎您有一个Header,它必须在PageSetup之后,但是您的代码将PageSetup添加到了Worksheet的末尾。
有关如何将项目添加到正确位置的示例,请参见我的回答为什么在此示例中追加AutoFilter会损坏我的Excel文件?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接