Skip to content

Commit 13cbb11

Browse files
init
1 parent 30772a2 commit 13cbb11

File tree

11 files changed

+1337
-0
lines changed

11 files changed

+1337
-0
lines changed

POIWord/jacob-1.18-x64.dll

201 KB
Binary file not shown.

POIWord/pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.4.RELEASE</version>
10+
<relativePath /> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>com.example</groupId>
13+
<artifactId>POIWord</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>POIWord</name>
16+
<description>Demo project for Spring Boot</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
34+
<dependency>
35+
<groupId>org.apache.poi</groupId>
36+
<artifactId>poi</artifactId>
37+
<version>3.9</version>
38+
</dependency>
39+
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
40+
<dependency>
41+
<groupId>org.apache.poi</groupId>
42+
<artifactId>poi-ooxml</artifactId>
43+
<version>3.9</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.apache.poi</groupId>
47+
<artifactId>poi-excelant</artifactId>
48+
<version>3.9</version>
49+
</dependency>
50+
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
51+
<dependency>
52+
<groupId>commons-io</groupId>
53+
<artifactId>commons-io</artifactId>
54+
<version>2.4</version>
55+
</dependency>
56+
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
57+
<dependency>
58+
<groupId>org.freemarker</groupId>
59+
<artifactId>freemarker</artifactId>
60+
</dependency>
61+
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
62+
<dependency>
63+
<groupId>commons-codec</groupId>
64+
<artifactId>commons-codec</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.hynnet</groupId>
68+
<artifactId>jacob</artifactId>
69+
<version>1.18</version>
70+
</dependency>
71+
</dependencies>
72+
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-maven-plugin</artifactId>
78+
</plugin>
79+
</plugins>
80+
</build>
81+
82+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class PoiWordApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(PoiWordApplication.class, args);
11+
}
12+
13+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.example.demo.freemarker;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
7+
import java.io.FileOutputStream;
8+
import java.io.IOException;
9+
import java.io.OutputStreamWriter;
10+
11+
import org.apache.commons.codec.binary.Base64;
12+
import org.apache.commons.io.IOUtils;
13+
14+
import freemarker.core.ParseException;
15+
import freemarker.template.Configuration;
16+
import freemarker.template.MalformedTemplateNameException;
17+
import freemarker.template.Template;
18+
import freemarker.template.TemplateException;
19+
import freemarker.template.TemplateExceptionHandler;
20+
import freemarker.template.TemplateNotFoundException;
21+
22+
public class ExportDocUtil {
23+
24+
private Configuration configuration = null;
25+
26+
private String encoding;
27+
28+
public ExportDocUtil(String encoding) {
29+
this.encoding = encoding;
30+
configuration = new Configuration(Configuration.VERSION_2_3_27);
31+
configuration.setDefaultEncoding(encoding);
32+
try {
33+
configuration.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));
34+
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
35+
configuration.setLogTemplateExceptions(false);
36+
configuration.setWrapUncheckedExceptions(true);
37+
} catch (IOException e) {
38+
e.printStackTrace();
39+
}
40+
}
41+
42+
public Template getTemplate(String templateName) {
43+
Template template = null;
44+
try {
45+
template = configuration.getTemplate(templateName);
46+
} catch (TemplateNotFoundException e) {
47+
e.printStackTrace();
48+
} catch (MalformedTemplateNameException e) {
49+
e.printStackTrace();
50+
} catch (ParseException e) {
51+
e.printStackTrace();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
return template;
56+
}
57+
58+
public static String getImageStr(String imagePath) {
59+
FileInputStream inputStream = null;
60+
try {
61+
inputStream = new FileInputStream(imagePath);
62+
byte[] data = new byte[inputStream.available()];
63+
inputStream.read(data);
64+
return data != null ? Base64.encodeBase64String(data) : "";
65+
} catch (FileNotFoundException e) {
66+
e.printStackTrace();
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
} finally {
70+
IOUtils.closeQuietly(inputStream);
71+
}
72+
return "";
73+
}
74+
75+
public void exportDoc(String filePath, String templateName, Object data) {
76+
FileOutputStream fileOutputStream = null;
77+
OutputStreamWriter outputStreamWriter = null;
78+
BufferedWriter writer = null;
79+
try {
80+
fileOutputStream = new FileOutputStream(filePath);
81+
outputStreamWriter = new OutputStreamWriter(fileOutputStream, encoding);
82+
writer = new BufferedWriter(outputStreamWriter);
83+
getTemplate(templateName).process(data, writer);
84+
} catch (TemplateException e) {
85+
e.printStackTrace();
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
} finally {
89+
IOUtils.closeQuietly(fileOutputStream);
90+
IOUtils.closeQuietly(outputStreamWriter);
91+
IOUtils.closeQuietly(writer);
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)