[size=large][color=red]使用POI操作PPT文档[/color][/size] [url]http://zyplaiai.iteye.com/blog/778547[/url]
1)如果是创建新的PPT文档,直接使用SlideShow和Slide类就可以,其中SlideShow表示PPT文档,Slide表示某一张幻灯片
如下代码创建空的PPT文档:
SlideShow ppt = new SlideShow();
Slide[] slides = ppt.getSlides();
assertTrue(slides.length == 0);
savePPTFile(ppt);
private void savePPTFile(SlideShow ppt) throws Exception{
FileOutputStream out = new FileOutputStream("ppt测试.ppt");
ppt.write(out);
out.close();
}
2)设置母版,这样后续的新建幻灯片都将使用母版的字体,背景等设置
SlideShow ppt = new SlideShow();
//设置幻灯片大小
ppt.setPageSize(new Dimension(760,600));
SlideMaster master = ppt.getSlidesMasters()[0];
//设置母板背景,支持多种图片格式
int picIndex = ppt.addPicture(new File("background.png"), Picture.PNG);
Picture background = new Picture(picIndex);
//设置图片位置
background.setAnchor(new java.awt.Rectangle(0, 0, ppt.getPageSize().width
, ppt.getPageSize().height));
master.addShape(background);
3)创建幻灯片并插入文本
SlideShow ppt = new SlideShow();
Slide newSlide = ppt.createSlide();
//添加幻灯片标题
TextBox title = newSlide.addTitle();
RichTextRun titleRun = title.getTextRun().getRichTextRuns()[0];
titleRun.setFontColor(Color.RED);
title.setText("ppt测试");
//添加文本框
TextBox txt = new TextBox();
RichTextRun richTextRun = txt.getTextRun().getRichTextRuns()[0];
richTextRun.setFontColor(Color.BLUE);
//setText参数字符串可以包含回车、换行符,但是最后一行不能以\r\n结尾,否则设置的格式没有效果(v3.5)
richTextRun.setText("这里可以换行\r\n第二行文本");
txt.setAnchor(new java.awt.Rectangle(50,150,400,400));
newSlide.addShape(txt);
savePPTFile(ppt);
4)插入图片,支持多种格式
SlideShow ppt = new SlideShow();
Slide newSlide = ppt.createSlide();
int picIndex = ppt.addPicture(new File("图片.jpg"), Picture.JPEG);
Picture jpg = new Picture(picIndex);
//set image position in the slide
jpg.setAnchor(new java.awt.Rectangle(360, 200, 280, 260));
newSlide.addShape(jpg);
savePPTFile(ppt);
5)插入表格(v3.5)
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
String[][] datas = {
{"序号", "姓名","年龄"},
{"1", "张三","30"},
{"2", "李四","27"},
};
//create a table of 3 rows and 3 columns
Table table = new Table(3, 3);
for (int i = 0; i < datas.length; i++) {
for (int j = 0; j < datas[i].length; j++) {
TableCell cell = table.getCell(i, j);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("宋体");
rt.setFontSize(12);
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
cell.setText(datas[i][j]);
if(i == 0){//首行背景设置为灰色
cell.setFillColor(Color.GRAY);
}
}
}
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(2.0);
table.setAllBorders(border);
slide.addShape(table);
table.moveTo(160,260);
savePPTFile(ppt);
6)如果是读取已存在的PPT文档则还要用到HSLFSlideShow,下面代码将PPT文件导出为图片(png)格式,如果幻灯片上有中文字符则这些字符的字体需要修改为支持中文的字体(宋体等),否则导出的图片的中文字符不能正常显示
SlideShow ppt = new SlideShow(new HSLFSlideShow("PPT测试.ppt"));
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height
, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
slide[i].draw(graphics);
FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
7)提取PPT文档信息
SlideShow ppt = new SlideShow(new HSLFSlideShow("PPT测试.ppt"));
Slide[] slides = ppt.getSlides();
//提取文本信息
for (Slide each : slides) {
System.out.println(each.getTitle()) ;
TextRun[] textRuns = each.getTextRuns();
for (int i=0 ;i< textRuns.length; i++ ) {
System.out.println(textRuns[i].getText());
RichTextRun[] richTextRuns = textRuns[i].getRichTextRuns();
for (int j = 0; j < richTextRuns.length; j++) {
System.out.println(richTextRuns[j].getText());
}
}
}
//提取所有JPEG图片
PictureData[] picDatas = ppt.getPictureData();
for (int i=0;i<picDatas.length;i++) {
if(picDatas[i].getType() == Picture.JPEG){
FileOutputStream out = new FileOutputStream("jpg_" + i + ".jpg");
ppt.write(out);
out.close();
}
}
8)设置PPT文档摘要信息(文档点击鼠标右键查看属性)
HSLFSlideShow hslf = HSLFSlideShow.create();
DocumentSummaryInformation dsi= hslf.getDocumentSummaryInformation();
SummaryInformation si= hslf.getSummaryInformation();
dsi.setCompany("yourCompany");
dsi.setCategory("ppt测试");
si.setAuthor("yourName");
si.setTitle("标题");
SlideShow ppt = new SlideShow(hslf);
savePPTFile(ppt);
[size=large][color=red]Java 利用POI生成PPT[/color][/size] [url]http://282912533.iteye.com/blog/566798[/url]
AVA生成处理PPT文件, 在GOOGLE了一番没找到示例 , 自己下了POI包 , 看完自带的examples , 发现离实际操作上还有许多地方没懂 , 花了点时间研究一番,并贴上代码 , 希望给有相同需求的人有所帮助 . 当然 , 也相信在未来各类报表工具诸如ireport等会出新版本解决 Java 生成 PPT的问题 .
下面是几个简单的示例:
一, 解析PPT文件中的图片
package test.ppt;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.SlideShow;
/**
* 运行程序, 所在工程需要导入 POI3.6 开发包
* @author 杜磊米
*
*/
public class OutputPicture {
// 图片默认存放路径
public final static String path = "F:\\ppt\\";
public static void main(String[] args) throws Exception {
// 加载PPT
HSLFSlideShow _hslf = new HSLFSlideShow("F:\\Downloads\\myPPT.ppt");
SlideShow _slideShow = new SlideShow(_hslf);
// 获取PPT文件中的图片数据
PictureData[] _pictures = _slideShow.getPictureData();
// 循环读取图片数据
for (int i = 0; i < _pictures.length; i++) {
StringBuilder fileName = new StringBuilder(path);
PictureData pic_data = _pictures[i];
fileName.append(i);
// 设置格式
switch (pic_data.getType()) {
case Picture.JPEG:
fileName.append(".jpg");
break;
case Picture.PNG:
fileName.append(".png");
break;
default:
fileName.append(".data");
}
// 输出文件
FileOutputStream fileOut = new FileOutputStream(new File(fileName.toString()));
fileOut.write(pic_data.getData());
fileOut.close();
}
}
}
二 , 在PPT文件中加入外部图片
package test.ppt;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
/**
* 将图片置入PPT文件中
* @author 杜磊米
*/
public class InputPicture {
public static String path = "F:\\images\\myImage.png";
public static String OUTPUT = "F:\\ppt\\myppt.ppt";
public static void main(String[] args) throws Exception {
if(args.length != 0){
path = args[0];
}
// 构建PPT
SlideShow _slideShow = new SlideShow();
// 创建幻灯片
Slide _slide = _slideShow.createSlide();
// 设置图片类型
int pic_type = -1;
if(path.indexOf(".png") != -1){
pic_type = Picture.PNG;
}else{
pic_type = Picture.JPEG;
}
File file = new File(path);
BufferedImage image = ImageIO.read(file);
// 新置入图片索引位置
int newIndex = _slideShow.addPicture(file, pic_type);
// 根据索引位置 , 创建图片对象
Picture _picture = new Picture(newIndex);
// 设置图片显示位置
_picture.setAnchor(new Rectangle(100,100,image.getWidth(),image.getHeight()));
// 将图片放入幻灯片
_slide.addShape(_picture);
// 输出PPT文件
_slideShow.write(new FileOutputStream(new File(OUTPUT)));
}
}
三 , 操作文本对象
package test.ppt;
import java.awt.Color;
import java.awt.Rectangle;
import java.io.FileOutputStream;
import org.apache.poi.hslf.model.AutoShape;
import org.apache.poi.hslf.model.Line;
import org.apache.poi.hslf.model.ShapeTypes;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
/**
* 操作 Text 文本
* @author 杜磊米
*/
public class InputTextRun {
public static void main(String[] args) throws Exception{
SlideShow _slideShow = new SlideShow();
Slide slide = _slideShow.createSlide();
// 创建并置入简单文本
TextBox _text = new TextBox();
TextRun _textRun = _text.createTextRun();
_textRun.setRawText("杜磊米");
_text.setAnchor(new Rectangle(10,10,100,100));
// 创建并置入带有样式的文本
AutoShape _autoShape = new AutoShape(ShapeTypes.Rectangle); //设置形状
TextRun _autoText = _autoShape.createTextRun();
_autoText.setRawText("杜磊米");
_autoShape.setAnchor(new Rectangle(200,200,100,100));
_autoShape.setFillColor(new Color(170,215,255));
_autoShape.setLineWidth(5.0);
_autoShape.setLineStyle(Line.LINE_DOUBLE);
// AutoShape 对象可以设置多个不同样式文本
TextRun _autoText2 = _autoShape.createTextRun();
RichTextRun _richText = _autoText2.appendText("杜");
_richText.setFontColor(new Color(255,255,255));
RichTextRun _richText2 = _autoText2.appendText("磊米");
_richText2.setFontColor(new Color(255,0,0));
_richText2.setFontSize(12);
// 将文本对象置入幻灯片
slide.addShape(_text);
slide.addShape(_autoShape);
// 输出文件
_slideShow.write(new FileOutputStream("F:\\ppt\\text.ppt"));
}
}
四 , 设置各类文件属性
package test.ppt;标签:Java,hslf,示例,import,POI,SlideShow,PPT,ppt,new From: https://blog.51cto.com/u_3871599/6138547
import java.awt.Dimension;
import java.io.FileOutputStream;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
/**
* 设置PPT属性
* @author 杜磊米
*
*/
public class PPTProperty {
public static void main(String [] args)throws Exception{
HSLFSlideShow hslf = HSLFSlideShow.create();
SlideShow _slideShow = new SlideShow(hslf);
// 设置页面大小
_slideShow.setPageSize(new Dimension(400,600));
// 设置后创建出相应大小的幻灯片
Slide slide = _slideShow.createSlide();
DocumentSummaryInformation doc = hslf.getDocumentSummaryInformation();
SummaryInformation info = hslf.getSummaryInformation();
doc.setCompany("secret");
info.setAuthor("杜磊米");
info.setTitle("nothing");
// 输出文件
_slideShow.write(new FileOutputStream("F:\\ppt\\demo.ppt"));
// 完成后, 找到文件 , 右键属性查看:)
}
}