2016-11-23 115 views
3

我的程序中有一個使用DevExpress創建的excel文件。我需要在這個文件中添加水平中斷頁面,但我不能,因爲我的DevExpress版本無法處理它。所以我在一個單獨的類中使用OpenXML來檢索生成的excel文件,以便將其添加到水平中斷頁面。當我設置頁面設​​置時,OpenXML破壞了excel文件

的DevExpress的生成之後,我的文件看起來像這樣: enter image description here

所以它擁有6頁。我想代之以: enter image description here

爲了在單獨的工作表上打印每個選項卡。

所以我用PAGESETUP從OpenXML可以定義我的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(); 
    } 
} 

但處理後,當我嘗試打開該文件時出錯是顯示了「對不起,我們發現了一個問題有些內容「。

你們有什麼想法如何幫助我嗎?

非常感謝!

回答

1

很難說沒有看到文件,但我很確定這是因爲你的元素的順序發生。

ECMA-376 standard定義了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但你的代碼是PageSetupWorksheet的末尾加入。

舉一個例子你如何添加項目到正確的位置,請參閱我的回答Why appending AutoFilter corrupts my excel file in this example?

0

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