java 导出word

 1 利用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);

WordUtil 

package com.stsoft.learning.controller.exam;

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. POI

//新建一个文档  
              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 (适合输入数据含格式)

    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()+"&nbsp;&nbsp;&nbsp;总分:"+
                         vo.getScoreTotal()+"&nbsp;&nbsp;&nbsp;通过分:"+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()
原文地址:https://www.cnblogs.com/dingding0505/p/4250676.html