Skip to content

Commit 4af9ec2

Browse files
committed
20210902 增加 poi 的最佳实践
1 parent bb733f7 commit 4af9ec2

23 files changed

+4065
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626

2727

2828

29+
## 项目3:poi-practice
30+
31+
### 参考文档
32+
33+
- [进行Junit测试时 从 Resources读取文件](https://blog.csdn.net/weixin_40040107/article/details/90679413)
34+
- [JAVA实现POI对excel操作,超方便实用【实践代码】](https://www.jianshu.com/p/d63571a8195c)
35+
- [JAVA POI报错:org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTRImpl.getXmlObjectArray](https://blog.csdn.net/u011781521/article/details/116260483)
36+
37+
38+
2939

3040

3141

poi-practice/pom.xml

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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>com.coderdream</groupId>
8+
<artifactId>poi-practice</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>poi-practice</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<poi.version>5.0.0</poi.version>
20+
</properties>
21+
22+
<dependencies>
23+
24+
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
25+
<dependency>
26+
<groupId>org.apache.poi</groupId>
27+
<artifactId>poi</artifactId>
28+
<version>${poi.version}</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.apache.poi</groupId>
33+
<artifactId>poi-ooxml</artifactId>
34+
<version>${poi.version}</version>
35+
</dependency>
36+
37+
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
38+
<!--
39+
<dependency>
40+
<groupId>org.apache.poi</groupId>
41+
<artifactId>poi-ooxml-schemas</artifactId>
42+
<version>${poi.version}</version>
43+
</dependency>
44+
-->
45+
<dependency>
46+
<groupId>org.apache.poi</groupId>
47+
<artifactId>poi</artifactId>
48+
<version>5.0.0</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.apache.poi</groupId>
53+
<artifactId>poi-scratchpad</artifactId>
54+
<version>5.0.0</version>
55+
</dependency>
56+
<!-- <dependency>
57+
<groupId>org.apache.poi</groupId>
58+
<artifactId>poi-ooxml-schemas</artifactId>
59+
<version>4.1.2</version>
60+
</dependency>-->
61+
<dependency>
62+
<groupId>org.apache.poi</groupId>
63+
<artifactId>poi-ooxml-full</artifactId>
64+
<version>5.0.0</version>
65+
</dependency>
66+
67+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
68+
<dependency>
69+
<groupId>org.apache.commons</groupId>
70+
<artifactId>commons-lang3</artifactId>
71+
<version>3.12.0</version>
72+
</dependency>
73+
74+
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
75+
<dependency>
76+
<groupId>org.projectlombok</groupId>
77+
<artifactId>lombok</artifactId>
78+
<version>1.18.20</version>
79+
<scope>provided</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>javax.servlet</groupId>
83+
<artifactId>javax.servlet-api</artifactId>
84+
<version>3.1.0</version>
85+
</dependency>
86+
87+
<!-- 文件上传依赖包 -->
88+
<dependency>
89+
<groupId>commons-io</groupId>
90+
<artifactId>commons-io</artifactId>
91+
<version>2.4</version>
92+
</dependency>
93+
<!-- 文件上传包 -->
94+
<dependency>
95+
<groupId>commons-fileupload</groupId>
96+
<artifactId>commons-fileupload</artifactId>
97+
<version>1.3.1</version>
98+
</dependency>
99+
100+
<dependency>
101+
<groupId>junit</groupId>
102+
<artifactId>junit</artifactId>
103+
<version>4.11</version>
104+
<scope>test</scope>
105+
</dependency>
106+
</dependencies>
107+
108+
<build>
109+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
110+
<plugins>
111+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
112+
<plugin>
113+
<artifactId>maven-clean-plugin</artifactId>
114+
<version>3.1.0</version>
115+
</plugin>
116+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
117+
<plugin>
118+
<artifactId>maven-resources-plugin</artifactId>
119+
<version>3.0.2</version>
120+
</plugin>
121+
<plugin>
122+
<artifactId>maven-compiler-plugin</artifactId>
123+
<version>3.8.0</version>
124+
</plugin>
125+
<plugin>
126+
<artifactId>maven-surefire-plugin</artifactId>
127+
<version>2.22.1</version>
128+
</plugin>
129+
<plugin>
130+
<artifactId>maven-jar-plugin</artifactId>
131+
<version>3.0.2</version>
132+
</plugin>
133+
<plugin>
134+
<artifactId>maven-install-plugin</artifactId>
135+
<version>2.5.2</version>
136+
</plugin>
137+
<plugin>
138+
<artifactId>maven-deploy-plugin</artifactId>
139+
<version>2.8.2</version>
140+
</plugin>
141+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
142+
<plugin>
143+
<artifactId>maven-site-plugin</artifactId>
144+
<version>3.7.1</version>
145+
</plugin>
146+
<plugin>
147+
<artifactId>maven-project-info-reports-plugin</artifactId>
148+
<version>3.0.0</version>
149+
</plugin>
150+
</plugins>
151+
</pluginManagement>
152+
</build>
153+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderdream;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.coderdream;
2+
3+
/**
4+
* @author :CoderDream
5+
* @date :Created in 2021/9/2 17:09
6+
* @description:
7+
* @modified By:CoderDream
8+
* @version: $
9+
*/
10+
11+
12+
import org.apache.commons.lang3.StringUtils;
13+
14+
import java.nio.charset.Charset;
15+
import java.nio.charset.StandardCharsets;
16+
17+
/**
18+
* 字符集工具类
19+
*
20+
* @author ruoyi
21+
*/
22+
public class CharsetKit
23+
{
24+
/** ISO-8859-1 */
25+
public static final String ISO_8859_1 = "ISO-8859-1";
26+
/** UTF-8 */
27+
public static final String UTF_8 = "UTF-8";
28+
/** GBK */
29+
public static final String GBK = "GBK";
30+
31+
/** ISO-8859-1 */
32+
public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO_8859_1);
33+
/** UTF-8 */
34+
public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8);
35+
/** GBK */
36+
public static final Charset CHARSET_GBK = Charset.forName(GBK);
37+
38+
/**
39+
* 转换为Charset对象
40+
*
41+
* @param charset 字符集,为空则返回默认字符集
42+
* @return Charset
43+
*/
44+
public static Charset charset(String charset)
45+
{
46+
return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
47+
}
48+
49+
/**
50+
* 转换字符串的字符集编码
51+
*
52+
* @param source 字符串
53+
* @param srcCharset 源字符集,默认ISO-8859-1
54+
* @param destCharset 目标字符集,默认UTF-8
55+
* @return 转换后的字符集
56+
*/
57+
public static String convert(String source, String srcCharset, String destCharset)
58+
{
59+
return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));
60+
}
61+
62+
/**
63+
* 转换字符串的字符集编码
64+
*
65+
* @param source 字符串
66+
* @param srcCharset 源字符集,默认ISO-8859-1
67+
* @param destCharset 目标字符集,默认UTF-8
68+
* @return 转换后的字符集
69+
*/
70+
public static String convert(String source, Charset srcCharset, Charset destCharset)
71+
{
72+
if (null == srcCharset)
73+
{
74+
srcCharset = StandardCharsets.ISO_8859_1;
75+
}
76+
77+
if (null == destCharset)
78+
{
79+
destCharset = StandardCharsets.UTF_8;
80+
}
81+
82+
if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset))
83+
{
84+
return source;
85+
}
86+
return new String(source.getBytes(srcCharset), destCharset);
87+
}
88+
89+
/**
90+
* @return 系统字符集编码
91+
*/
92+
public static String systemCharset()
93+
{
94+
return Charset.defaultCharset().name();
95+
}
96+
}
97+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coderdream;
2+
3+
/**
4+
* @author Hongten
5+
* @created 2014-5-21
6+
*/
7+
public class Common {
8+
9+
public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
10+
public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
11+
12+
public static final String EMPTY = "";
13+
public static final String POINT = ".";
14+
public static final String LIB_PATH = Class.class.getClass().getResource("/").getPath();;
15+
public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2003_POSTFIX;
16+
public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2010_POSTFIX;
17+
public static final String STUDENT_INFO_XLS_OUT_PATH = LIB_PATH + "/student_info_2003-2007.xls";
18+
public static final String STUDENT_INFO_XLSX_OUT_PATH = LIB_PATH + "/student_info_2010.xlsx";
19+
public static final String NOT_EXCEL_FILE = " : Not the Excel file!";
20+
public static final String PROCESSING = "Processing...";
21+
public static final String WRITE_DATA = "write data to file : ";
22+
23+
}

0 commit comments

Comments
 (0)