???紵 发表于 2015-11-13 15:07:58

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(&quot;C:\\Users\\Administrator\\Desktop\\file&quot;).getCanonicalPath();
FileInputStream inputStream = new FileInputStream(new File(fileDir&#43;&quot;\\template.doc&quot;));
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 = &quot;&quot;&#43;System.currentTimeMillis();
fileName &#43;= &quot;.doc&quot;;
FileOutputStream out = new FileOutputStream(fileDir&#43;&quot;\\&quot;&#43;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(&quot;C:\\Users\\Administrator\\Desktop\\file&quot;).getCanonicalPath();
FileInputStream inputStream = new FileInputStream(new File(fileDir&#43;&quot;\\template.doc&quot;));
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(&quot;application/x-msdownload&quot;);
response.addHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=\&quot;test.doc\&quot;&quot;);
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(&quot;utf-8&quot;);
response.setContentType(&quot;application/x-msdownload&quot;);
response.addHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot;&#43; new String(fileName.getBytes(&quot;utf-8&quot;), &quot;ISO8859-1&quot;));

版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: apache.poi包简单操作word文档