apache.poi包简单操作word文档

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.usermodel.Field;
import org.apache.poi.hwpf.usermodel.Fields;
import org.apache.poi.hwpf.usermodel.Range;

/**  * word文档操作工具 * Created by lichunlong on 2015/5/6 0006.  */ public class WordUtil {
    /**  * 修改word并另保存在本地 * @param map 需要修改的键值对 */ public static void writeAndSave(Map<String, String> map) {
        try {
            //读取word模板 String fileDir = new File("C:\\Users\\Administrator\\Desktop\\file").getCanonicalPath();
            FileInputStream inputStream = new FileInputStream(new File(fileDir+"\\template.doc"));
            HWPFDocument doc = new HWPFDocument(inputStream);
// Fields fields = doc.getFields(); // Iterator<Field> ite = fields.getFields(FieldsDocumentPart.MAIN).iterator(); // while(ite.hasNext()){ // System.out.println(ite.next().getType()); // }  //读取word文本内容 Range range = doc.getRange();
// System.out.println(range.text());  //替换文本内容 for (Map.Entry<String,String> entry : map.entrySet()) {
                range.replaceText(entry.getKey(), entry.getValue());
            }

            //输出字节流 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            String fileName = ""+System.currentTimeMillis();
            fileName += ".doc";
            FileOutputStream out = new FileOutputStream(fileDir+"\\"+fileName,true);
            doc.write(outputStream);
            out.write(outputStream.toByteArray());
            out.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**  * 修改word并提供下载 * @param request * @param response * @param map 需要修改的键值对 * @throws ServletException  * @throws IOException  */ public static void writeAndPost(HttpServletRequest request, HttpServletResponse response, Map<String, String> map) throws ServletException, IOException{
        try {

            //读取word模板文件 String fileDir = new File("C:\\Users\\Administrator\\Desktop\\file").getCanonicalPath();
            FileInputStream inputStream = new FileInputStream(new File(fileDir+"\\template.doc"));
            HWPFDocument doc = new HWPFDocument(inputStream);

            //替换读取到的word模板内容的指定字段 Range range = doc.getRange();
            for (Map.Entry<String,String> entry : map.entrySet()) {
                range.replaceText(entry.getKey(), entry.getValue());
            }

            //输出word内容文件流,提供下载 response.reset();
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Disposition", "attachment; filename=\"test.doc\"");
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ServletOutputStream servletOutputStream = response.getOutputStream();
            doc.write(outputStream);
            servletOutputStream.write(outputStream.toByteArray());
            servletOutputStream.flush();
            servletOutputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{

        }
    }
}

动态设置文件名,解决中文乱码问题:
response.setCharacterEncoding("utf-8");
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment; filename="+ new String(fileName.getBytes("utf-8"), "ISO8859-1"));

你可能感兴趣的:(apache.poi包简单操作word文档)