zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    linux centos7 如何安装mysql
    Json转换 在java中的应用
    最最简单的spring mvc + Maven项目
    windows下 申请免费ssl证书的方法 (letsencrypt)
    PowerDesigner中Table视图同时显示Code和Name
    在linux中 部署 mongo 数据库服务端
    Java保存图片到数据库Blob格式
    MyBatis mapper记录
    vue防止多次点击,重复请求
    金额的单位转换,元转分
  • 原文地址:https://www.cnblogs.com/dingding0505/p/4250676.html
Copyright © 2011-2022 走看看