本文将介绍如何使用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);
}
}