当前位置: 移动技术网 > IT编程>开发语言>Java > springMVC导出word模板的方法

springMVC导出word模板的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了springmvc导出word模板的具体代码,供大家参考,具体内容如下

controller 调用

@requestmapping(value = "/exportword")
public void exportword(httpservletresponse response, httpservletrequest request) throws ioexception {
  string templatepath = request.getservletcontext().getrealpath("") + "/template/税源信息比对.docx"; 
  string filename = new string("税源信息比对".getbytes("gb2312"), "iso8859-1") + ".docx";
  /*数据*/
  map<string, object> params = new hashmap<string, object>(); 
  params.put("${name}", "aaaa"); 
  params.put("${sex}", "bbbb");

  templewordutil wordutil = new templewordutil();

  xwpfdocument doc; 
  inputstream is = new fileinputstream(templatepath);
 // is = getclass().getclassloader().getresourceasstream(templatepath); 
  doc = new xwpfdocument(is);  //只能使用.docx的

  wordutil.replaceinpara(doc, params); 
  //替换表格里面的变量 
  wordutil.replaceintable(doc, params); 
  outputstream os = response.getoutputstream(); 

  response.setcontenttype("application/vnd.ms-excel"); 
  response.setheader("content-disposition", "attachment;filename=" + filename); 

  doc.write(os); 

  wordutil.close(os); 
  wordutil.close(is); 

  os.flush(); 
  os.close(); 

}

templewordutil 工具类

import org.apache.poi.xwpf.usermodel.*; 
  
import java.io.*; 
import java.util.iterator; 
import java.util.list; 
import java.util.map; 
import java.util.regex.matcher; 
import java.util.regex.pattern; 
 
/**
 * 写入word工具类
 * @author z
 *
 */
public class templewordutil {
 
  /** 
   * 替换段落里面的变量 
   * 
   * @param doc  要替换的文档 
   * @param params 参数,导入的数据 
   */ 
  public void replaceinpara(xwpfdocument doc, map<string, object> params) { 
    iterator<xwpfparagraph> iterator = doc.getparagraphsiterator(); 
    xwpfparagraph para; 
    while (iterator.hasnext()) { 
      para = iterator.next(); 
      this.replaceinpara(para, params); 
    } 
  } 
  
  /** 
   * 替换段落里面的变量 
   * 
   * @param para  要替换的段落 
   * @param params 参数 
   */ 
  public void replaceinpara(xwpfparagraph para, map<string, object> params) { 
    list<xwpfrun> runs; 
    //matcher matcher; 
    if (this.matcher(para.getparagraphtext()).find()) { 
      runs = para.getruns(); 
  
      int start = -1; 
      int end = -1; 
      string str = ""; 
      for (int i = 0; i < runs.size(); i++) { 
        xwpfrun run = runs.get(i); 
        string runtext = run.tostring(); 
        if ('$' == runtext.charat(0)&&'{' == runtext.charat(1)) { 
          start = i; 
        } 
        if ((start != -1)) { 
          str += runtext; 
        } 
        if ('}' == runtext.charat(runtext.length() - 1)) { 
          if (start != -1) { 
            end = i; 
            break; 
          } 
        } 
      } 
  
      for (int i = start; i <= end; i++) { 
        para.removerun(i); 
        i--; 
        end--; 
      } 
  
      for (string key : params.keyset()) { 
        if (str.equals(key)) { 
          para.createrun().settext((string) params.get(key)); 
          break; 
        } 
      } 
  
  
    } 
  } 
  
  /** 
   * 替换表格里面的变量 
   * 
   * @param doc  要替换的文档 
   * @param params 参数 
   */ 
  public void replaceintable(xwpfdocument doc, map<string, object> params) { 
    iterator<xwpftable> iterator = doc.gettablesiterator(); 
    xwpftable table; 
    list<xwpftablerow> rows; 
    list<xwpftablecell> cells; 
    list<xwpfparagraph> paras; 
    while (iterator.hasnext()) { 
      table = iterator.next(); 
      rows = table.getrows(); 
      for (xwpftablerow row : rows) { 
        cells = row.gettablecells(); 
        for (xwpftablecell cell : cells) { 
          paras = cell.getparagraphs(); 
          for (xwpfparagraph para : paras) { 
            this.replaceinpara(para, params); 
          } 
        } 
      } 
    } 
  } 
  
  /** 
   * 正则匹配字符串 
   * 
   * @param str 
   * @return 
   */ 
  private matcher matcher(string str) { 
    pattern pattern = pattern.compile("\\$\\{(.+?)\\}", pattern.case_insensitive); 
    matcher matcher = pattern.matcher(str); 
    return matcher; 
  } 
  
  /** 
   * 关闭输入流 
   * 
   * @param is 
   */ 
  public void close(inputstream is) { 
    if (is != null) { 
      try { 
        is.close(); 
      } catch (ioexception e) { 
        e.printstacktrace(); 
      } 
    } 
  } 
  
  /** 
   * 关闭输出流 
   * 
   * @param os 
   */ 
  public void close(outputstream os) { 
    if (os != null) { 
      try { 
        os.close(); 
      } catch (ioexception e) { 
        e.printstacktrace(); 
      } 
    } 
  } 
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网