poi操作ppt创建表格

2024-02-01 02:48
文章标签 操作 ppt poi 创建表格

本文主要是介绍poi操作ppt创建表格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

POI版本3.14。

创建powerpoint,一页ppt,里面插入两个表格。

/* ====================================================================Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements.  See the NOTICE file distributed withthis 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 withthe License.  You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations 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 taableString[][] 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();//构造一页pptHSLFSlide 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 taableString[][] 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 columnHSLFTable 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();}
}


这篇关于poi操作ppt创建表格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/665852

相关文章

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

Java Stream.reduce()方法操作实际案例讲解

《JavaStream.reduce()方法操作实际案例讲解》reduce是JavaStreamAPI中的一个核心操作,用于将流中的元素组合起来产生单个结果,:本文主要介绍JavaStream.... 目录一、reduce的基本概念1. 什么是reduce操作2. reduce方法的三种形式二、reduce

MySQL表空间结构详解表空间到段页操作

《MySQL表空间结构详解表空间到段页操作》在MySQL架构和存储引擎专题中介绍了使用不同存储引擎创建表时生成的表空间数据文件,在本章节主要介绍使用InnoDB存储引擎创建表时生成的表空间数据文件,对... 目录️‍一、什么是表空间结构1.1 表空间与表空间文件的关系是什么?️‍二、用户数据在表空间中是怎么

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Mysql数据库中数据的操作CRUD详解

《Mysql数据库中数据的操作CRUD详解》:本文主要介绍Mysql数据库中数据的操作(CRUD),详细描述对Mysql数据库中数据的操作(CRUD),包括插入、修改、删除数据,还有查询数据,包括... 目录一、插入数据(insert)1.插入数据的语法2.注意事项二、修改数据(update)1.语法2.有

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

Java实现MinIO文件上传的加解密操作

《Java实现MinIO文件上传的加解密操作》在云存储场景中,数据安全是核心需求之一,MinIO作为高性能对象存储服务,支持通过客户端加密(CSE)在数据上传前完成加密,下面我们来看看如何通过Java... 目录一、背景与需求二、技术选型与原理1. 加密方案对比2. 核心算法选择三、完整代码实现1. 加密上

SQL常用操作精华之复制表、跨库查询、删除重复数据

《SQL常用操作精华之复制表、跨库查询、删除重复数据》:本文主要介绍SQL常用操作精华之复制表、跨库查询、删除重复数据,这些SQL操作涵盖了数据库开发中最常用的技术点,包括表操作、数据查询、数据管... 目录SQL常用操作精华总结表结构与数据操作高级查询技巧SQL常用操作精华总结表结构与数据操作复制表结

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性