0
点赞
收藏
分享

微信扫一扫

joplin的安装和使用

我阿霆哥 2023-06-09 阅读 77

使用java代码的freemarker模板将JSP页面转换成word文档导出

使用java代码的freemarker模板将JSP页面转换成word文档导出

一、准备好freemarker模板,

我的模板是这样的

在这里插入图片描述

二、xml文件格式化

在word文档的模板准备好之后,点开它,把它另保存为**Word XML文档(*.xml)类型的文件,在使用富文本进行打开,本博主使用的是nodepad++**软件打开的,复制里面的文字之后,使用浏览器搜素xml格式化,如:

在这里插入图片描述

把xml文件里面的文字赋值到这里进行格式化,复制一个xml文件副本,把这个副本后缀改为.ftl,之后,使用nodepad++文件打开之后,把格式化后的文字粘贴到后缀文件为.ftl的文件里面,这个文件就是最终的模板文件了,

三、导入依赖

导入文件需要的依赖,本博主的依赖是

<dependency>
			<groupId>fr.opensagres.xdocreport</groupId>
			<artifactId>fr.opensagres.xdocreport.core</artifactId>
			<version>2.0.2</version>
		</dependency>
		<dependency>
			<groupId>fr.opensagres.xdocreport</groupId>
			<artifactId>fr.opensagres.xdocreport.document</artifactId>
			<version>2.0.2</version>
		</dependency>
		<dependency>
			<groupId>fr.opensagres.xdocreport</groupId>
			<artifactId>fr.opensagres.xdocreport.template</artifactId>
			<version>2.0.2</version>
		</dependency>
		<dependency>
			<groupId>fr.opensagres.xdocreport</groupId>
			<artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
			<version>2.0.2</version>
		</dependency>
		<dependency>
			<groupId>fr.opensagres.xdocreport</groupId>
			<artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
			<version>2.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.23</version>
		</dependency>

模板文件的位置:

在这里插入图片描述

四、主程序:

代码:

//这个是使用map集合来保存需要进行渲染的数据集的,
Map<String,Object> map = new HashMap<>();
//使用map.put来吧需要进行渲染的数据一以及数据的名称保存起来,在进行模板渲染

//在添加数据完成之后,

//这个是在
private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);

//这个是获取程序里模板文件以及模板文件
//ResourceUtils.CLASSPATH_URL_PREFIX 获取程序里面得模板所在得resource路径,这是我程序里面的工具类,需要自己去找到自己程序里面模板的这个路径
 File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/");
			//获取模板文件并且设置给configuration
            configuration.setDirectoryForTemplateLoading(file);
			//设置编码格式
            configuration.setDefaultEncoding("utf-8");

			//获取模板文件名称
            Template free = configuration.getTemplate("SummaryExportTemplate.ftl");
			//这个是中间文件的名字,这个名字是随便取得
            String testName = "test";

            File doc = null;
            FileInputStream ist = null;
            ServletOutputStream oust =null;
			try {
                doc = createDoc(map, free, testName);
                ist = new FileInputStream(doc);
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/x-download");
                wordTitle = new String(wordTitle.getBytes(), "ISO-8859-1");
                //使用浏览器下载
                response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(wordTitle)));
                oust = response.getOutputStream();
                byte[] buffer = new byte[512];// 缓冲区
                int bytesToRead = -1;
                while ((bytesToRead = ist.read(buffer)) !=-1){
                    oust.write(buffer,0,bytesToRead);
                }
            }finally {
                if (ist !=null){
                    ist.close();
                }
                if (oust != null){
                    oust.close();
                }
                if (doc !=null){
                    doc.delete();
                }
            }

private static File createDoc(Map<?, ?> dataMap, Template template, String name) {
        File f = new File(name);
        Template t = template;
        try {
            // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
            Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
            t.process(dataMap, w);
            w.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        return f;

    }

@Override
    public Results<?> companyDeclareSummaryWord(CompanyDeclareQuery query, HttpServletResponse response) {

        try {

            Map<String, Object> args = new HashMap<String, Object>();
            args.put("id", query.getId());
            //初审3
            args.put("scoreField", "q.ass1_score");
            
            CompanyDeclareTatalScoreVo tatal = companyDeclareMapper.getScoreTatalByDeclareIdAndScoreField(args);

            Map<String,Object> arge = new HashMap<>();
            arge.put("id",query.getId());
            CompanyDeclare declare = companyDeclareMapper.selectCompanyDeclare(arge);
            //文件名字
            String wordTitle = declare.getCompanyName()+"优秀企业评选主要指标计分表"+".docx";
            

            //查询资质得分信息
            CompanyDeclareQualQuery qualQuery = new CompanyDeclareQualQuery();
            qualQuery.setDeclareId(query.getId());
            List<CompanyDeclareQual> declareQualList = companyDeclareQualService.selectCompanyDeclareQuals(qualQuery).getData();
            tatal.setDeclareQualList(declareQualList);
            //保存数据得集合
            Map<String,Object> map = new HashMap<>();
            map.put("tatalqual2",tatal.getQualScore());
            Double tatalqual1 = (tatal.getQualScore() > 3 ? 3 :tatal.getQualScore());
            //保存数据
            map.put("tatalqual1",tatalqual1);




            File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/");
            configuration.setDirectoryForTemplateLoading(file);
            configuration.setDefaultEncoding("utf-8");


            Template free = configuration.getTemplate("SummaryExportTemplate.ftl");
            String testName = "test";
            File doc = null;
            FileInputStream ist = null;
            ServletOutputStream oust =null;
            try {

                doc = createDoc(map, free, testName);
                ist = new FileInputStream(doc);
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/x-download");
                wordTitle = new String(wordTitle.getBytes(), "ISO-8859-1");
                //使用浏览器下载
                response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(wordTitle)));
                oust = response.getOutputStream();
                byte[] buffer = new byte[512];// 缓冲区
                int bytesToRead = -1;
                while ((bytesToRead = ist.read(buffer)) !=-1){
                    oust.write(buffer,0,bytesToRead);
                }
            }finally {
                if (ist !=null){
                    ist.close();
                }
                if (oust != null){
                    oust.close();
                }
                if (doc !=null){
                    doc.delete();
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);

	//创建word文件以及编码
    private static File createDoc(Map<?, ?> dataMap, Template template, String name) {
        File f = new File(name);
        Template t = template;
        try {
            // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
            Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
            t.process(dataMap, w);
            w.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        return f;

    }

模板文件的路径:

在这里插入图片描述
**如果测试无效的可以联系博主进行寻求帮助!

举报

相关推荐

0 条评论