java 如何将word 转换为ftl_java+freemarker+word 生成转换doc文档

package com.csnt.scdp.bizmodules.helper;

import com.csnt.scdp.framework.helper.IOHelper;

import com.csnt.scdp.framework.util.UUIDUtil;

import freemarker.template.Template;

import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;

import java.io.*;

import java.net.URLEncoder;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;/**

* Created by weixiao on 2018/9/6.*/public class DocGenerateHelper {

public static String genDoc(Map outMap, Template t, String clientFileName) throws Exception {

SimpleDateFormat df= new SimpleDateFormat("MMddHHmmss");

String tempFileName= IOHelper.checkPath(System.getProperty("java.io.tmpdir")) + "docgenerate" + File.separator + df.format(new Date()) + File.separator + clientFileName + ".doc";

File file= newFile(tempFileName);

File fileParent=file.getParentFile();//首先创建父类文件夹

if (!fileParent.exists()) {

fileParent.mkdirs();

}

file.createNewFile();

Writer out= null;try{

IOHelper.generateEmptyDic(tempFileName,false);

out= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFileName), "utf-8"), 10240);

t.process(outMap, out);

}finally{

out.flush();

out.close();

}returntempFileName;

}//通过文件输入输出流下载word

//设置向浏览器端传送的文件格式

public static voidgenerateDocWithDownload(Map outMap, Template t, String clientFileName,HttpServletResponse response) throws Exception {if (clientFileName == null || clientFileName == "") {

clientFileName=UUIDUtil.getUUID();

}

String fileName=genDoc(outMap, t, clientFileName);

response.reset();

InputStream fis= null;

OutputStream toClient= null;

File outFile= newFile(fileName);try{

fis= new BufferedInputStream(newFileInputStream(outFile));byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();//清空response

response.reset();//设置response的Header

clientFileName = URLEncoder.encode(clientFileName + ".doc", "utf-8"); //这里要用URLEncoder转下才能正确显示中文名称

response.addHeader("Content-Disposition", "attachment;filename=" +clientFileName);

response.addHeader("Content-Length", "" +outFile.length());

toClient= newBufferedOutputStream(response.getOutputStream());

response.setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

fis.close();

toClient.close();

}finally{

WordExportHelper.deleteFile(fileName);

String directory=fileName.substring(0,fileName.lastIndexOf("\\"));

WordExportHelper.deleteFile(directory);if (fis != null) {

fis.close();

}if (toClient != null) {

toClient.close();

}

}

}//通过文件输入输出流下载word

//设置向浏览器端传送的文件格式

public static voidgenerateDocDownload(String path,String clientFileName,HttpServletResponse response) throws Exception {if (clientFileName == null || clientFileName == "") {

clientFileName=UUIDUtil.getUUID();

}//String fileName=genDoc(clientFileName);

response.reset();

InputStream fis= null;

OutputStream toClient= null;

File outFile= newFile(path);try{

fis= new BufferedInputStream(newFileInputStream(outFile));byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();//清空response

response.reset();//设置response的Header//clientFileName = URLEncoder.encode(clientFileName + ".doc", "utf-8"); //这里要用URLEncoder转下才能正确显示中文名称

response.addHeader("Content-Disposition", "attachment;filename=" +clientFileName);

response.addHeader("Content-Length", "" +outFile.length());

toClient= newBufferedOutputStream(response.getOutputStream());

response.setContentType("application/octet-stream");

toClient.flush();

fis.close();

toClient.close();

}finally{if (fis != null) {

fis.close();

}if (toClient != null) {

toClient.close();

}

}

}

public static String genDoc(String clientFileName) throws Exception {

SimpleDateFormat df= new SimpleDateFormat("MMddHHmmss");

String tempFileName= IOHelper.checkPath(System.getProperty("java.io.tmpdir")) + "docgenerate" + File.separator + df.format(new Date()) + File.separator + clientFileName + ".doc";

File file= newFile(tempFileName);

File fileParent=file.getParentFile();//首先创建父类文件夹

if (!fileParent.exists()) {

fileParent.mkdirs();

}

file.createNewFile();

Writer out= null;try{

IOHelper.generateEmptyDic(tempFileName,false);

out= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFileName), "utf-8"), 10240);

}finally{

out.flush();

out.close();

}returntempFileName;

}

public static String generateDocWithDownload_temp(Map outMap, Template t, String clientFileName) throws Exception {

String fileUrl= "";if (clientFileName == null || clientFileName == "") {

clientFileName=UUIDUtil.getUUID();

}

String fileName=genDoc(outMap, t, clientFileName);

HttpServletResponse response=ServletActionContext.getResponse();

response.reset();

InputStream fis= null;

OutputStream toClient= null;

File outFile= newFile(fileName);try{

fis= new BufferedInputStream(newFileInputStream(outFile));byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();//清空response

response.reset();//设置response的Header

clientFileName = URLEncoder.encode(clientFileName + ".doc", "utf-8"); //这里要用URLEncoder转下才能正确显示中文名称

response.addHeader("Content-Disposition", "attachment;filename=" +clientFileName);

response.addHeader("Content-Length", "" +outFile.length());

toClient= newBufferedOutputStream(response.getOutputStream());

response.setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

fileUrl=outFile.toString();

fis.close();

toClient.close();

}finally{if (fis != null) {

fis.close();

}if (toClient != null) {

toClient.close();

}

}returnfileUrl;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来回答你的问题。 在Java中,我们可以使用Apache FreeMarker作为模板引擎来将Word化为ftl模板。下面是一个简单的示例代码: ``` import freemarker.template.Template; import freemarker.template.Configuration; import freemarker.template.TemplateExceptionHandler; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Map; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; public class WordToFTLConverter { public static void convert(String inputFile, String outputFile, Map<String, Object> data) throws IOException, InvalidFormatException { XWPFDocument docx = new XWPFDocument(new FileInputStream(inputFile)); Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setDirectoryForTemplateLoading(new File("./templates")); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); Template template = cfg.getTemplate("example.ftl"); Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"); template.process(data, out); out.flush(); out.close(); } } ``` 在这个示例中,我们使用Apache POI来解析Word文档,并使用Apache FreeMarkerWord化为ftl模板。我们需要提供两个参数:一个输入文件路径和一个输出文件路径。此外,我们还需要提供一个Map对象,其中包含ftl模板中要使用的数据。 需要注意的是,我们还需要在类路径中提供一个名为"example.ftl"的模板文件,它将被用于将Word化为ftl模板。 希望这个回答能够解决你的问题,如果你还有其他的疑问,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值