是否可以在数据透视表(Excel Interop)中添加手动分页符?

本文关键字:添加 分页 Interop Excel 数据 透视 是否 | 更新日期: 2023-09-27 18:17:03

当以编程方式生成"正常" Excel表格时,可以将Range的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行)就不会跨越多个工作表。如果一个块开始时当前页上的空间少于5行,则使用换页符跳转到下一页。

数据透视表是可能的吗?

是否可以在数据透视表(Excel Interop)中添加手动分页符?

我理解,当你指的是"数据的逻辑块"它意味着数据对应于一个PivotFieldItem。如果是这种情况,试试这个:

设置这些PivotTable属性为TRUE:

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

还将PivotFieldLayoutPageBreak属性设置为TRUE,您希望每次更改条目时设置换行符:

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

还需要将此属性设置为TRUE,以防只有一条记录的项目

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

根据需要替换"PivotTable1"answers"Class"值

虽然上面的命令是在VBA中,但它们应该能让你很好地了解如何使用C#

设置这些属性。