关于word的文件的相关操作代码
```java
package com.jeecg.word;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import freemarker.template.Configuration;
import freemarker.template.Template;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.ConnectException;
import java.util.Map;
import java.util.Properties;
/**
* 描述: word文件操作工具类
*
* @author lk
* @version 1.0
* @date 2019/9/9 9:56
*/
public class WordUtil {
/**
* 功能描述: 根据模板生成word文件
* @param dataMap word中需要展示的动态数据,用map集合来保存
* @param templateName word模板名称,例如:test.ftl
* @param filePath 文件生成的目标路径,例如:D:/wordFile/
* @param fileName 生成的文件名称,例如:test.doc
* @return void
* @author lk
* @date 2019/9/9 9:04
*/
public static void createWord(Map dataMap, String templateName, String filePath, String fileName){
try {
//创建配置实例
Configuration configuration = new Configuration();
//设置编码
configuration.setDefaultEncoding("UTF-8");
//ftl模板文件
configuration.setClassForTemplateLoading(WordUtil.class,"template");
//获取模板
Template template = configuration.getTemplate(templateName);
//输出文件
File outFile = new File(filePath+File.separator+fileName);
//如果输出目标文件夹不存在,则创建
if (!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
//将模板和数据模型合并生成文件
Writer out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (outFile),"UTF-8"));
//生成文件
template.process(dataMap, out);
//关闭流
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* doc转pdf
* @param docPath doc文件路径,包含.doc
* @param pdfPath pdf文件路径,包含.pdf
* @return
*/
public static File doc2Pdf(String docPath, String pdfPath){
File pdfFile = new File(pdfPath);
FileOutputStream fileOutputStream = null;
String os = System.getProperty("os.name");
if(os.toLowerCase().indexOf("linux") !=-1){
FontSettings.setFontsFolder (WordUtil.getvalueByKey ("linuxFontPath"),false);
}
try {
String str = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes());
License license = new License();
license.setLicense(is);
com.aspose.words.Document document = new com.aspose.words.Document(docPath);
fileOutputStream= new FileOutputStream (pdfFile);
document.save( fileOutputStream,SaveFormat.PDF);
} catch (Exception e) {
System.out.println ("****** doc转pdf异常******");
e.printStackTrace();
}finally {
if(fileOutputStream != null){
try {
fileOutputStream.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
return pdfFile;
}
/**
* 功能描述: pdf文件在线预览
* @param pdfPath
* @param response
* @return void
* @author lk
* @date 2019/9/10 11:02
*/
public static void previewPdf(String pdfPath, HttpServletResponse response){
File pdfFile = new File (pdfPath);
String fileName = pdfFile.getName ();
OutputStream outputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("application/pdf;charset=UTF-8;");
response.addHeader("Content-Disposition", "inline;filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8")+"\"");
bufferedInputStream = new BufferedInputStream(new FileInputStream (pdfFile));
byte[] buffBytes = new byte[1024];
outputStream = response.getOutputStream();
int read = 0;
while ((read = bufferedInputStream.read(buffBytes)) != -1) {
outputStream.write(buffBytes, 0, read);
}
} catch (ConnectException e) {
System.out.println ("****pdf预览失败****");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(outputStream != null){
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取properties文件生成路径
* @return
*/
public static String getFilePath(){
InputStream insss =WordUtil.class.getClassLoader().getResourceAsStream("com/jeecg/word/template/fileSavePath.properties");
Properties pss = new Properties();
try {
pss.load(insss);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String filePath = pss.getProperty("filePath");
return filePath;
}
/**
* 功能描述: 读取properties 根据key获取value
* @param key
* @return java.lang.String
* @author lk
* @date 2019/9/12 9:25
*/
public static String getvalueByKey(String key){
InputStream insss =WordUtil.class.getClassLoader().getResourceAsStream("com/jeecg/word/template/fileSavePath.properties");
Properties pss = new Properties();
try {
pss.load(insss);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String filePath = pss.getProperty(key);
return filePath;
}
/**
* 功能描述: 文件下载
* @param filePath
* @param response
* @author lk
* @date 2019/10/15 16:34
*/
public static void downloadFile(String filePath, HttpServletResponse response){
File file = new File (filePath);
String fileName = file.getName ();
OutputStream outputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("application/msword;charset=UTF-8;");
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
bufferedInputStream = new BufferedInputStream(new FileInputStream (file));
byte[] buffBytes = new byte[1024];
outputStream = response.getOutputStream();
int read = 0;
while ((read = bufferedInputStream.read(buffBytes)) != -1) {
outputStream.write(buffBytes, 0, read);
}
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(outputStream != null){
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}