java poi合并多个ppt完成ppt或者pptx转图片操作,windows环境正常,linux环境报错。

他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Java POI导出ppt简单实现
Java使用poi组件导出ppt报表幻灯片,poi导出pptx表格可以合并单元格,输出老版本的ppt不支持合并单元格,下面介绍poi导出pptx的一些常用功能, 采用的是poi-3.8-.jar,poi-ooxml-3.8-.jar,poi-scratchpad-3.8-.jar。
poi输出pptx首先需要创建幻灯片,可以创建多个幻灯片,然后幻灯片中可以加入表格、图片、文本等元素,如下通过ppt.createSlide()创建一个幻灯片,幻灯片中加入TextBox文本,需要指定TextBox坐标位置,长和宽可以设置为0,自动适应文本大小,&如果不通过setAnchor()方法指定坐标,则幻灯片中不会显示该文本元素。
&&&&&&&&&&
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需要指定坐标位置,幻灯片的元素都需要指定坐标位置。
&&&&&&&&&&
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. i++) {
&&&XSLFTableRow tableRow = table.addRow();
&&&for(int j = 0; j & datas[i]. j++) {
&&&&&&&XSLFTableCell tableCell = tableRow.addCell();
&&&&&&&XSLFTextParagraph p = tableCell.addNewTextParagraph();
&&&&&&&XSLFTextRun tr = p.addNewTextRun();
&&&&&&&tr.setText(String.valueOf(datas[i][j]));
单元格可以设置居左、居中、居右、上下居中、设置边框、设置边框颜色、设置单元格背景颜色, 设置文本居中是使用XSLFTextParagraph p 段落对象设置居中。
&&&&&&&&&&
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);
有时幻灯片中表格文本比较多,需要设置表格的列宽度,在创建每行时设置高度,在创建表格之后设置表格每列宽度
&&&&&&&&&&
tableRow.setHeight(30);
table.setColumnWidth(0, 150);
table.setColumnWidth(1, 150);
table.setColumnWidth(2, 250);
单元格文本可设置字体大小、颜色、斜体、粗体、下划线等, 设置字体样式时通过XSLFTextRun tr 对象设置。
&&&&&&&&&&
tr.setFontSize(16);
tr.setBold(true);
tr.setItalic(true);
tr.setUnderline(true);
tr.setFontFamily("\u5b8b\u4f53");
tr.setFontColor(Color.RED);
合并单元格需要在表格创建完之后(行与列全部创建完之后),mergeCells()方法有四个参数,第一个参数:开始行,第二个参数:合并结束行,第三个参数:开始列,第四个参数:合并结束列。
&&&&&&&&&&
table.mergeCells(0, 0, 0, 2);
幻灯片中插入图片首选在ppt对象中加入图片生成一个idx图片对应下标值,幻灯片对象slide创建图片传人下标值, 设置图片在幻灯片中的绝对位置,图片元素必须设置大小,否则不显示。
&&&&&&&&&&
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()方法创建
&&&&&&&&&&
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();
link.setAddress("");
poi导出pptx例子源码
import java.awt.C
import java.awt.geom.Rectangle2D;
import java.io.F
import java.io.FileOutputS
import org.apache.commons.io.FileU
import org.apache.poi.xslf.usermodel.TextA
import org.apache.poi.xslf.usermodel.VerticalA
import org.apache.poi.xslf.usermodel.XMLSlideS
import org.apache.poi.xslf.usermodel.XSLFH
import org.apache.poi.xslf.usermodel.XSLFPictureD
import org.apache.poi.xslf.usermodel.XSLFPictureS
import org.apache.poi.xslf.usermodel.XSLFS
import org.apache.poi.xslf.usermodel.XSLFT
import org.apache.poi.xslf.usermodel.XSLFTableC
import org.apache.poi.xslf.usermodel.XSLFTableR
import org.apache.poi.xslf.usermodel.XSLFTextB
import org.apache.poi.xslf.usermodel.XSLFTextP
import org.apache.poi.xslf.usermodel.XSLFTextR
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. i++) {
&&&&&&&&&&&XSLFTableRow tableRow = table.addRow();
&&&&&&&&&&&for(int j = 0; j & datas[i]. 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();
&&&&&&&link.setAddress("");
&&&&&&&&&&&&&&&&
&&&&&&&&ppt.write(new FileOutputStream("/Users/mike/ppt8.pptx"));
阅读(...) 评论()豆丁微信公众号
君,已阅读到文档的结尾了呢~~
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
应用POI实现将PPT文档转换为图片
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='http://www.docin.com/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 java poi ppt 的文章

 

随机推荐