1 . 实现代码
package com.base.pf.common.util;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.axis.encoding.Base64;
import com.base.pf.base.util.PropertiesMgr;
import com.base.pf.base.util.StringUtils;
/**
* 导出WORD
*
* @author ZHEN.L
* @date 2014.09.23
* @date 2014.11.05
*
*/
public class WordUtils {
/**
* 转换PDF格式
*/
public static final String WORD_2_PDF = "Word.pdf";
/**
* word导出标记
*/
public static final String WORD_EXP_TAG = "Word.exp";
/**
* word导出值
*/
public static final String WORD_EXP_TAG_VALUE = "word";
/**
* 模板名称标记
*/
public static final String WORD_TEMPLATE_NAME = "Word.fileName";
/**
* 模板名称标记
*/
public static final String WORD_FILE_NAME = "fileName";
/**
* 模板名称标记:动态字段
*/
public static final String WORD_FILE_NAME_FIELD = "FieldfileName";
// 模板路径
protected static final String WORD_TEMPLATE_PATH = "\\page\\project\\template_word\\";
/**
* word中换行:需要将word中\r\n转回为以下字符串 <w:spacing w:line="240" w:line-rule="auto"
* /> 240为单倍行距,360为1.5倍行距
*/
protected static final String LINE_FEED = "</w:t></w:r></w:p><w:p wsp:rsidR=\"004C3B63\" wsp:rsidRDefault=\"004C3B63\" wsp:rsidP=\"00302FB9\"><w:pPr><w:spacing w:line=\"340\" w:line-rule=\"auto\" /><w:rPr><w:rFonts w:ascii=\"宋体\" w:h-ansi=\"宋体\"/><wx:font wx:val=\"宋体\"/><w:sz w:val=\"24\"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:ascii=\"宋体\" w:h-ansi=\"宋体\" w:hint=\"fareast\"/><wx:font wx:val=\"宋体\"/><w:sz w:val=\"24\"/></w:rPr><w:t> ";
protected static final String PDF_PATH = PropertiesMgr
.getValue("exp_word_pdf_path") + "/";
/**
* 导出word
*
* @param response
*/
public synchronized static String exp(HttpServletRequest request,
HttpServletResponse response, Object obj) {
String xml = "";
String PDF = request.getParameter(WORD_2_PDF);
Map map = BeanToMapUtils.convertBean(obj);
String templatePath = request.getServletContext().getRealPath("/")
+ WORD_TEMPLATE_PATH;
String templateName = request.getParameter(WORD_TEMPLATE_NAME);
// 模板名称,程序中获取,通过DTO传递过来
if (map.get("templateName") != null) {
templateName = String.valueOf(map.get("templateName"));
}
File file = new File(templatePath + templateName);
String fileName = request.getParameter(WORD_FILE_NAME);
String fieldFileName = request.getParameter(WORD_FILE_NAME_FIELD);
if (!file.exists()) {
throw new NullPointerException("模板不存在!");
}
OutputStream os = null;
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
file), "UTF-8"));
String buf = null;
while ((buf = br.readLine()) != null) {
xml += buf;
}
// ContactDtoT dto = new ContactDtoT();
// if (!(obj instanceof Map))
// BeanUtils.copyProperties(obj, dto);
if (!StringUtils.isEmpty(fieldFileName)) {
String temp = String.valueOf(map.get(fieldFileName));
if (!StringUtils.isEmpty(temp)) {
fileName = temp.trim() + fileName;
}
}
xml = replace(xml, map);
if (!"word".equalsIgnoreCase(PDF)) {
File pdfFile = new File(PDF_PATH);
if (!pdfFile.exists()) {
pdfFile.mkdirs();
}
Outp