2016-11-08 52 views
0

當編程生成一個「正常」的Excel工作表,一個範圍的PageBreak屬性可以設置爲xlPageBreakManual,然後你可以指定頁面與代碼打破像這樣:是否可以向數據透視表(Excel Interop)添加手動分頁符?

workbook.Worksheets[0].VPageBreaks.Add(sheet.Range["G1"]); 

...但就是這個打印數據透視表時可能嗎?我不這麼認爲,因爲數據透視表是從一個數據源(在我的案例中是一個「原始數據」頁面,我隨後隱藏的頁面)提供的,但如果是這樣,我想知道如何。

這就是我現在做的準備印刷的PivotTablized片:

private void FormatPivotTable() 
{ 
    . . . 
    ConfigureForPrinting(_xlPivotTableSheet.UsedRange.Rows.Count); 
} 

private void ConfigureForPrinting(int finalRow) 
{ 
    string lastColumn = GetExcelTextColumnName(_xlPivotTableSheet.UsedRange.Columns.Count); 
    string printArea = String.Format("A1:{0}{1}", lastColumn, finalRow); 
    _xlPivotTableSheet.PageSetup.PrintArea = printArea; 
    _xlPivotTableSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape; 
    _xlPivotTableSheet.PageSetup.Zoom = false; 
    _xlPivotTableSheet.PageSetup.FitToPagesWide = 1; 
    _xlPivotTableSheet.PageSetup.FitToPagesTall = 50; 

    _xlPivotTableSheet.PageSetup.LeftMargin = _xlApp.Application.InchesToPoints(0.5); 
    _xlPivotTableSheet.PageSetup.RightMargin = _xlApp.Application.InchesToPoints(0.5); 
    _xlPivotTableSheet.PageSetup.TopMargin = _xlApp.Application.InchesToPoints(0.5); 
    _xlPivotTableSheet.PageSetup.BottomMargin = _xlApp.Application.InchesToPoints(0.5); 
    _xlPivotTableSheet.PageSetup.HeaderMargin = _xlApp.Application.InchesToPoints(0.5); 
    _xlPivotTableSheet.PageSetup.FooterMargin = _xlApp.Application.InchesToPoints(0.5); 

    string rangeToRepeat = string.Format("$A$6:${0}$7", lastColumn); 
    _xlPivotTableSheet.PageSetup.PrintTitleRows = rangeToRepeat; 
} 

什麼用戶希望的是,數據的每個邏輯塊放在一起在一張紙上,這樣一個塊(5行)不能跨越多個表單。 IOW,如果塊開始時當前頁面上的空間少於5行,則跳至帶分頁符的下一頁。

這可能與數據透視表?

回答

2

我明白,當你指的是「邏輯數據塊」這意味着數據對應的是PivotFieldItem。如果是這樣的情況下,嘗試這個辦法:

設置這些PivotTable屬性TRUE

ActiveSheet.PivotTables("PivotTable1").PrintTitles = True 
ActiveSheet.PivotTables("PivotTable1").RepeatItemsOnEachPrintedPage = True 

也設置爲TRUE您要到每一個有時間設置分頁符PivotFieldLayoutPageBreak財產項目的變化:

ActiveSheet.PivotTables("PivotTable1").PivotFields("Class").LayoutPageBreak = True 

還需要這個屬性的情況下設置爲TRUE有隻項目一個記錄。

ActiveSheet.PivotTables("PivotTable1").PivotFields("Class").LayoutBlankLine = True 

要求

替換「PivotTable1」和「類」的價值觀雖然上面的命令是VBA他們應該給你如何使用設置這些屬性是個好主意C#

相關問題