poi操作ppt创建表格
2016-06-13 18:51
417 查看
POI版本3.14。
创建powerpoint,一页ppt,里面插入两个表格。
创建powerpoint,一页ppt,里面插入两个表格。
/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.hslf.examples; import java.awt.Color; import java.io.FileOutputStream; import org.apache.poi.hslf.usermodel.HSLFSlide; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.hslf.usermodel.HSLFTable; import org.apache.poi.hslf.usermodel.HSLFTableCell; import org.apache.poi.hslf.usermodel.HSLFTextRun; import org.apache.poi.sl.draw.DrawTableShape; import org.apache.poi.sl.usermodel.TextParagraph.TextAlign; import org.apache.poi.sl.usermodel.VerticalAlignment; /** * Demonstrates how to create tables * * @author Yegor Kozlov */ public final class TableDemo { public static void main(String[] args) throws Exception { //test data for the first taable String[][] txt1 = { {"INPUT FILE", "NUMBER OF RECORDS"}, {"Item File", "11,559"}, {"Vendor File", "502"}, {"Purchase History File - # of PO\u2019s\r(12/01/04 - 05/31/06)", "12,852"}, {"Purchase History File - # of PO Lines\r(12/01/04 - 05/31/06)", "53,523" }, {"Total PO History Spend", "$10,172,038"} }; //构造ppt对象 HSLFSlideShow ppt = new HSLFSlideShow(); //构造一页ppt HSLFSlide slide = ppt.createSlide(); //构造一个6行2列的表 HSLFTable table1 = slide.createTable(6, 2); for (int i = 0; i < txt1.length; i++) { for (int j = 0; j < txt1[i].length; j++) { //获取表的单元格 HSLFTableCell cell = table1.getCell(i, j); //单元格的文本域 HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0); //设置样式 rt.setFontFamily("Arial"); rt.setFontSize(10d); if(i == 0){ cell.getFill().setForegroundColor(new Color(227, 227, 227)); } else { rt.setBold(true); } //设置格子垂直居中 cell.setVerticalAlignment(VerticalAlignment.MIDDLE); //设置格子水平居中 cell.setHorizontalCentered(true); //设置格子文本 cell.setText(txt1[i][j]); } } DrawTableShape dts1 = new DrawTableShape(table1); dts1.setAllBorders(1.0, Color.black); //设置第一列宽度 table1.setColumnWidth(0, 300); //设置第二列宽度 table1.setColumnWidth(1, 150); //页面宽度 int pgWidth = ppt.getPageSize().width; //移动位置 table1.moveTo((pgWidth - table1.getAnchor().getWidth())/2., 100.); //test data for the second taable String[][] txt2 = { {"Data Source"}, {"CAS Internal Metrics - Item Master Summary\r" + "CAS Internal Metrics - Vendor Summary\r" + "CAS Internal Metrics - PO History Summary"} }; //two rows, one column HSLFTable table2 = slide.createTable(2, 1); for (int i = 0; i < txt2.length; i++) { for (int j = 0; j < txt2[i].length; j++) { HSLFTableCell cell = table2.getCell(i, j); HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0); rt.setFontSize(10d); rt.setFontFamily("Arial"); if(i == 0){ cell.getFill().setForegroundColor(new Color(0, 51, 102)); rt.setFontColor(Color.white); rt.setBold(true); rt.setFontSize(14d); cell.setHorizontalCentered(true); } else { rt.getTextParagraph().setBullet(true); rt.setFontSize(12d); rt.getTextParagraph().setTextAlign(TextAlign.LEFT); cell.setHorizontalCentered(false); } cell.setVerticalAlignment(VerticalAlignment.MIDDLE); cell.setText(txt2[i][j]); } } table2.setColumnWidth(0, 300); table2.setRowHeight(0, 30); table2.setRowHeight(1, 70); DrawTableShape dts2 = new DrawTableShape(table2); dts2.setOutsideBorders(Color.black, 1.0); table2.moveTo(200, 400); FileOutputStream out = new FileOutputStream("hslf-table.ppt"); ppt.write(out); out.close(); ppt.close(); } }
相关文章推荐
- 如何使用 Beamer 创建演示文稿
- ASP.NET中实现导出ppt文件数据的实例分享
- C#实现将PPT转换成HTML的方法
- 使用impress.js制作幻灯片
- java读取word-excel-ppt文件代码
- PHP读取PPT文件的方法
- Android SDK 百度地图通过poi城市内检索简介接口的使用
- Android实现带列表的地图POI周边搜索功能
- Android百度地图poi范围搜索
- Android 百度地图POI搜索功能实例代码
- Java使用poi操作excel实例解析
- Java 使用poi把数据库中数据导入Excel的解决方法
- java使用POI读取properties文件并写到Excel的方法
- java使用poi读取ppt文件和poi读取excel、word示例
- java实现PPT转PDF出现中文乱码问题的解决方法
- Python实现对PPT文件进行截图操作的方法
- Python输出PowerPoint(ppt)文件中全部文字信息的方法
- 如何写好PPT