C# 是否可以将手动分页符添加到数据透视表(Excel互操作)?
以编程方式生成“普通”Excel工作表时,可以将范围的PageBreak属性设置为xlPageBreakManual,然后可以使用如下代码指定页面的中断位置: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
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#