java上怎么把代码复制到WPS_java如何导出word和wps⽂档java如何导出word和wps⽂档
java如何导出word和wps⽂档
使⽤场景:打开⼀个表单页⾯,导出word或wps⽂件,代码框架基于springboot+jpa
⼀、准备word模板
⼆、l⽂件中引⼊依赖
cn.afterturn
easypoi-base
3.0.3
cn.afterturn
easypoi-web
3.0.3
cn.afterturn
easypoi-annotation
xml文件怎么打开3.0.3
三、构造ExportWord⼯具类,⾥⾯包含导出word和导出wps两种⽅法
导出⽂件⼯具类中的⽂件流⼀定要按照最先使⽤的最后关闭原则,否则会造成临时⽂件删除不了的问题。导出wps的⽅法就是在导出word ⽅法的基础上将临时⽂件内容写⼊到下载的wps⽂件中。
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.URLEncoder;
import java.util.Map;
public class ExportWord {
/**
* 导出word
*
第⼀步⽣成替换后的word⽂件,只⽀持docx
*
第⼆步下载⽣成的⽂件
*
第三步删除⽣成的临时⽂件
* @param templatePath word模板地址
* @param temDir ⽣成临时⽂件存放地址
* @param fileName ⽂件名
* @param params 替换的参数
* @param request HttpServletRequest
* @param response HttpServletResponse
*/
public static void exportWord(String templatePath, String temDir, String fileName, Map params, HttpServletRequest request, HttpServletResponse response){
Assert.dsWith(".docx"),"word导出请使⽤docx格式");
FileOutputStream fos = null;
OutputStream out = null;
if (!dsWith("/")){
temDir = temDir+ File.separator;
}
File dir = new File(temDir);
if (!ists()){
dir.mkdirs();
}
try {
String userAgent = Header("user-agent").toLowerCase();
if (ains("msie")||ains("like gecko")){
fileName = de(fileName,"UTF-8");
}else {
fileName = new Bytes("utf-8"),"ISO-8859-1");
}
XWPFDocument doc = portWord07(templatePath, params); String tmpPath = temDir + fileName;
fos = new FileOutputStream(tmpPath);
doc.write(fos);
//设置强制下载不打开
response.setContentType("application/force-download");
//设置⽂件名
response.addHeader("Content-Disposition","attachment;fileName=" + fileName); out = OutputStream();
doc.write(out);
}catch (Exception e){
e.printStackTrace();
}finally {
try {
out.close();
fos.close();
delFileWord(temDir,fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/
**
* 删除临时⽣成的⽂件
*/
public static void delFileWord(String filePath,String fileName){
File file = new File(filePath + fileName);
File file1 = new File(filePath);
file.delete();
file1.delete();
}
/**
* 导出wps
*
第⼀步⽣成替换后的word⽂件,只⽀持docx
*
第⼆步将临时word⽂件内容复制到下载⽣成的wps⽂件中
*
第三步删除⽣成的临时⽂件
* @param templatePath word模板地址
* @param temDir ⽣成临时⽂件存放地址
* @param fileName ⽂件名
* @param params 替换的参数
* @param request HttpServletRequest
* @param response HttpServletResponse
*/
public static void exportWps(String templatePath, String temDir,String fileName,Map params, HttpServletRequest request, HttpServletResponse response){
FileOutputStream fos = null;
InputStream input = null;
BufferedInputStream bins=null;
OutputStream outs = null;
BufferedOutputStream bouts = null;
if (!dsWith("/")){
temDir = temDir+ File.separator;
}
File dir = new File(temDir);
if (!ists()){
dir.mkdirs();
}
try {
String userAgent = Header("user-agent").toLowerCase();
if (ains("msie")||ains("like gecko")){
fileName = de(fileName,"UTF-8");
}else {
fileName = new Bytes("utf-8"),"ISO-8859-1");
}
XWPFDocument doc = portWord07(templatePath, params); String tmpPath = temDir + fileName;
fos= new FileOutputStream(tmpPath);
doc.write(fos);
input = new FileInputStream(tmpPath);
bins = new BufferedInputStream(input);
outs = OutputStream();
bouts = new BufferedOutputStream(outs);
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;fileName=" + fileName); int bytesRead=0;
byte[] buffer = new byte[8192];
while((bytesRead = ad(buffer,0,8192))!=-1){
bouts.write(buffer,0,bytesRead);
}
bouts.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try{
bouts.close();
outs.close();
bins.close();
input.close();
fos.close();
delFileWord(temDir,fileName);
}catch (Exception e){
e.printStackTrace();