spire.presentation提供了丰富全面的api供程序员对powerpoint表格进行处理。本文将介绍如何使用该组件创建表格,删除行和列,合并单元格,设置自定义格式和删除表格。
创建表格
c#
//创建一个powerpoint文档
presentation ppt = new presentation();
ppt.slidesize.type = slidesizetype.screen16x9;
//初始化一个itable实例,并指定位置、行数和列数、行高和列宽
double[] widths = new double[] { 100, 100, 100, 100 };
double[] heights = new double[] { 15, 15, 15, 15};
itable table = ppt.slides[0].shapes.appendtable(80, 80, widths, heights);
//为表格设置内置格式
table.stylepreset = tablestylepreset.lightstyle1accent2;
//声明并初始化一个string[,]数组
string[,] data = new string[,]
{
{"姓名","年龄","性别","工号" },
{"张三","28","男","0023" },
{"李四","30","男","0024" },
{"王五","26","女","0025" }
};
//将数组内容填充到表格
for (int i = 0; i < 4; i )
{
for (int j = 0; j < 4; j )
{
table[j, i].textframe.text = data[i, j];
table[j, i].textframe.paragraphs[0].textranges[0].latinfont = new textfont("arial");
}
}
//保存文档
ppt.savetofile("创建表格.pptx", fileformat.pptx2013);
vb.net
'创建一个powerpoint文档
dim ppt as new presentation()
ppt.slidesize.type = slidesizetype.screen16x9
'初始化一个itable实例,并指定位置、行数和列数、行高和列宽
dim widths as double() = new double() {100, 100, 100, 100}
dim heights as double() = new double() {15, 15, 15, 15}
dim table as itable = ppt.slides(0).shapes.appendtable(80, 80, widths, heights)
'为表格设置内置格式
table.stylepreset = tablestylepreset.lightstyle1accent2
'声明并初始化一个string[,]数组
dim data as string(,) = new string(,) {{"姓名", "年龄", "性别", "工号"}, {"张三", "28", "男", "0023"}, {"李四", "30", "男", "0024"}, {"王五", "26", "女", "0025"}}
'将数组内容填充到表格
for i as integer = 0 to 3
for j as integer = 0 to 3
table(j, i).textframe.text = data(i, j)
table(j, i).textframe.paragraphs(0).textranges(0).latinfont = new textfont("arial")
next
next
'保存文档
ppt.savetofile("创建表格.pptx", fileformat.pptx2013)
删除行和列
c#
//初始化一个presentation实例
presentation ppt = new presentation();
//加载一个powerpoint文档
ppt.loadfromfile("创建表格.pptx");
//获取第一张幻灯片上的表格
itable table = null;
foreach (ishape shape in ppt.slides[0].shapes)
{
if (shape is itable)
{
table = (itable)shape;
//删除第三列
table.columnslist.removeat(2, false);
//删除第四行
table.tablerows.removeat(3, false);
}
}
//保存文档
ppt.savetofile("删除行与列.pptx",fileformat.pptx2013);
vb.net
'初始化一个presentation实例
dim ppt as new presentation()
'加载一个powerpoint文档
ppt.loadfromfile("创建表格.pptx")
'获取第一张幻灯片上的表格
dim table as itable = nothing
for each shape as ishape in ppt.slides(0).shapes
if typeof shape is itable then
table = directcast(shape, itable)
'删除第三列
table.columnslist.removeat(2, false)
'删除第四行
table.tablerows.removeat(3, false)
end if
next
'保存文档
ppt.savetofile("删除行与列.pptx", fileformat.pptx2013)
合并单元格、设置单元格格式
c#
//创建一个powerpoint文档
presentation ppt = new presentation();
ppt.slidesize.type = slidesizetype.screen16x9;
//初始化一个itable实例,并指定位置、行数和列数、行高和列宽
double[] widths = new double[] { 100, 100, 100, 100 };
double[] heights = new double[] { 15, 15, 15, 15 };
itable table = ppt.slides[0].shapes.appendtable(80, 80, widths, heights);
//声明并初始化一个string[,]数组
string[,] data = new string[,]
{
{"订单及支付情况","","","" },
{"订单号","日期","客户","是否支付" },
{"1021","2017/5/1","阳光地产","是" },
{"1022","2017/5/2","","否" }
};
//将数组内容填充到表格
for (int i = 0; i < 4; i )
{
for (int j = 0; j < 4; j )
{
table[j, i].textframe.text = data[i, j];
table[j, i].textframe.paragraphs[0].textranges[0].latinfont = new textfont("arial");
}
}
//将表格的默认格式设为空(自定义表格格式须清除表格默认格式)
table.stylepreset = tablestylepreset.none;
//合并第一行的单元格
for (int i = 0; i < table.columnslist.count-1; i )
{
cell cell1 = table[i, 0];
cell cell2 = table[i 1, 0];
table.mergecells(cell1, cell2, true);
}
//在合并后新的单元格设置文字水平对齐方式和背景色
cell newcell1 = table[0, 0];
newcell1.textframe.paragraphs[0].alignment = textalignmenttype.center;
newcell1.fillformat.filltype = fillformattype.solid;
newcell1.fillformat.solidcolor.color = system.drawing.color.gray;
//垂直合并单元格[2,2]和单元格[2,3]
cell cell3 = table[2, 2];
cell cell4 = table[2, 3];
table.mergecells(cell3, cell4, true);
//在合并后新的单元格设置文字垂直对齐方式
cell newcell2 = table[2, 2];
newcell2.textanchortype = textanchortype.center;
//为单元格[3,2]、[3,3]设置背景色
cell cell5 = table[3, 2];
cell cell6 = table[3, 3];
cell5.fillformat.filltype = fillformattype.solid;
cell6.fillformat.filltype = fillformattype.solid;
cell5.fillformat.solidcolor.color = system.drawing.color.green;
cell6.fillformat.solidcolor.color = system.drawing.color.red;
//保存文档
ppt.savetofile("操作单元格.pptx", fileformat.pptx2013);
vb.net
'创建一个powerpoint文档
dim ppt as new presentation()
ppt.slidesize.type = slidesizetype.screen16x9
'初始化一个itable实例,并指定位置、行数和列数、行高和列宽
dim widths as double() = new double() {100, 100, 100, 100}
dim heights as double() = new double() {15, 15, 15, 15}
dim table as itable = ppt.slides(0).shapes.appendtable(80, 80, widths, heights)
'声明并初始化一个string[,]数组
dim data as string(,) = new string(,) {{"订单及支付情况", "", "", ""}, {"订单号", "日期", "客户", "是否支付"}, {"1021", "2017/5/1", "阳光地产", "是"}, {"1022", "2017/5/2", "", "否"}}
'将数组内容填充到表格
for i as integer = 0 to 3
for j as integer = 0 to 3
table(j, i).textframe.text = data(i, j)
table(j, i).textframe.paragraphs(0).textranges(0).latinfont = new textfont("arial")
next
next
'将表格的默认格式设为空(自定义表格格式须清除表格默认格式)
table.stylepreset = tablestylepreset.none
'合并第一行的单元格
for i as integer = 0 to table.columnslist.count - 2
dim cell1 as cell = table(i, 0)
dim cell2 as cell = table(i 1, 0)
table.mergecells(cell1, cell2, true)
next
'在合并后新的单元格设置文字水平对齐方式和背景色
dim newcell1 as cell = table(0, 0)
newcell1.textframe.paragraphs(0).alignment = textalignmenttype.center
newcell1.fillformat.filltype = fillformattype.solid
newcell1.fillformat.solidcolor.color = system.drawing.color.gray
'垂直合并单元格[2,2]和单元格[2,3]
dim cell3 as cell = table(2, 2)
dim cell4 as cell = table(2, 3)
table.mergecells(cell3, cell4, true)
'在合并后新的单元格设置文字垂直对齐方式
dim newcell2 as cell = table(2, 2)
newcell2.textanchortype = textanchortype.center
'为单元格[3,2]、[3,3]设置背景色
dim cell5 as cell = table(3, 2)
dim cell6 as cell = table(3, 3)
cell5.fillformat.filltype = fillformattype.solid
cell6.fillformat.filltype = fillformattype.solid
cell5.fillformat.solidcolor.color = system.drawing.color.green
cell6.fillformat.solidcolor.color = system.drawing.color.red
'保存文档
ppt.savetofile("操作单元格.pptx", fileformat.pptx2013)
删除表格
c#
//初始化一个presentation实例
presentation ppt = new presentation();
//加载一个powerpoint文档
ppt.loadfromfile("创建表格.pptx");
//初始化一个list对象,元素类型为ishape
list tableshapes= new list();
//获取第一张幻灯片上所有的表格图形并添加到list
foreach (ishape shape in ppt.slides[0].shapes)
{
if (shape is itable)
{
tableshapes.add(shape);
}
}
//从幻灯片删除第一个表格图形
ppt.slides[0].shapes.remove(tableshapes[0]);
//保存文档
ppt.savetofile("删除表格.pptx", fileformat.pptx2013);
vb.net
'初始化一个presentation实例
dim ppt as new presentation()
'加载一个powerpoint文档
ppt.loadfromfile("创建表格.pptx")
'初始化一个list对象,元素类型为ishape
dim tableshapes as new list(of ishape)()
'获取第一张幻灯片上所有的表格图形并添加到list
for each shape as ishape in ppt.slides(0).shapes
if typeof shape is itable then
tableshapes.add(shape)
end if
next
'从幻灯片删除第一个表格图形
ppt.slides(0).shapes.remove(tableshapes(0))
'保存文档
ppt.savetofile("删除表格.pptx", fileformat.pptx2013)