1:maven 依赖:
采用的是4.1.2的版本导出
<!--导出文件--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
2:代码源码
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();//创建幻灯片
XSLFTextBox textBox = slide.createTextBox();
textBox.setAnchor(new Rectangle2D.Double(10,10, 200, 30));
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(TextParagraph.TextAlign.CENTER);
tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
if(i == datas.length - 1 && j == 3-1) {
tr.setFontSize(16d);
tr.setBold(true);
tr.setItalic(true);
tr.setUnderlined(true);
tr.setFontFamily("\u5b8b\u4f53");
tr.setFontColor(Color.RED);
}
tableCell.setBorderWidth(TableCell.BorderEdge.left,1);
tableCell.setBorderWidth(TableCell.BorderEdge.right,1);
tableCell.setBorderWidth(TableCell.BorderEdge.top,1);
tableCell.setBorderWidth(TableCell.BorderEdge.bottom,1);
tableCell.setBorderColor(TableCell.BorderEdge.bottom,Color.BLACK);
tableCell.setBorderColor(TableCell.BorderEdge.left,Color.BLACK);
tableCell.setBorderColor(TableCell.BorderEdge.right,Color.BLACK);
tableCell.setBorderColor(TableCell.BorderEdge.top,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("D:\\Users\\ppt.png"));
XSLFPictureData idx = ppt.addPicture(bt, PictureData.PictureType.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, 80, 30));
XSLFTextRun r = linkText.addNewTextParagraph().addNewTextRun();
r.setText("Apache POI");
XSLFHyperlink link = r.createHyperlink();
link.setAddress("http://poi.apache.org");
ppt.write(new FileOutputStream("D:\\Users\\pptTest.pptx"));