java 导出word几种方法总结

来源:互联网 发布:30岁没结婚的女人 知乎 编辑:程序博客网 时间:2024/05/18 13:47

       最近项目里要求从后台获取试题然后生成一张试卷导出word。开始是利用xml 由freemark模版生成,但是由于数据库里存了来自富文本框带html格式的数据,此部分样式无法生成。只好先生成html,再导出word。以下是我用到几种导出word的方法。

1.利用freemark模版生成

    如果不是从数据库取出的数据本身带html格式的话,这种方法是比较推荐的,适用生成格式比较复杂的情况。先在word里画好你的模版,然后另存为xml格式,再将里面的内容用freemark标签代替。
   
             PaperToWordVo vo=new PaperToWordVo();     vo=paperResservice.getPaperInfo((String)session.getAttribute("dataowner"),paperId,vo);response.setHeader("Content-Disposition","attachment;filename="+new String((vo.getPaperTitle()+".doc").getBytes("UTF-8"),"ISO8859-1"));response.setContentType("text/html;charset=UTF-8");response.setCharacterEncoding("UTF-8");        WordUtil handler = new WordUtil();         Writer out;out = response.getWriter();                        handler.write("/com/stsoft/learning/model", "test.xml", vo, out); //其中test.xml 就是word生成预先修改好的模版

WordUtil.java

import java.io.IOException;import java.io.Writer;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.Template;public class WordUtil {    private Configuration configuration = null;    public WordUtil() {        try {            configuration = new Configuration();            configuration.setDefaultEncoding("UTF-8");        } catch (Exception e) {            System.out.println(e.getMessage());            e.printStackTrace();        }    }    private Template getTemplate(String templatePath, String templateName)            throws IOException {        configuration.setClassForTemplateLoading(this.getClass(), templatePath);        Template t = configuration.getTemplate(templateName);        t.setEncoding("UTF-8");        return t;    }    public void write(String templatePath, String templateName,            PaperToWordVo dataMap, Writer out) {        try {            Template t = getTemplate(templatePath, templateName);            t.process(dataMap, out);            out.close();        } catch (Exception e) {            System.out.println(e.getMessage());            e.printStackTrace();        }    }}

2.利用Apache POI 

   POI在生成excel时优势更突出,POI生成word 由你自己在后台写出格式,当然也可以利用模版。
  下面是一个很简单的例子,具体可以看官网
  
             //新建一个文档                XWPFDocument doc = new XWPFDocument();              //创建一个段落                XWPFParagraph para = doc.createParagraph();                               //一个XWPFRun代表具有相同属性的一个区域。                XWPFRun run = para.createRun();                run.setBold(true); //加粗                run.setText(vo.getPaperTitle());                    run.addBreak();              run = para.createRun();                //run.setColor("FF0000");                List<QusetionTypeVo> tl=vo.getTypeList();              for(QusetionTypeVo ty:tl){                    run.setText("第"+ty.getQtypeNO()+"部分 "+ty.getQtypeTitle());                     List<QuestionVo>ql=ty.getQuestionList();                     for(QuestionVo que:ql){                         run.setText("第"+que.getQuestionNo()+"题、 "+que.getQuestionTitle());                              run.addBreak();                     }                    run.addBreak();              }

3.先生成html再导出word (适合取出的数据含html格式)

             //将你要展示的内容转换成html  再将其转word  其中也利用到POI 
                 String content = "<html> <head> </head><div style=\"text-align: center\">" +                         "<span style=\"font-size: 28px\">"                            +vo.getPaperTitle()+"</span> <br> <span style=\"font-size: 13px\">考试时间:"+vo.getTimeLimit()+"   总分:"+                         vo.getScoreTotal()+"   通过分:"+vo.getScorePass()+"</span> </div><br><br>"+typeDiv.toString()+"</html>";
                 byte b[] = content.getBytes();                   ByteArrayInputStream bais = new ByteArrayInputStream(b);                   POIFSFileSystem poifs = new POIFSFileSystem();                   DirectoryEntry directory = poifs.getRoot();                   DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);                   OutputStream ostream = response.getOutputStream();                 poifs.writeFilesystem(ostream);                   bais.close();                   ostream.close()

时间有限,讲的很粗略,待完善...


0 0
原创粉丝点击