Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以将手动分页符添加到数据透视表(Excel互操作)?_C#_Excel_Pivot Table_Excel Interop_Page Break - Fatal编程技术网

C# 是否可以将手动分页符添加到数据透视表(Excel互操作)?

C# 是否可以将手动分页符添加到数据透视表(Excel互操作)?,c#,excel,pivot-table,excel-interop,page-break,C#,Excel,Pivot Table,Excel Interop,Page Break,以编程方式生成“普通”Excel工作表时,可以将范围的PageBreak属性设置为xlPageBreakManual,然后可以使用如下代码指定页面的中断位置: workbook.Worksheets[0].VPageBreaks.Add(sheet.Range["G1"]); …但在打印数据透视表时是否可以这样做?我不这么认为,因为数据透视表是从数据源(在我的例子中是一页“原始数据”,我随后将其隐藏)馈送的,但如果是,我想知道如何馈送 以下是我现在准备打印的数据透视表的步骤: private

以编程方式生成“普通”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行)就不会跨越多个工作表。注意,如果块开始时当前页上的空间少于5行,请使用分页符跳到下一页


数据透视表是否可以这样做?

我理解,当您指的是“逻辑数据块”时,它指的是与
数据透视字段项相对应的数据。如果是这种情况,请尝试以下方法:

将这些
数据透视表属性设置为
TRUE

ActiveSheet.PivotTables("PivotTable1").PrintTitles = True
ActiveSheet.PivotTables("PivotTable1").RepeatItemsOnEachPrintedPage = True
还将
数据透视字段的
LayoutPageBreak
属性设置为
TRUE
,以便在每次更改项目时为其设置分页符:

ActiveSheet.PivotTables("PivotTable1").PivotFields("Class").LayoutPageBreak = True
如果项目只有一条记录,还需要将此属性设置为
TRUE

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


尽管上面的命令在
VBA
中,但它们应该能让您很好地了解如何使用
C#

设置这些属性,我理解,当您提到“逻辑数据块”时,它指的是与
数据透视字段项相对应的数据。如果是这种情况,请尝试以下方法:

将这些
数据透视表属性设置为
TRUE

ActiveSheet.PivotTables("PivotTable1").PrintTitles = True
ActiveSheet.PivotTables("PivotTable1").RepeatItemsOnEachPrintedPage = True
还将
数据透视字段的
LayoutPageBreak
属性设置为
TRUE
,以便在每次更改项目时为其设置分页符:

ActiveSheet.PivotTables("PivotTable1").PivotFields("Class").LayoutPageBreak = True
如果项目只有一条记录,还需要将此属性设置为
TRUE

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

尽管上面的命令位于
VBA
中,但它们应该能让您很好地了解如何使用
C#