java使用POI导出PPT(超简单方法,包含图片和表格)
在做项目中遇到一个需求,将职员的信息导出成一个形式固定的ppt文档,poi有许多方法可以实现,因为我是一名Java小白,于是便想用最简单的方法来实现,代码如下:
该需求需要用到poi的jar包:
https://download.csdn.net/download/weixin_42142057/10652136
public static void main(String[] args) throws IOException {
//创建PPT
SlideShow ppt = new SlideShow();
//设置ppt母版,也就是背景图片。
ppt.setPageSize(new Dimension(760,600));
SlideMaster master = ppt.getSlidesMasters()[0];
int picIndex =0;
try {
picIndex = ppt.addPicture(new File("c:/text.png"), Picture.PNG);
} catch (IOException e) {
e.printStackTrace();
throw new InfomationException("加载ppt背景图片失败"+e);
}
Picture background = new Picture(picIndex);
background.setAnchor(new Rectangle(0,0, ppt.getPageSize().width, ppt.getPageSize().height));
master.addShape(background);
//创建ppt的一张幻灯片
Slide newSlide = ppt.createSlide();
//职员所属单位
//创建一个文本框
TextBox orgTxt = new TextBox();
RichTextRun orgRichTextRun = orgTxt.getTextRun().getRichTextRuns()[0];
//赋值
orgRichTextRun.setText("ssdw");
//设置文本框在ppt中的位置
orgTxt.setAnchor(new Rectangle(90,30,400,30));
//将文本框加入到ppt
newSlide.addShape(orgTxt);
//个人照片
int pic =0;
try {
//bytes是图片的二进制码
pic = ppt.addPicture(bytes, Picture.PNG);
} catch (IOException e1) {
e1.printStackTrace();
throw new InfomationException("加载员工照片失败"+e1);
}
Picture staffPicture = new Picture(pic);
staffPicture.setAnchor(new Rectangle(60,80,180,180));
newSlide.addShape(staffPicture);
//个人信息框
TextBox personTxt = new TextBox();
RichTextRun personRichTextRun = personTxt.getTextRun().getRichTextRuns()[0];
personRichTextRun.setText(" "+personInfo);
personTxt.setAnchor(new Rectangle(20,280,300,180));
newSlide.addShape(personTxt);
//家庭情况-表格
//创建一个表格,并设置其行数和列数,我这里行数是随着不同职员的数据大小而变化的
Table familytable = new Table(familyInfos.size(),3);
//遍历行与列
for (int i =0; i < familyInfos.size(); i++) {
for (int j =0; j < 3; j++) {
//获取单元格
TableCell cell = familytable.getCell(i, j);
//设置单元格的行高
familytable.setRowHeight(i,10);
if (j ==0) {
//设置单元格的宽度
familytable.setColumnWidth(j,55);
//给单元格里赋值
cell.setText(familyInfos.get(i).getYbrgx()+":");
}else if (j ==1) {
familytable.setColumnWidth(j,70);
cell.setText(familyInfos.get(i).getCyxm());
}else {
familytable.setColumnWidth(j,180);
cell.setText(familyInfos.get(i).getDwjzw());
}
}
}
newSlide.addShape(familytable);
//设置表格在ppt中的位置
familytable.moveTo(20,480);
TextBox familyTxt = new TextBox();
RichTextRun familyRichTextRun = familyTxt.getTextRun().getRichTextRuns()[0];
familyRichTextRun.setText("家庭情况:");
familyTxt.setAnchor(new Rectangle(20,460,300,40));
newSlide.addShape(familyTxt);
//工作经历标题
TextBox workExpTitle = new TextBox();
RichTextRun workExpRichRunTitle = workExpTitle.getTextRun().getRichTextRuns()[0];
workExpRichRunTitle.setText(stdName+"简历");
workExpRichRunTitle.setFontSize(24);
workExpTitle.setAnchor(new Rectangle(450,70,200,40));
newSlide.addShape(workExpTitle);
//工作经历
Table positionTable = new Table(maps.size(),2);
Iterator<Entry<String, String>> it = maps.entrySet().iterator();
intx =0;
while(it.hasNext()) {
Map.Entry<String, String> entity = (Entry<String, String>) it.next();
TableCell cell1 = positionTable.getCell(x,0);
cell1.setText(entity.getKey());
TableCell cell2 = positionTable.getCell(x,1);
cell2.setText(entity.getValue() );
positionTable.setRowHeight(x,10);
positionTable.setColumnWidth(0,160);
positionTable.setColumnWidth(1,250);x++;
}
newSlide.addShape(positionTable);
positionTable.moveTo(330,110);
//导出ppt
File outFile = new File("c:/demo.ppt");
FileOutputStreamout = new FileOutputStream(outFile);
ppt.write(out);out.close();
}
小伙伴们,赶紧试试吧,祝你一次成功!