Skip to content

Commit 74feb03

Browse files
committed
JavaLib 之 office 操作示例
1 parent 4a3cff4 commit 74feb03

3 files changed

Lines changed: 414 additions & 0 deletions

File tree

codes/javalib/office/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.github.dunwu.javalib</groupId>
8+
<artifactId>javalib-office</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>pom</packaging>
11+
<name>JavaLib Demos - Office</name>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<java.version>1.8</java.version>
16+
<maven.compiler.source>${java.version}</maven.compiler.source>
17+
<maven.compiler.target>${java.version}</maven.compiler.target>
18+
<poi.version>4.0.0</poi.version>
19+
</properties>
20+
21+
<dependencies>
22+
<!-- OFFICE BEGIN -->
23+
<dependency>
24+
<groupId>org.apache.poi</groupId>
25+
<artifactId>poi</artifactId>
26+
<version>${poi.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.apache.poi</groupId>
30+
<artifactId>poi-ooxml</artifactId>
31+
<version>${poi.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.poi</groupId>
35+
<artifactId>poi-scratchpad</artifactId>
36+
<version>${poi.version}</version>
37+
</dependency>
38+
<!-- OFFICE END -->
39+
40+
<dependency>
41+
<groupId>junit</groupId>
42+
<artifactId>junit</artifactId>
43+
<version>4.12</version>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
</project>
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
package io.github.dunwu.javalib.office;
2+
3+
4+
import org.apache.poi.hpsf.DocumentSummaryInformation;
5+
import org.apache.poi.hpsf.SummaryInformation;
6+
import org.apache.poi.hwpf.HWPFDocument;
7+
import org.apache.poi.ooxml.POIXMLProperties;
8+
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
9+
import org.apache.poi.xwpf.usermodel.*;
10+
11+
import java.io.File;
12+
import java.io.FileInputStream;
13+
import java.io.FileOutputStream;
14+
import java.io.IOException;
15+
16+
/**
17+
* @author Zhang Peng
18+
* @since 2018-11-08
19+
* @see <a href="https://poi.apache.org/">https://poi.apache.org/</a>
20+
* @see
21+
* <a href="https://www.w3cschool.cn/apache_poi_word/apache_poi_word_overview.html">https://www.w3cschool.cn/apache_poi_word/apache_poi_word_overview.html</a>
22+
*/
23+
public class WordUtil {
24+
/**
25+
* 创建空白文档
26+
* @param filename
27+
* @throws IOException
28+
*/
29+
public static void create(String filename) throws IOException {
30+
//Blank Document
31+
XWPFDocument document = new XWPFDocument();
32+
//Write the Document in file system
33+
FileOutputStream out = new FileOutputStream(new File(filename));
34+
document.write(out);
35+
out.close();
36+
System.out.printf("create %s written successully\n", filename);
37+
}
38+
39+
/**
40+
* 创建 *.docx 文档,包含 content 内容
41+
* @param filename
42+
* @throws IOException
43+
*/
44+
public static void create(String filename, String content) throws IOException {
45+
//Blank Document
46+
XWPFDocument document = new XWPFDocument();
47+
//Write the Document in file system
48+
FileOutputStream out = new FileOutputStream(new File(filename));
49+
50+
//create Paragraph
51+
XWPFParagraph paragraph = document.createParagraph();
52+
XWPFRun run = paragraph.createRun();
53+
run.setText(content);
54+
document.write(out);
55+
out.close();
56+
System.out.printf("create %s written successully\n", filename);
57+
}
58+
59+
/**
60+
* 创建 *.docx 文档,包含 content 内容,content 内容置于边框中
61+
* @param filename
62+
* @throws IOException
63+
*/
64+
public static void createWithBorders(String filename, String content) throws IOException {
65+
//Blank Document
66+
XWPFDocument document = new XWPFDocument();
67+
68+
//Write the Document in file system
69+
FileOutputStream out = new FileOutputStream(new File(filename));
70+
71+
//create paragraph
72+
XWPFParagraph paragraph = document.createParagraph();
73+
74+
//Set bottom border to paragraph
75+
paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
76+
77+
//Set left border to paragraph
78+
paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
79+
80+
//Set right border to paragraph
81+
paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
82+
83+
//Set top border to paragraph
84+
paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);
85+
86+
XWPFRun run = paragraph.createRun();
87+
run.setText(content);
88+
89+
document.write(out);
90+
out.close();
91+
System.out.printf("create %s written successully\n", filename);
92+
}
93+
94+
/**
95+
* 表格
96+
* @param filename
97+
* @throws IOException
98+
*/
99+
public static void createWithTable(String filename) throws IOException {
100+
//Blank Document
101+
XWPFDocument document = new XWPFDocument();
102+
103+
//Write the Document in file system
104+
FileOutputStream out = new FileOutputStream(new File(filename));
105+
106+
//create table
107+
XWPFTable table = document.createTable();
108+
//create first row
109+
XWPFTableRow tableRowOne = table.getRow(0);
110+
tableRowOne.getCell(0).setText("col one, row one");
111+
tableRowOne.addNewTableCell().setText("col two, row one");
112+
tableRowOne.addNewTableCell().setText("col three, row one");
113+
//create second row
114+
XWPFTableRow tableRowTwo = table.createRow();
115+
tableRowTwo.getCell(0).setText("col one, row two");
116+
tableRowTwo.getCell(1).setText("col two, row two");
117+
tableRowTwo.getCell(2).setText("col three, row two");
118+
//create third row
119+
XWPFTableRow tableRowThree = table.createRow();
120+
tableRowThree.getCell(0).setText("col one, row three");
121+
tableRowThree.getCell(1).setText("col two, row three");
122+
tableRowThree.getCell(2).setText("col three, row three");
123+
124+
document.write(out);
125+
out.close();
126+
System.out.printf("create %s written successully\n", filename);
127+
}
128+
129+
/**
130+
* 字体样式
131+
* @param filename
132+
* @throws IOException
133+
*/
134+
public static void createWithFontStyle(String filename) throws IOException {
135+
//Blank Document
136+
XWPFDocument document = new XWPFDocument();
137+
138+
//Write the Document in file system
139+
FileOutputStream out = new FileOutputStream(new File(filename));
140+
141+
//create paragraph
142+
XWPFParagraph paragraph = document.createParagraph();
143+
144+
//Set Bold an Italic
145+
XWPFRun paragraphOneRunOne = paragraph.createRun();
146+
paragraphOneRunOne.setBold(true);
147+
paragraphOneRunOne.setItalic(true);
148+
paragraphOneRunOne.setText("Font Style");
149+
paragraphOneRunOne.addBreak();
150+
151+
//Set text Position
152+
XWPFRun paragraphOneRunTwo = paragraph.createRun();
153+
paragraphOneRunTwo.setText("Font Style two");
154+
paragraphOneRunTwo.setTextPosition(100);
155+
156+
//Set Strike through and Font Size and Subscript
157+
XWPFRun paragraphOneRunThree = paragraph.createRun();
158+
paragraphOneRunThree.setStrike(true);
159+
paragraphOneRunThree.setFontSize(20);
160+
paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
161+
paragraphOneRunThree.setText(" Different Font Styles");
162+
163+
document.write(out);
164+
out.close();
165+
System.out.printf("create %s written successully\n", filename);
166+
}
167+
168+
/**
169+
* 对齐方式
170+
* @param filename
171+
* @throws IOException
172+
*/
173+
public static void createWithAlign(String filename) throws IOException {
174+
//Blank Document
175+
XWPFDocument document = new XWPFDocument();
176+
177+
//Write the Document in file system
178+
FileOutputStream out = new FileOutputStream(new File(filename));
179+
180+
//create paragraph
181+
XWPFParagraph paragraph = document.createParagraph();
182+
183+
//Set alignment paragraph to RIGHT
184+
paragraph.setAlignment(ParagraphAlignment.RIGHT);
185+
XWPFRun run = paragraph.createRun();
186+
run.setText("At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning "
187+
+ "purpose in the domains of Academics, Information " + "Technology, Management and Computer Programming "
188+
+ "Languages.");
189+
190+
//Create Another paragraph
191+
paragraph = document.createParagraph();
192+
193+
//Set alignment paragraph to CENTER
194+
paragraph.setAlignment(ParagraphAlignment.CENTER);
195+
run = paragraph.createRun();
196+
run.setText(
197+
"The endeavour started by Mohtashim, an AMU " + "alumni, who is the founder and the managing director "
198+
+ "of Tutorials Point (I) Pvt. Ltd. He came up with the "
199+
+ "website tutorialspoint.com in year 2006 with the help"
200+
+ "of handpicked freelancers, with an array of tutorials" + " for computer programming languages. ");
201+
document.write(out);
202+
out.close();
203+
System.out.printf("create %s written successully\n", filename);
204+
}
205+
206+
/**
207+
* 文本提取
208+
* @param filename
209+
* @throws IOException
210+
*/
211+
public static void extractor(String filename) throws IOException {
212+
XWPFDocument docx = new XWPFDocument(new FileInputStream(filename));
213+
//using XWPFWordExtractor Class
214+
XWPFWordExtractor we = new XWPFWordExtractor(docx);
215+
System.out.println(we.getText());
216+
}
217+
218+
public static void setDocxProperties(String filename) throws IOException {
219+
FileInputStream fis = new FileInputStream(new File(filename));
220+
XWPFDocument doc = new XWPFDocument(fis);
221+
222+
POIXMLProperties properties = doc.getProperties();
223+
POIXMLProperties.CoreProperties coreProperties = properties.getCoreProperties();
224+
coreProperties.setCreator("星环信息科技有限公司");
225+
coreProperties.setLastModifiedByUser("星环信息科技有限公司");
226+
POIXMLProperties.ExtendedProperties extendedProperties = properties.getExtendedProperties();
227+
extendedProperties.getUnderlyingProperties().setCompany("星环信息科技有限公司");
228+
229+
FileOutputStream fos = new FileOutputStream(new File(filename));
230+
doc.write(fos);
231+
232+
fos.close();
233+
doc.close();
234+
fis.close();
235+
}
236+
237+
public static void setDocProperties(String filename) throws IOException {
238+
System.out.println("filename = [" + filename + "]");
239+
FileInputStream fis = new FileInputStream(new File(filename));
240+
HWPFDocument doc = new HWPFDocument(fis);
241+
242+
SummaryInformation summaryInformation = doc.getSummaryInformation();
243+
summaryInformation.setAuthor("张鹏");
244+
summaryInformation.setLastAuthor("张鹏");
245+
DocumentSummaryInformation documentSummaryInformation = doc.getDocumentSummaryInformation();
246+
documentSummaryInformation.setCompany("张鹏");
247+
documentSummaryInformation.setDocumentVersion("1");
248+
249+
FileOutputStream fos = new FileOutputStream(new File(filename));
250+
doc.write(fos);
251+
252+
fos.close();
253+
doc.close();
254+
fis.close();
255+
}
256+
}

0 commit comments

Comments
 (0)