1.程序说明
1.1编程语言:Java
1.2 第三方库:Apache POI
Apache POI 官网:
http://poi.apache.org/
1.3程序功能
使用Apache POI创建带有表格的PPT
1.4程序作者
Fans同学
2.程序源代码
package org.apache.poi.hslf.examples;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.FileOutputStream;
import org.apache.poi.hslf.model.Line;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.Table;
import org.apache.poi.hslf.model.TableCell;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.Table;
import org.apache.poi.hslf.model.TableCell;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
/**
* 创建带有表格的PPT
*
* @author Fans.Lei
*/
public final class PPTTableDemo {
* 创建带有表格的PPT
*
* @author Fans.Lei
*/
public final class PPTTableDemo {
public static final String filePath = "c:/poi/fans.ppt";
// 入口函数
public static void main(String[] args) throws Exception {
PPTTableDemo tableDemo = new PPTTableDemo();
tableDemo.createPPT();
}
public static void main(String[] args) throws Exception {
PPTTableDemo tableDemo = new PPTTableDemo();
tableDemo.createPPT();
}
// 创建一个含有2个表格的只有1页内容的PPT
public void createPPT() throws Exception {
public void createPPT() throws Exception {
// 第一个表格的测试数据
String[][] namePlaceDatas = { { "昵称", "工作地点" }, { "Fans同学", "北京市朝阳区" },
{ "刺客", "广东省珠海市" }, { "嘻嘻猴", "广东省深圳市" }, { "月亮", "湖北省武汉市" },
{ "Wonderful", "湖北省武汉市" } };
String[][] namePlaceDatas = { { "昵称", "工作地点" }, { "Fans同学", "北京市朝阳区" },
{ "刺客", "广东省珠海市" }, { "嘻嘻猴", "广东省深圳市" }, { "月亮", "湖北省武汉市" },
{ "Wonderful", "湖北省武汉市" } };
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
// 6行2列
Table namePlaces = new Table(6, 2);
for (int i = 0; i < namePlaceDatas.length; i++) {
for (int j = 0; j < namePlaceDatas[i].length; j++) {
TableCell cell = namePlaces.getCell(i, j);
cell.setText(namePlaceDatas[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("宋体");
rt.setFontSize(14);
if (i == 0) {
cell.getFill().setForegroundColor(new Color(227, 227, 227));
rt.setBold(true);
} else {
// /rt.setBold(true);
}
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
}
}
Table namePlaces = new Table(6, 2);
for (int i = 0; i < namePlaceDatas.length; i++) {
for (int j = 0; j < namePlaceDatas[i].length; j++) {
TableCell cell = namePlaces.getCell(i, j);
cell.setText(namePlaceDatas[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("宋体");
rt.setFontSize(14);
if (i == 0) {
cell.getFill().setForegroundColor(new Color(227, 227, 227));
rt.setBold(true);
} else {
// /rt.setBold(true);
}
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
}
}
// 设置表格的边框
Line border1 = namePlaces.createBorder();
border1.setLineColor(Color.black);
border1.setLineWidth(1.0);
namePlaces.setAllBorders(border1);
// 设置表格的列宽
namePlaces.setColumnWidth(0, 200);
namePlaces.setColumnWidth(1, 200);
namePlaces.setColumnWidth(0, 200);
namePlaces.setColumnWidth(1, 200);
slide.addShape(namePlaces);
int pgWidth = ppt.getPageSize().width;
namePlaces.moveTo((pgWidth - namePlaces.getAnchor().width) / 2, 100);
int pgWidth = ppt.getPageSize().width;
namePlaces.moveTo((pgWidth - namePlaces.getAnchor().width) / 2, 100);
// 第二个表格的测试数据
String[][] bookDatas = {
{ "Fans荐书" },
{ "<<浪潮之巅>>@吴军--高科技公司的兴衰史\r" + "<<孙子兵法>>@孙子--兵学圣典,天下第一兵书\r"
+ "<<货币战争>>@宋鸿兵--欧美主要金融势力的兴衰和发展" } };
String[][] bookDatas = {
{ "Fans荐书" },
{ "<<浪潮之巅>>@吴军--高科技公司的兴衰史\r" + "<<孙子兵法>>@孙子--兵学圣典,天下第一兵书\r"
+ "<<货币战争>>@宋鸿兵--欧美主要金融势力的兴衰和发展" } };
// 2行1列
Table books = new Table(2, 1);
for (int i = 0; i < bookDatas.length; i++) {
for (int j = 0; j < bookDatas[i].length; j++) {
TableCell cell = books.getCell(i, j);
cell.setText(bookDatas[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontSize(10);
rt.setFontName("宋体");
if (i == 0) {
cell.getFill().setForegroundColor(new Color(0, 51, 102));
rt.setFontColor(Color.white);
rt.setBold(true);
rt.setFontSize(16);
cell.setHorizontalAlignment(TextBox.AlignCenter);
} else {
rt.setBullet(true);
rt.setFontSize(14);
cell.setHorizontalAlignment(TextBox.AlignLeft);
}
cell.setVerticalAlignment(TextBox.AnchorMiddle);
}
}
Table books = new Table(2, 1);
for (int i = 0; i < bookDatas.length; i++) {
for (int j = 0; j < bookDatas[i].length; j++) {
TableCell cell = books.getCell(i, j);
cell.setText(bookDatas[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontSize(10);
rt.setFontName("宋体");
if (i == 0) {
cell.getFill().setForegroundColor(new Color(0, 51, 102));
rt.setFontColor(Color.white);
rt.setBold(true);
rt.setFontSize(16);
cell.setHorizontalAlignment(TextBox.AlignCenter);
} else {
rt.setBullet(true);
rt.setFontSize(14);
cell.setHorizontalAlignment(TextBox.AlignLeft);
}
cell.setVerticalAlignment(TextBox.AnchorMiddle);
}
}
// 设置 表格的列宽和行高
books.setColumnWidth(0, 400);
books.setRowHeight(0, 30);
books.setRowHeight(1, 70);
Line border2 = books.createBorder();
books.setOutsideBorders(border2);
books.setOutsideBorders(border2);
slide.addShape(books);
int pgWidth2 = ppt.getPageSize().width;
books.moveTo((pgWidth2 - books.getAnchor().width) / 2, 400);
books.moveTo((pgWidth2 - books.getAnchor().width) / 2, 400);
// 将PPT写入文件中
FileOutputStream out = new FileOutputStream(filePath);
ppt.write(out);
out.close();
FileOutputStream out = new FileOutputStream(filePath);
ppt.write(out);
out.close();
}
}
}
3.程序运行结果
版权声明:本文为博主原创文章,未经博主允许不得转载。