一、添加依赖
<properties>
<poi.version>4.1.0</poi.version>
</properties>
<!-- poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- poi处理xlsx格式,用于处理word中的表格 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- poi-tl基于poi的word模板引擎 -->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
</dependency>
二、创建工具类
import org.apache.poi.sl.usermodel.TextShape;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.awt.*;
import java.io.*;
import java.util.Date;
import java.util.Optional;
/**
* @Author: BillYu
* @Description:创建三件套文件
* @Date: Created in 11:02 2022/10/12.
*/
public class PoiUtil {
public static byte[] pptTemplateByteArray=null;
static {
try {
pptTemplateByteArray = IOUtils.toByteArray(PoiUtil.class.getClassLoader().getResourceAsStream("演示文稿.pptx"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] createWordFile() throws IOException {
XWPFDocument doc = new XWPFDocument();
//设置作者为空,默认为Apache POI
Date now = new Date();
doc.getProperties().getCoreProperties().setCreated(Optional.of(now));
doc.getProperties().getCoreProperties().setModified(Optional.of(now));
doc.getProperties().getCoreProperties().setTitle(null);
doc.getProperties().getCoreProperties().setCreator(null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
doc.write(bos);
return bos.toByteArray();
}
public static byte[] createExcelFile() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("工作表1");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Date now = new Date();
wb.getProperties().getCoreProperties().setCreated(Optional.of(now));
wb.getProperties().getCoreProperties().setModified(Optional.of(now));
wb.getProperties().getCoreProperties().setTitle(null);
wb.getProperties().getCoreProperties().setCreator(null);
wb.write(bos);
return bos.toByteArray();
}
/**
* 根据ppt模版文件创建
* @return
* @throws IOException
*/
public static byte[] createPPTFileByTemplate() throws IOException {
XMLSlideShow ppt = new XMLSlideShow(new ByteArrayInputStream(pptTemplateByteArray));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Date now = new Date();
ppt.getProperties().getCoreProperties().setCreated(Optional.of(now));
ppt.getProperties().getCoreProperties().setModified(Optional.of(now));
ppt.getProperties().getCoreProperties().setTitle(null);
ppt.getProperties().getCoreProperties().setCreator(null);
ppt.write(bos);
return bos.toByteArray();
}
/**
* 创建ppt文件并手动绘制第一页
* 较为复杂,效果没有模版文件快捷方便
* @return
* @throws IOException
*/
public static byte[] createPPTFile() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
Date now = new Date();
ppt.getProperties().getCoreProperties().setCreated(Optional.of(now));
ppt.getProperties().getCoreProperties().setModified(Optional.of(now));
ppt.getProperties().getCoreProperties().setTitle(null);
ppt.getProperties().getCoreProperties().setCreator(null);
XSLFSlide slide1 = ppt.createSlide();
slide1.createTextBox();
XSLFTextBox shape1 = slide1.createTextBox();
Rectangle anchor = new Rectangle( 100, 100, 500, 200);
shape1.setAnchor(anchor);
//添加标题文本库
shape1.setTextPlaceholder(TextShape.TextPlaceholder.TITLE);
shape1.setHorizontalCentered(true);
XSLFTextBox shape2 = slide1.createTextBox();
Rectangle anchor2 = new Rectangle(100, 310, 500, 120);
shape2.setAnchor(anchor2);
shape2.setTextPlaceholder(TextShape.TextPlaceholder.HALF_BODY);
shape2.setHorizontalCentered(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ppt.write(bos);
return bos.toByteArray();
}
}
三、测试验证
public static void main(String[] args) {
try {
PoiUtil.createWordFile();
PoiUtil.createExcelFile();
PoiUtil.createPPTFileByTemplate();
PoiUtil.createPPTFile();
} catch (IOException e) {
e.printStackTrace();
}
}
通常需求会在创建文档时要求填充一些初始化的内容格式,这时主要有两种途径:一是创建空白文档通过poi绘制需要的内容和样式,这种方式可能会因为内容样式复杂而导致绘制比较繁琐,同时会有一定耗时;另一种是通过文件模版创建,在某个文件的基础上进行创建,在程序启动时将文件流读取到服务内存(需要注意文件大小),更加方便快捷。