本文将介绍如何使用spire.presentation for java组件创建表格,删除表格行和列,删除表格,合并单元格和设置自定义格式。
创建表格
import com.spire.presentation.fileformat;
import com.spire.presentation.itable;
import com.spire.presentation.presentation;
import com.spire.presentation.tablestylepreset;
import com.spire.presentation.textalignmenttype;
public class addtable {
public static void main(string[] args) throws exception {
//实例化一个presentation对象
presentation presentation = new presentation();
//设置表格行数和列数、行高和列宽
double[] widths = new double[] { 80d, 80d, 100d, 80d };
double[] heights = new double[] { 15d, 15d, 15d, 15d, 15d, 15d };
//添加一个表格
itable table = presentation.getslides().get(0).getshapes().appendtable((float)presentation.getslidesize().getsize().getwidth() / 2 - 275, 90, widths, heights);
//设置表格内置样式
table.setstylepreset(tablestylepreset.light_style_3_accent_1);
//设置对齐方式
for (int i = 0; i < 4; i )
{
table.get(i, 0).gettextframe().getparagraphs().get(0).setalignment(textalignmenttype.center);
}
//声明一个string数组
string[][] datastr = new string[][]
{
{"姓名", "性别", "部门", "工号"},
{"winny", "女", "综合", "0109"},
{"lois", "女", "综合", "0111"},
{"jois", "男", "技术", "0110"},
{"moon", "女", "销售", "0112"},
{"winny", "女", "后勤", "0113"}
};
//向表格中填充数据
for (int i = 0; i < 6; i )
{
for (int j = 0; j < 4; j )
{
table.get(j, i).gettextframe().settext(datastr[i][j]);
}
}
//保存文件
presentation.savetofile("e:\\创建表格.pptx", fileformat.pptx_2013);
}
}
删除行和列
import com.spire.presentation.fileformat;
import com.spire.presentation.ishape;
import com.spire.presentation.islide;
import com.spire.presentation.itable;
import com.spire.presentation.presentation;
public class deletetablerowandcolumn {
public static void main(string[] args) throws exception {
//实例化一个ppt对象并加载示例文档
presentation ppt = new presentation();
ppt.loadfromfile("e:\\result.pptx");
//获取第一张幻灯片上的表格
islide slide = ppt.getslides().get(0);
itable table = null;
for(int i = 0; i< slide.getshapes().getcount(); i )
{
ishape shape = slide.getshapes().get(i);
if ((shape instanceof itable)) {
table = ((itable)(shape));
//删除列
table.getcolumnslist().removeat(2, false);
//删除行
table.gettablerows().removeat(3, false);
}
}
//保存文档
ppt.savetofile("e:\\删除行与列.pptx",fileformat.pptx_2013);
}
删除表格
import com.spire.presentation.fileformat;
import com.spire.presentation.presentation;
public class deletetable {
public static void main(string[] args) throws exception {
// 实例化一个ppt对象
presentation ppt = new presentation();
// 加载powerpoint文档
ppt.loadfromfile("e:\\result.pptx");
// 删除第一张幻灯片中的第一个表格
ppt.getslides().get(0).getshapes().removeat(0);
// 保存文档
ppt.savetofile("e:\\删除表格.pptx", fileformat.pptx_2013);
}
}
合并单元格、设置单元格格式
import java.awt.color;
import com.spire.presentation.cell;
import com.spire.presentation.fileformat;
import com.spire.presentation.itable;
import com.spire.presentation.presentation;
import com.spire.presentation.tablestylepreset;
import com.spire.presentation.textalignmenttype;
import com.spire.presentation.textanchortype;
import com.spire.presentation.drawing.fillformattype;
public class customtable {
public static void main(string[] args) throws exception {
//实例化一个presentation对象
presentation presentation = new presentation();
//设置表格行数和列数、行高和列宽
double[] widths = new double[]{80d, 80d, 100d, 80d};
double[] heights = new double[]{15d, 15d, 15d, 15d, 15d};
//添加一个表格
itable table = presentation.getslides().get(0).getshapes().appendtable(90, 90, widths, heights);
//声明一个string数组
string[][] datastr = new string[][]
{
{"订单详细信息", "", "", ""},
{"订单号", "地址", "支付金额", "是否发货"},
{"1101", "北京", "200", "是"},
{"1102", "成都", "100", "否"},
{"1103", "河北", "300", "是"}
};
//向表格中填充数据
for (int i = 0; i < 5; i ) {
for (int j = 0; j < 4; j ) {
table.get(j, i).gettextframe().settext(datastr[i][j]);
}
}
//设置表格的默认格式为空
table.setstylepreset(tablestylepreset.none);
//合并表格第一行的单元格
for (int i = 0; i < table.getcolumnslist().getcount()-1; i )
{
cell cell1 = table.get(i, 0);
cell cell2 = table.get(i 1, 0);
table.mergecells(cell1, cell2, true);
}
//设置单元格的文字水平对齐方式、背景颜色
cell newcell1 = table.get(0, 0);
newcell1.gettextframe().getparagraphs().get(0).setalignment(textalignmenttype.center);
newcell1.getfillformat().setfilltype(fillformattype.solid);
newcell1.getfillformat().getsolidcolor().setcolor(color.gray);
//垂直合并单元格[2,2]和单元格[2,3]
cell cell3 = table.get(2, 2);
cell cell4 = table.get(2, 3);
table.mergecells(cell3, cell4, true);
//设置文字垂直对齐方式
cell newcell2 = table.get(2, 2);
newcell2.settextanchortype(textanchortype.center);
//设置单元格[3,2]、[3,3]的背景色
cell cell5 = table.get(3, 2);
cell cell6 = table.get(3, 3);
cell5.getfillformat().setfilltype(fillformattype.solid);
cell6.getfillformat().setfilltype(fillformattype.solid);
cell5.getfillformat().getsolidcolor().setcolor(color.blue);
cell6.getfillformat().getsolidcolor().setcolor(color.yellow);
//保存文档
presentation.savetofile("e:\\操作表格.pptx", fileformat.pptx_2013);
}
}