2016-11-23 51 views
3

私のプログラムからDevExpressで作成したExcelファイルがあります。このファイルに水平ブレークページを追加する必要がありますが、私のDevExpressバージョンがそれを処理しないため、私はできません。そこで、別のクラスでOpenXMLを使用して、生成されたExcelファイルを取得して、水平ブレークページを追加します。pageSetupを設定するとOpenXMLファイルが壊れます

DevExpress社の世代の後、私のファイルは次のようになります。 enter image description here

だから、6ページを持っています。私はこれの代わりにこれを持っています: enter image description here

別のシートに各タブを印刷するために。

だから私は私のExcelファイルの幅と高さを定義するためのOpenXMLからのPageSetupを使用:

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

しかし、処理した後、私はエラーが現れているファイルを開こうとすると、「申し訳ありませんが、我々は問題を発見いくつかの内容で[...] "

あなたは私を助ける方法を知っていますか?

多くの感謝!

答えて

1

ファイルを見ることなく伝えるのは難しいですが、私はあなたの要素の順序のためにこれが起こっていると確信しています。

ECMA-376 standardは、Excelドキュメントのスキーマを定義します。その一部はWorksheetの定義です(3900ページ)。定義は、その全体をここに貼り付けるには大きすぎるが、PageSetupに関するセクションでは、次のようになります。Worksheetとして

<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"/> 

はシーケンスとして定義され、順序が重要です。あなたのスクリーンショットを見ると、の後にになるが表示されますが、PageSetupPageSetupの最後にWorksheetの最後にコードが追加されています。あなたは正しい場所にアイテムを追加することができます方法の例について

は、OpenXMLのSDKはFitToPageと呼ばれるプロパティを提供するクラスと呼ばれるPageSetupPropertiesを提供 Why appending AutoFilter corrupts my excel file in this example?

0

に私の答えを参照してください。

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); 
を:ラジオボタンを選択し得るために、このプロパティをtrueに設定し

関連する問題