Java uses poi to export the implementation code of ppt file
- 2021-10-11 18:24:38
- OfStack
What is poi
Apache POI is a free and open source cross-platform Java API written by Java. Apache POI provides API to Java program to read and write Microsoft Office format files. POI is an acronym for "Poor Obfuscation Implementation", meaning "Fuzzy implementation of concise version".
Packages commonly used in poi
HSSF-Provides the ability to read and write files in Microsoft Excel XLS format.
XSSF-Provides the ability to read and write files in Microsoft Excel OOXML XLSX format.
HWPF-Provides the ability to read and write files in Microsoft Word DOC format.
HSLF-Provides the ability to read and write files in Microsoft PowerPoint format.
HDGF-Provides the ability to read files in Microsoft Visio format.
HPBF-Provides the ability to read files in Microsoft Publisher format.
HSMF-Provides the ability to read files in Microsoft Outlook format.
Import poi related dependencies
<!-- Import dependencies -->
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
Write relevant code
XMLSlideShow ppt = new XMLSlideShow();
// Create a slide show
XSLFSlide slide = ppt.createSlide();
// Create a text box
XSLFTextBox textBox = slide.createTextBox();
// x y Set distance w h Setting Size
textBox.setAnchor(new Rectangle2D.Double(300,50, 100, 50));
// Set the contents of the text box
textBox.addNewTextParagraph().addNewTextRun().setText(" Create PPT");
// Insert a picture
// Object of the picture file Object
File file = new File("D:\\work\\ppt_demo\\src\\main\\resources\\static\\8.png");
// Get byte stream
byte[] bt = FileUtils.readFileToByteArray(file);
XSLFPictureData idx = ppt.addPicture(bt, PictureData.PictureType.PNG);
// Insert a picture
XSLFPictureShape pic = slide.createPicture(idx);
pic.setAnchor(new Rectangle2D.Double(100,100,500,350));
// Create a new 1 Slides on the page
XSLFSlide slide2 = ppt.createSlide();
XSLFTextBox textBox2 = slide2.createTextBox();
// x y Set distance w h Setting Size
textBox2.setAnchor(new Rectangle2D.Double(300,50, 100, 50));
textBox2.addNewTextParagraph().addNewTextRun().setText(" Create ppt2");
// Write ppt Medium
ppt.write(new FileOutputStream("D:\\work\\ppt_demo\\src\\main\\resources\\static\\ppt8.pptx"));
The above is Java using poi export ppt file implementation code details, more about Java export ppt file information please pay attention to other related articles on this site!