java使用POI導出PPT(超簡單方法,包含圖片和表格)
在做項目中遇到一個需求,將職員的信息導出成一個形式固定的ppt文檔,poi有許多方法可以實現,因為我是一名Java小白,于是便想用最簡單的方法來實現,代碼如下:
該需求需要用到poi的jar包:
https://download.csdn.net/download/weixin_42142057/10652136
public static void main(String[] args) throws IOException {//創建PPTSlideShow ppt = new SlideShow()//設置ppt母版,也就是背景圖片。ppt
.setPageSize(new Dimension(
760,
600))SlideMaster master = ppt
.getSlidesMasters()[
0]int picIndex =
0try {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))//將文本框加入到pptnewSlide
.addShape(orgTxt)//個人照片int pic =
0try {//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 =
0for (int j =
0 //獲取單元格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()int
x =
0while(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)//導出pptFile outFile = new File(
"c:/demo.ppt")FileOutputStream
out = new FileOutputStream(outFile)ppt
.write(
out)
out.close()}
小伙伴們,趕緊試試吧,祝你一次成功!
總結
以上是生活随笔為你收集整理的java中使用poi导出ppt(图片和表格)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。