C# 将表格拆分为PowerPoint文件中的多张幻灯片
我正在使用并在我的PPTX文件中创建一个大表。类似于,例如:C# 将表格拆分为PowerPoint文件中的多张幻灯片,c#,table-splitting,gembox-presentation,C#,Table Splitting,Gembox Presentation,我正在使用并在我的PPTX文件中创建一个大表。类似于,例如: PresentationDocument presentation=新的PresentationDocument(); 幻灯片=presentation.Slides.AddNew(SlideLayoutType.Custom); int rowCount=100; int columnCount=4; int columnWidth=5; Table Table=slide.Content.AddTable(1,1,columnCo
PresentationDocument presentation=新的PresentationDocument();
幻灯片=presentation.Slides.AddNew(SlideLayoutType.Custom);
int rowCount=100;
int columnCount=4;
int columnWidth=5;
Table Table=slide.Content.AddTable(1,1,columnCount*columnWidth,0,LengthUnit.cm);
对于(int i=0;i
正如所料,该表不适合幻灯片:
因此,我需要将这个表拆分为多个表或多张幻灯片,以便每个表都适合其幻灯片,并且所有行都可见
我该怎么做?如何确定新的
表格行
是否会超过幻灯片
高度?如果有动态行高度(例如,某些单元格可能包含多行文本),则需要对内容进行分页
例如,请参见以下内容:
table.Frame.FormatDrawing(new PaginatorOptions() { UpdateTableRowHeights = true });
DrawingLayout tableLayout = table.Frame.Layout;
double maxHeight = presentation.SlideSize.Height - tableLayout.Top;
double currentHeight = 0;
TableRowCollection sourceRows = table.Rows;
TableRowCollection newRows = null;
int currentRowIndex = 0;
// Split the main table into multiple new tables based on the row heights.
while (currentRowIndex < sourceRows.Count)
{
currentHeight += sourceRows[currentRowIndex].Height;
// Create new slide with new table.
if (currentHeight > maxHeight)
{
currentHeight = sourceRows[currentRowIndex].Height;
Slide newSlide = presentation.Slides.AddNew(SlideLayoutType.Blank);
Table newTable = newSlide.Content.AddTable(tableLayout.Left, tableLayout.Top, tableLayout.Width, 0);
foreach (var column in table.Columns)
newTable.Columns.AddClone(column);
newRows = newTable.Rows;
}
// Move row from the main table to a new table.
if (newRows != null)
{
newRows.AddClone(sourceRows[currentRowIndex]);
sourceRows.RemoveAt(currentRowIndex);
}
else
{
++currentRowIndex;
}
}
presentation.Save("output.pptx");
int rowsPerSlide = 16;
TableRowCollection rows = table.Rows;
DrawingLayout layout = table.Frame.Layout;
for (int t = 1, tablesCount = (int)Math.Ceiling(rows.Count / (double)rowsPerSlide); t < tablesCount; t++)
{
Slide newSlide = presentation.Slides.AddNew(SlideLayoutType.Blank);
Table newTable = newSlide.Content.AddTable(layout.Left, layout.Top, layout.Width, 0);
foreach (var column in table.Columns)
newTable.Columns.AddClone(column);
for (int r = rowsPerSlide, rowsCount = Math.Min(rowsPerSlide * 2, rows.Count); r < rowsCount; r++)
{
newTable.Rows.AddClone(rows[rowsPerSlide]);
rows.RemoveAt(rowsPerSlide);
}
}
presentation.Save("output.pptx");
table.Frame.FormatDrawing(新的PaginatorOptions(){UpdateTableRowHeights=true});
DrawingLayout tableLayout=table.Frame.Layout;
double maxHeight=presentation.SlideSize.Height-tableLayout.Top;
双电流高度=0;
TableRowCollection sourceRows=table.Rows;
TableRowCollection newRows=null;
int currentRowIndex=0;
//根据行高将主表拆分为多个新表。
while(currentRowIndex最大高度)
{
currentHeight=sourceRows[currentRowIndex]。高度;
Slide newSlide=presentation.Slides.AddNew(SlideLayoutType.Blank);
Table newTable=newSlide.Content.AddTable(tableLayout.Left,tableLayout.Top,tableLayout.Width,0);
foreach(table.Columns中的var列)
newTable.Columns.AddClone(column);
newRows=newTable.Rows;
}
//将行从主表移动到新表。
if(新行!=null)
{
AddClone(sourceRows[currentRowIndex]);
RemoveAt(currentRowIndex);
}
其他的
{
++当前行索引;
}
}
presentation.Save(“output.pptx”);
如果您有恒定的行高,如屏幕截图所示,则可以简化此操作
例如,如下所示:
table.Frame.FormatDrawing(new PaginatorOptions() { UpdateTableRowHeights = true });
DrawingLayout tableLayout = table.Frame.Layout;
double maxHeight = presentation.SlideSize.Height - tableLayout.Top;
double currentHeight = 0;
TableRowCollection sourceRows = table.Rows;
TableRowCollection newRows = null;
int currentRowIndex = 0;
// Split the main table into multiple new tables based on the row heights.
while (currentRowIndex < sourceRows.Count)
{
currentHeight += sourceRows[currentRowIndex].Height;
// Create new slide with new table.
if (currentHeight > maxHeight)
{
currentHeight = sourceRows[currentRowIndex].Height;
Slide newSlide = presentation.Slides.AddNew(SlideLayoutType.Blank);
Table newTable = newSlide.Content.AddTable(tableLayout.Left, tableLayout.Top, tableLayout.Width, 0);
foreach (var column in table.Columns)
newTable.Columns.AddClone(column);
newRows = newTable.Rows;
}
// Move row from the main table to a new table.
if (newRows != null)
{
newRows.AddClone(sourceRows[currentRowIndex]);
sourceRows.RemoveAt(currentRowIndex);
}
else
{
++currentRowIndex;
}
}
presentation.Save("output.pptx");
int rowsPerSlide = 16;
TableRowCollection rows = table.Rows;
DrawingLayout layout = table.Frame.Layout;
for (int t = 1, tablesCount = (int)Math.Ceiling(rows.Count / (double)rowsPerSlide); t < tablesCount; t++)
{
Slide newSlide = presentation.Slides.AddNew(SlideLayoutType.Blank);
Table newTable = newSlide.Content.AddTable(layout.Left, layout.Top, layout.Width, 0);
foreach (var column in table.Columns)
newTable.Columns.AddClone(column);
for (int r = rowsPerSlide, rowsCount = Math.Min(rowsPerSlide * 2, rows.Count); r < rowsCount; r++)
{
newTable.Rows.AddClone(rows[rowsPerSlide]);
rows.RemoveAt(rowsPerSlide);
}
}
presentation.Save("output.pptx");
int rowsPerSlide=16;
TableRowCollection行=table.rows;
DrawingLayout=table.Frame.layout;
对于(int t=1,tableCount=(int)Math.天花(rows.Count/(double)rowsPerSlide);t