java poi 操作ppt,该怎么解决
- 提问者网友:最爱你的唇
- 2021-03-10 19:20
- 五星知识达人网友:白昼之月
- 2021-03-10 20:11
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;
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文件中加入外部图片
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;
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)));
}
}
操作文本对象
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;
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"));
}
}
设置各类文件属性
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;
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"));
// 完成后, 找到文件 , 右键属性查看:)
}
}
- 1楼网友:一袍清酒付
- 2021-03-10 21:11
java是可以用poi操作word。excel这些的 看下面这个能帮上你不
1、环境支持 1.1 添加poi支持:包下载地址 http://www.apache.org/dyn/closer.cgi/poi/release/ 1.2 poi对excel文件的读取操作比较方便,poi还提供对word的doc格式文件的读取。但在它的发行版本中没有发布对word支持的模块,需要另外下载一个poi的扩展的jar包。下载地址为 http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/ 下载extractors-0.4_zip这个文件 package com.ray.poi.util; import java.io.bytearrayinputstream; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import org.apache.poi.poifs.filesystem.directoryentry; import org.apache.poi.poifs.filesystem.documententry; import org.apache.poi.poifs.filesystem.poifsfilesystem; import org.textmining.text.extraction.wordextractor; public class poiwordutil { public static string readdoc(string doc) throws exception { // 创建输入流读取doc文件 fileinputstream in = new fileinputstream(new file(doc)); wordextractor extractor = null; string text = null; // 创建wordextractor extractor = new wordextractor(); // 对doc文件进行提取 text = extractor.extracttext(in); return text; } public static boolean writedoc(string path, string content) { boolean w = false; try { // byte b[] = content.getbytes("iso-8859-1"); byte b[] = content.getbytes(); bytearrayinputstream bais = new bytearrayinputstream(b); poifsfilesystem fs = new poifsfilesystem(); directoryentry directory = fs.getroot(); documententry de = directory.createdocument("worddocument", bais); fileoutputstream ostream = new fileoutputstream(path); fs.writefilesystem(ostream); bais.close(); ostream.close(); } catch (ioexception e) { e.printstacktrace(); } return w; } } 测试 package com.ray.poi.util; import junit.framework.testcase; public class poiutiltest extends testcase { public void testreaddoc() { try{ string text = poiwordutil.readdoc("e:/work_space/poi/com/ray/poi/util/demo.doc"); system.out.println(text); }catch(exception e){ e.printstacktrace(); } } public void testwritedoc() { string wr; try { wr = poiwordutil.readdoc("e:/work_space/poi/com/ray/poi/util/demo.doc"); boolean b = poiwordutil.writedoc("c:\\demo.doc",wr); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } }
- 2楼网友:过活
- 2021-03-10 20:49