引入依賴
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
</dependency>
<!-- 下面的版本需要對應(yīng)上面依賴中的版本 否則可能會起沖突 -->
<!-- 下面的依賴主要是為了使用Apache原生的WordExtractor對doc后綴文件的解析 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.1</version>
</dependency>
<!-- 糊涂Api工具 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.8.10</version>
</dependency>
工具類封裝
public class WordDocumentUtil {
/**
* 解析文檔文件
*
* @param file 文檔文件
* @return 文檔內(nèi)容
*/
public static String parseWord(MultipartFile file) {
String wordTxt = "";
InputStream stream = null;
try {
if (file.getOriginalFilename().endsWith(".doc")) {
stream = file.getInputStream();
// Apache Poi
WordExtractor ex = new WordExtractor(stream);
wordTxt = ex.getText();
} else if (file.getOriginalFilename().endsWith(".docx")) {
stream = file.getInputStream();
// EasyPoi
XWPFDocument document = new XWPFDocument(stream);
XWPFWordExtractor ex = new XWPFWordExtractor(document);
wordTxt = ex.getText();
}
} catch (Exception e) {
// 此處建議拋出異常 "文檔解析有誤"
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return wordTxt;
}
/**
* 判斷文檔類型進行不同的分割方式
* ".doc"后綴需要以"\r\n"切割 而".docx"后綴需要以"\n"切割
*
* @param file 文件名:以file.getOriginalFilename()傳入
* @param wordTxt 文件內(nèi)容
* @return 內(nèi)容數(shù)組
*/
public static String[] judgeType(String file, String wordTxt) {
boolean suffixFlag = file.endsWith(".doc");
return suffixFlag ? Arrays.stream(wordTxt.split("\r\n")).toArray(String[]::new)
: Arrays.stream(wordTxt.split("\n")).toArray(String[]::new);
}
/**
* 導(dǎo)出resources下的word模板表
*
* @param fileName 文件名
* @param response {@link HttpServletResponse}
*/
public void exportTemplate(String fileName, HttpServletResponse response) {
InputStream inputStream = null;
try {
String path = "/word/" + fileName;
inputStream = this.getClass().getResourceAsStream(path);
String newFileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(fileName);
byte[] bytes = new byte[1024 * 1024];
// 輸入流讀取文件
if (inputStream != null) {
inputStream.read(bytes);
}
response.setCharacterEncoding("UTF-8");
response.setContentType("application/msword");
response.setHeader("Access-Control-Expose-Headers","Content-disposition");
response.setHeader("Content-Disposition","attachment;filename=" + newFileName);
response.getOutputStream().write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
亂碼問題
????????如果這里造成了讀取resources下的文件返回前端亂碼問題:除了HttpServletResponse響應(yīng)中設(shè)置字體問題,還有可能是因為在編譯期文件就已經(jīng)亂碼了,所以需要在pom.xml中增加以下配置。文章來源地址http://www.zghlxwxcb.cn/news/detail-676873.html
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>doc</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>
文章來源:http://www.zghlxwxcb.cn/news/detail-676873.html
到了這里,關(guān)于【Easypoi & Apache poi】 Java后端 Word導(dǎo)入與導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!