POI 创建PPT小记

通用ppt创建步骤

       //创建一份ppt
       XMLSlideShow ppt=new XMLSlideShow();
		//创建第一张幻灯片
		XSLFSlide slide1 = ppt.createSlide();
		//标题文本域
		XSLFTextBox textBox1 = slide1.createTextBox();
		//设置xy轴宽高
		textBox1.setAnchor(new Rectangle2D.Double(50,150, 650, 0));
		//创建文本
		XSLFTextRun run1=textBox1.addNewTextParagraph().addNewTextRun();
		//字体风格
		run1.setFontFamily("宋体");
		//加粗
		run1.setBold(true);
		//字号
		run1.setFontSize((double) 44);
		//颜色
		run1.setFontColor(Color.BLACK);
		//文本信息
		run1.setText("这是一份PPT"); 
		//如果不改文本样式可以直接 textBox1.setText("这是一份PPT")

		//注意:当文本信息里有转译字符时,如\n,先setText再设置样式时,样式可能会被影响,所以一般先设样式,再setText

PPT表格创建步骤

	//表格标题,单级表头只要一维数组即可,多级表头需要多维数组,下面用二级表头
	String[][] titles={ {"学号","名称","年龄","班级","学科成绩","",""} ,
				{"","","","","语文","数学","英语"} };  //注:数组长度需要一样,空的元素是为了表格合并

	XSLFTable table=slide4.createTable();
		table.setAnchor(new Rectangle2D.Double(30,150, 0, 0));
		//嵌套循环添加表头
		for(int j=0;j<titles.length;j++) {
		//创建行
			XSLFTableRow row=table.addRow();
			for(int m=0;m<titles[j].length;m++) {
			//行里创建单元格
				XSLFTableCell cell=row.addCell();
				
				//背景色
				cell.setFillColor(new Color(222, 231, 209));
				//垂直居中
				cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
				//水平居中
				cell.addNewTextParagraph().setTextAlign(TextAlign.CENTER);
				//表格线
				cell.setBorderColor(BorderEdge.bottom, new Color(155, 187, 89));
				cell.setBorderColor(BorderEdge.left, new Color(155, 187, 89));
				cell.setBorderColor(BorderEdge.right, new Color(155, 187, 89));
				cell.setBorderColor(BorderEdge.top, new Color(155, 187, 89));
				//文本信息、大小,其他样式参考上面的
				cellRun.setFontFamily("宋体");
				cellRun.setFontSize((double) 12);
				//添加表头
				XSLFTextRun cellRun=cell.setText(titles[j][m]);
			}
		}
	//表头创建完就是添加数据了,与添加表头一般,数据我是提前处理好的List<List<String>>类型的,应该会有更好的方法
	for(int x=0;x<list.size();x++) {
			//创建行
			XSLFTableRow row1=table.addRow();
			for(int y=0;y<list.get(x).size();y++) {
			//创建单元格
				XSLFTableCell cell=row1.addCell();
				//单双行颜色区分
				if(x%2>0) {
					cell.setFillColor(new Color(208, 216, 232));
				}else {
					cell.setFillColor(new Color(233, 237, 244));
				}
				//垂直水平居中
				cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
				cell.addNewTextParagraph().setTextAlign(TextAlign.CENTER);
				//边框
				cell.setBorderColor(BorderEdge.bottom, new Color(155, 187, 89));
				cell.setBorderColor(BorderEdge.left, new Color(155, 187, 89));
				cell.setBorderColor(BorderEdge.right, new Color(155, 187, 89));
				cell.setBorderColor(BorderEdge.top, new Color(155, 187, 89));
				//添加文本
				XSLFTextRun cellRun=cell.addNewTextParagraph().addNewTextRun();
				//文本信息
				cellRun.setFontFamily("宋体");
				cellRun.setFontSize((double) 12);
				cellRun.setText(list.get(x).get(y));
			}
		}
		//二级表头合并单元格
		table.mergeCells(0, 1, 0, 0);//第一(0)行与第二(1)行的第一(0)列合并
		table.mergeCells(0, 1, 1, 1);
		table.mergeCells(0, 1, 2, 2);
		table.mergeCells(0, 1, 3, 3);
		table.mergeCells(0, 0, 4, 6);//第一(0)行的第五(4)列合并与第七(6)列
		//设置列宽,这里的列是还没合并时的列
		table.setColumnWidth(0, 150);
		table.setColumnWidth(1, 150);
		table.setColumnWidth(2, 150);
		table.setColumnWidth(3, 150);
		table.setColumnWidth(4, 150);
		table.setColumnWidth(5, 150);
		table.setColumnWidth(6, 150);
	

PPT背景设置

		byte[] byteArray= IOUtils.toByteArray(new FileInputStream("文件地址.jpg"));
		XSLFPictureData pictureData=ppt.addPicture(byteArray, PictureType.JPEG);
		XSLFPictureShape shape=slide.createPicture(pictureData);
		shape.setAnchor(new Rectangle2D.Double(0, 0, ppt.getPageSize().width, ppt.getPageSize().height));

若有误,还请评论指出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值