/**
* 报告生成ppt
* @author 王明超
* @return void
* @throws IOException
*/
public void exportPpt(HttpServletResponse response, VpReportInfo vpReportInfo, List reportInfoList) throws IOException{
SlideShow ppt = new SlideShow();
//设置母版,这样后续的新建幻灯片都将使用母版的字体,背景等设置
SlideMaster master = ppt.getSlidesMasters()[0];
//Picture
String rootImg = ServletActionContext.getServletContext().getRealPath("resources\\images");
Picture pictpptLine = this.getPicture(ppt, rootImg+"\\pptLine.png", new Rectangle(3, 72, 717, 5));
master.addShape(pictpptLine);
Picture pictpptHead = this.getPicture(ppt, rootImg+"\\pptHead.png", new Rectangle(603, 0, 117, 84));
master.addShape(pictpptHead);
String filename = vpReportInfo.getReportName();
if(reportInfoList!=null && reportInfoList.size()>0) {
for(int i=0;i<reportInfoList.size();i++) {
VpReportPage vo = (VpReportPage)reportInfoList.get(i);
Slide slide = ppt.createSlide();
//TextBox
TextBox txt = this.getTextBox(vo.getTopCaption(), new Rectangle(25, 12, 648, 56), 24, "黑体", Color.black, TextBox.AlignLeft);
slide.addShape(txt);
//TextBox
TextBox txt2 = this.getTextBox(vo.getSubCaption(), new Rectangle(18, 96, 648, 56), 14, "华文中宋", Color.black, TextBox.AlignLeft);
slide.addShape(txt2);
if(!StringUtils.isEmpty(vo.getPicId())) {
String[] picSrc = vo.getPicId().split(",");
String picPath = ServletActionContext.getServletContext().getRealPath("uploadchartPic");
if(picSrc.length>0) {
if(picSrc.length == 1) {//单图
String picName = picSrc[0].substring(picSrc[0].lastIndexOf("/")+1, picSrc[0].length());
BufferedImage image = ImageIO.read(new File(picPath+"\\"+picName));
Picture pict = this.getPicture(ppt, picPath+"\\"+picName, new Rectangle((int)Math.floor(360-0.5*656), 173, 656, (int)Math.floor(656*((double)image.getHeight()/(double)image.getWidth()))));
slide.addShape(pict);
}
// else if(picSrc.length == 2 && "3".equals(vo.getTemplateType())) {//双图(左右排列)
// String picName1 = picSrc[0].substring(picSrc[0].lastIndexOf("/")+1, picSrc[0].length());
// String picName2 = picSrc[1].substring(picSrc[1].lastIndexOf("/")+1, picSrc[1].length());
// Picture pict1 = this.getPicture(ppt, picPath+"\\"+picName1, new Rectangle(50, 250, 300, 200));
// Picture pict2 = this.getPicture(ppt, picPath+"\\"+picName2, new Rectangle(350, 250, 300, 200));
// slide.addShape(pict1);
// slide.addShape(pict2);
// }
// else if(picSrc.length == 2 && "2".equals(vo.getTemplateType())) {//双图(上下排列)
// String picName1 = picSrc[0].substring(picSrc[0].lastIndexOf("/")+1, picSrc[0].length());
// String picName2 = picSrc[1].substring(picSrc[1].lastIndexOf("/")+1, picSrc[1].length());
// Picture pict1 = this.getPicture(ppt, picPath+"\\"+picName1, new Rectangle(250, 152, 200, 194));
// Picture pict2 = this.getPicture(ppt, picPath+"\\"+picName2, new Rectangle(250, 346, 200, 194));
// slide.addShape(pict1);
// slide.addShape(pict2);
// }
else if(picSrc.length >= 2 && ("2".equals(vo.getTemplateType()) || "4".equals(vo.getTemplateType()))) {//双图 多图(上下排列)
for(int j=0;j<picSrc.length;j++) {
String picName = picSrc[j].substring(picSrc[j].lastIndexOf("/")+1, picSrc[j].length());
BufferedImage image = ImageIO.read(new File(picPath+"\\"+picName));
Picture pict = this.getPicture(ppt, picPath+"\\"+picName, new Rectangle((int)Math.floor(360-((388/picSrc.length)*((double)image.getWidth()/(double)image.getHeight()))*0.5), (int)Math.floor(152+(388/picSrc.length)*j), (int)Math.floor((388/picSrc.length)*((double)image.getWidth()/(double)image.getHeight())), (int)Math.floor(388/picSrc.length)));
slide.addShape(pict);
}
}
else if(picSrc.length >= 2 && ("3".equals(vo.getTemplateType()) || "5".equals(vo.getTemplateType()))) {//双图 多图(左右排列)
for(int j=0;j<picSrc.length;j++) {
String picName = picSrc[j].substring(picSrc[j].lastIndexOf("/")+1, picSrc[j].length());
BufferedImage image = ImageIO.read(new File(picPath+"\\"+picName));
Picture pict = this.getPicture(ppt, picPath+"\\"+picName, new Rectangle((int)Math.floor((720/picSrc.length)*j), (int)Math.floor(152+194-((720/picSrc.length)*((double)image.getHeight()/(double)image.getWidth()))*0.5), (int)Math.floor(720/picSrc.length), (int)Math.floor((720/picSrc.length)*((double)image.getHeight()/(double)image.getWidth()))));
slide.addShape(pict);
}
}
}
}
}
}
Slide slideLast = ppt.createSlide();
//TextBox
TextBox txtLast = this.getTextBox("谢谢!", new Rectangle(264, 186, 200, 100), 48, "华文行楷", Color.red, TextBox.AlignCenter);
slideLast.addShape(txtLast);
// ppt.write(new FileOutputStream(new File("e:\\ppt\\wmc.ppt")));
// 向IE发送数据流
response.reset();
response.setContentType("application/vnd.ms-powerpoint"); // mspowerpoint
java.net.URLEncoder.encode(filename, "UTF-8");
String fname = new String(filename.getBytes("gb2312"), "iso-8859-1");
response.setHeader("Content-Disposition", "attachment;Filename=" + fname + ".ppt");
OutputStream os;
try {
os = response.getOutputStream();// 获取输出流
ppt.write(os);// 输出
os.flush();
os.close();// 关闭流
} catch (IOException e) {
System.out.println("导ppt异常" + e.getMessage());
e.printStackTrace();
}
}
public TextBox getTextBox(String text,Rectangle rec,int fontSize,String fontName,Color color,int align){
//TextBox
TextBox txt = new TextBox();
txt.setText(text);
if(rec != null) {
txt.setAnchor(rec);
}
//use RichTextRun to work with the text format
RichTextRun rt = txt.getTextRun().getRichTextRuns()[0];
if(fontSize != 0) {
rt.setFontSize(fontSize);
}
if(fontName != null) {
rt.setFontName(fontName);
}
if(color != null) {
rt.setFontColor(color);
}
if(align != 0) {
rt.setAlignment(align);
}
return txt;
}
public Picture getPicture(SlideShow ppt, String picPath, Rectangle rec) {
File file = new File(picPath);
Picture pict = null;
if(!file.exists()) {
System.out.println("文件:"+picPath+"不存在!");
}
try {
int idx = ppt.addPicture(file, Picture.PNG);
pict = new Picture(idx);
} catch (IOException e) {
e.printStackTrace();
}
if(pict != null && rec != null) {
pict.setAnchor(rec);
}
return pict;
}
ppt
最新推荐文章于 2024-05-23 16:27:57 发布