java通过poi操作ppt
Java POI导出ppt简单实现
Java使用poi组件导出ppt报表幻灯片,poi导出pptx表格可以合并单元格,输出老版本的ppt不支持合并单元格,
下面介绍poi导出pptx的一些常用功能, 采用的是poi-3.8-20120326.jar,poi-ooxml-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar。
创建幻灯片
poi输出pptx首先需要创建幻灯片,可以创建多个幻灯片,然后幻灯片中可以加入表格、图片、文本等元素,如下通过ppt.createSlide()创建一个幻灯片,幻灯片中加入TextBox文本,需要指定TextBox坐标位置,长和宽可以设置为0,自动适应文本大小,
如果不通过setAnchor()方法指定坐标,则幻灯片中不会显示该文本元素。

|
1
2
3
4
5
6
7
|
XMLSlideShow ppt = new XMLSlideShow();XSLFSlide slide = ppt.createSlide();//创建幻灯片XSLFTextBox textBox = slide.createTextBox();textBox.setAnchor(new Rectangle2D.Double(10,10, 0, 0));textBox.addNewTextParagraph().addNewTextRun().setText("创建幻灯片");ppt.write(new FileOutputStream("/Users/mike/ppt1.pptx")); |
幻灯片插入表格
幻灯片插入表格通过slide.createTable()方法创建表格,同样table需要指定坐标位置,幻灯片的元素都需要指定坐标位置。

|
1
2
3
4
5
6
7
8
9
10
11
12
|
Object[][] datas = {{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045, 2256}, {"广东省", 3000, 690}};XSLFTable table = slide.createTable();//创建表格table.setAnchor(new Rectangle2D.Double(10, 50, 0, 0));for(int i = 0; i < datas.length; i++) { XSLFTableRow tableRow = table.addRow(); //创建表格行 for(int j = 0; j < datas[i].length; j++) { XSLFTableCell tableCell = tableRow.addCell();//创建表格单元格 XSLFTextParagraph p = tableCell.addNewTextParagraph(); XSLFTextRun tr = p.addNewTextRun(); tr.setText(String.valueOf(datas[i][j])); }} |
设置表格样式
单元格可以设置居左、居中、居右、上下居中、设置边框、设置边框颜色、设置单元格背景颜色, 设置文本居中是使用XSLFTextParagraph p 段落对象设置居中。

|
1
2
3
4
5
6
7
8
9
10
11
12
|
tableCell.setFillColor(Color.getColor("0xdd7e6b"));p.setTextAlign(TextAlign.CENTER);tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);tableCell.setBorderBottom(1);tableCell.setBorderLeft(1);tableCell.setBorderTop(1);tableCell.setBorderRight(1);tableCell.setBorderBottomColor(Color.BLACK);tableCell.setBorderLeftColor(Color.BLACK);tableCell.setBorderTopColor(Color.BLACK);tableCell.setBorderRightColor(Color.BLACK); |
表格设置行高、列宽
有时幻灯片中表格文本比较多,需要设置表格的列宽度,在创建每行时设置高度,在创建表格之后设置表格每列宽度

|
1
2
3
4
|
tableRow.setHeight(30);table.setColumnWidth(0, 150);table.setColumnWidth(1, 150);table.setColumnWidth(2, 250); |
文本设置字体样式
单元格文本可设置字体大小、颜色、斜体、粗体、下划线等, 设置字体样式时通过XSLFTextRun tr 对象设置。

|
1
2
3
4
5
6
|
tr.setFontSize(16);tr.setBold(true);tr.setItalic(true);tr.setUnderline(true);tr.setFontFamily("\u5b8b\u4f53");tr.setFontColor(Color.RED); |
合并单元格
合并单元格需要在表格创建完之后(行与列全部创建完之后),mergeCells()方法有四个参数,第一个参数:开始行,第二个参数:合并结束行,第三个参数:开始列,第四个参数:合并结束列。

|
1
2
|
//合并单元格table.mergeCells(0, 0, 0, 2); |
幻灯片插入图片
幻灯片中插入图片首选在ppt对象中加入图片生成一个idx图片对应下标值,幻灯片对象slide创建图片传人下标值, 设置图片在幻灯片中的绝对位置,图片元素必须设置大小,否则不显示。

|
1
2
3
4
5
|
//插入图片byte[] bt = FileUtils.readFileToByteArray(new File("/Users/mike/pie.png"));int idx = ppt.addPicture(bt, XSLFPictureData.PICTURE_TYPE_PNG);XSLFPictureShape pic = slide.createPicture(idx);pic.setAnchor(new Rectangle(10, 200, 339, 197)); |
创建一个链接
文本链接通过XSLFTextRun对象的createHyperlink()方法创建

|
1
2
3
4
5
6
7
|
//创建一个文本链接XSLFTextBox linkText = slide.createTextBox();linkText.setAnchor(new Rectangle2D.Double(430, 310, 0, 0));XSLFTextRun r = linkText.addNewTextParagraph().addNewTextRun();r.setText("Apache POI");XSLFHyperlink link = r.createHyperlink(); |
完整例子
poi导出pptx例子源码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
import java.awt.Color;import java.awt.geom.Rectangle2D;import java.io.File;import java.io.FileOutputStream;import org.apache.commons.io.FileUtils;import org.apache.poi.xslf.usermodel.TextAlign;import org.apache.poi.xslf.usermodel.VerticalAlignment;import org.apache.poi.xslf.usermodel.XMLSlideShow;import org.apache.poi.xslf.usermodel.XSLFHyperlink;import org.apache.poi.xslf.usermodel.XSLFPictureData;import org.apache.poi.xslf.usermodel.XSLFPictureShape;import org.apache.poi.xslf.usermodel.XSLFSlide;import org.apache.poi.xslf.usermodel.XSLFTable;import org.apache.poi.xslf.usermodel.XSLFTableCell;import org.apache.poi.xslf.usermodel.XSLFTableRow;import org.apache.poi.xslf.usermodel.XSLFTextBox;import org.apache.poi.xslf.usermodel.XSLFTextParagraph;import org.apache.poi.xslf.usermodel.XSLFTextRun;public class TestExportPptx { public static void main(String[] args) throws Exception { XMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide();//创建幻灯片 XSLFTextBox textBox = slide.createTextBox(); textBox.setAnchor(new Rectangle2D.Double(10,10, 0, 0)); textBox.addNewTextParagraph().addNewTextRun().setText("创建幻灯片"); Object[][] datas = {{"区域产品销售额","",""},{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045, 2256}, {"广东省", 3000, 690}}; XSLFTable table = slide.createTable();//创建表格 table.setAnchor(new Rectangle2D.Double(10, 50, 0, 0)); for(int i = 0; i < datas.length; i++) { XSLFTableRow tableRow = table.addRow(); //创建表格行 for(int j = 0; j < datas[i].length; j++) { XSLFTableCell tableCell = tableRow.addCell();//创建表格单元格 XSLFTextParagraph p = tableCell.addNewTextParagraph(); XSLFTextRun tr = p.addNewTextRun(); tr.setText(String.valueOf(datas[i][j])); tableCell.setFillColor(Color.getColor("0xdd7e6b")); p.setTextAlign(TextAlign.CENTER); tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE); if(i == datas.length - 1 && j == 3-1) { tr.setFontSize(16); tr.setBold(true); tr.setItalic(true); tr.setUnderline(true); tr.setFontFamily("\u5b8b\u4f53"); tr.setFontColor(Color.RED); } tableCell.setBorderBottom(1); tableCell.setBorderLeft(1); tableCell.setBorderTop(1); tableCell.setBorderRight(1); tableCell.setBorderBottomColor(Color.BLACK); tableCell.setBorderLeftColor(Color.BLACK); tableCell.setBorderTopColor(Color.BLACK); tableCell.setBorderRightColor(Color.BLACK); } tableRow.setHeight(30); } //设置列宽 table.setColumnWidth(0, 150); table.setColumnWidth(1, 150); table.setColumnWidth(2, 250); //合并单元格 table.mergeCells(0, 0, 0, 2); //插入图片 byte[] bt = FileUtils.readFileToByteArray(new File("/Users/mike/pie.png")); int idx = ppt.addPicture(bt, XSLFPictureData.PICTURE_TYPE_PNG); XSLFPictureShape pic = slide.createPicture(idx); pic.setAnchor(new Rectangle2D.Double(10, 200, 339, 197)); //创建一个文本链接 XSLFTextBox linkText = slide.createTextBox(); linkText.setAnchor(new Rectangle2D.Double(430, 310, 0, 0)); XSLFTextRun r = linkText.addNewTextParagraph().addNewTextRun(); r.setText("Apache POI"); XSLFHyperlink link = r.createHyperlink(); ppt.write(new FileOutputStream("/Users/mike/ppt8.pptx")); }} |
浙公网安备 33010602011771号