是否有可能将手动分页符添加到数据透视表(Excel Interop)?

当以编程方式生成一个“正常”的Excel表,一个范围的PageBreak属性可以设置为xlPageBreakManual,然后你可以指定页面的代码,如下所示:

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

…但是这是可能的时打印数据透视表? 我不这么认为,因为数据透视表是从一个数据源(在我的情况下是我随后隐藏的“原始数据”页面)提供的,但是如果是这样的话,我想知道如何。

这就是我现在要做的印刷数据透视表:

 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,如果一个块开始时当前页面上的空间less于5行,则跳转到带分页符的下一页。

这可能与数据透视表?

我明白,当你指的是“逻辑数据块”时 ,意味着数据对应于PivotFieldItem 。 如果是这样的话,试试这个:

将这些PivotTable属性设置为TRUE

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

还要设置为TRUE每个项目发生更改时要为其设置分页PivotFieldLayoutPageBreak属性:

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

如果只有一个logging的项目,还需要将此属性设置为TRUE

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

根据需要replace“数据透视表1”和“类”值

虽然上面的命令是在VBA但是他们应该给你一个关于如何使用C#