javaxml转换word_java如何将xml类型的word⽂档转换为word
类型的⽂档
⼤致的思路是先⽤office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后⽤java来解析FreeMarker模板并输出Doc。经测试这样⽅式⽣成的word⽂档完全符合office标准,样式、内容控制⾮常便利,打印也不会变形,⽣成的⽂档和office中编辑⽂档完全⼀样。
⽤xml做导出⽅案。
先创建⼀个word⽂档,按照需求在word中填好⼀个模板,然后把对应的数据换成变量${},然后将⽂档保存为xml⽂档格式,使⽤⽂档编辑器打开这个xml格式的⽂档,去掉多余的xml符号,使⽤Freemarker读取这个⽂档然后替换掉变量,输出word⽂档即可
需要freemarker jar包
/**
Project Name:exam-services
File Name:DownloadService.java
Package Name:com.wt.service.download
Date:2016年9⽉28⽇下午4:44:37
Copyright (c) 2016, chenzhou1025@126 All Rights Reserved.
*/
package com.wt.service.download;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.URLEncoder;
SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import se.web.Invocation;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
/**
ClassName:DownloadService
Function: ⽂件下载.
Reason: ADD REASON.
Date: 2016年9⽉28⽇ 下午4:44:37
@author wpengfei
@version
@since JDK 1.6
@see
*/
@Service
public class DownloadService {
private Logger logger = Class());
/
**
* downLoad:(⽂件下载).
*
* @author wpengfei
* @param inv
* @param fileName
* @param path
* @throws IOException
* @since JDK 1.6
*/
public void downLoad(Invocation inv, String fileName, String path) throws IOException { File file = new File(path);// 构造要下载的⽂件
if (ists()) {
InputStream ins = null;
BufferedInputStream bins = null;
OutputStream outs = null;
BufferedOutputStream bouts = null;
try {
ins = new FileInputStream(path);// 构造⼀个读取⽂件的IO流对象
bins = new BufferedInputStream(ins);// 放到缓冲流⾥⾯
outs = Response().getOutputStream();// 获取⽂件输出IO流
bouts = new BufferedOutputStream(outs);
String path1 = Request().getSession().
getServletContext().getRealPath("/WEB-INF/downloads");
logger.info(path1);
设置response内容的类型Response().setHeader("Content-disposition",
"attachment;filename=" + de(fileName, "UTF-8"));// 设置头部信息
// Response().setContentLength((int)file.length());
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向⽹络传输⽂件流
while ((bytesRead = ad(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 这⾥⼀定要调⽤flush()⽅法
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bouts != null) {
bouts.close();
}
if (outs != null) {
outs.close();
}
if (bins != null) {
bins.close();
}
if (ins != null) {
ins.close();
}
}
} else {
logger.info("导出的⽂件不存在");
}
}
}
package com.wtmon.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
plate.Configuration;
plate.Template;
/**
@Desc:word操作⼯具类
@Author:
@Date:2014-1-22下午05:03:19
*/
public class WordUtil {
@SuppressWarnings("rawtypes")
public static String createWord2(Map dataMap, String templateName, String filePath, String fileName) { try {
// 创建配置实例
Configuration configuration = new Configuration();
// 设置编码
configuration.setDefaultEncoding("UTF-8");
// ftl模板⽂件统⼀放⾄ plate 包下⾯
configuration.setClassForTemplateLoading(WordUtil.class, "\\com\\wt\\common\\util\\");
// 获取模板
Template template = Template(templateName);
// 输出⽂件
File outFile = new File(filePath + File.separator + fileName);xml文件怎么打开
// 如果输出⽬标⽂件夹不存在,则创建
if (!ParentFile().exists()) {
}
// 将模板和数据模型合并⽣成⽂件
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
/
/ ⽣成⽂件
template.process(dataMap, out);
// 关闭流
out.flush();
out.close();
return filePath + File.separator + fileName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package st1;
import java.io.IOException;
SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import se.web.Invocation;
import se.web.annotation.Path;
import se.st.Get;
import org.apachemons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import com.wtmon.util.CommonsUtil;
import com.wtmon.util.Constants;
import com.wtmon.util.ResponseObject;
import com.wtmon.util.WordUtil;
import com.wt.service.download.DownloadService;
/**
@Desc:⽣成word
@Author: