freemaker 入门

本文介绍如何在Maven项目中集成Freemarker模板引擎,包括创建项目、配置pom文件、准备模板文件和使用代码生成文档的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 新建maven项目

    2.导入pom文件 ,这里是我的pom文件,里面有freemaker的包

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>get</groupId>
	<artifactId>jar</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>jar</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-resources-plugin</artifactId>
			<version>2.6</version>
		</dependency>


		<!-- poi操作 -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.9</version>
		</dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>ooxml-schemas</artifactId>
			<version>1.1</version>
		</dependency>
		<!-- freemarker jar -->
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.22</version>
		</dependency>

	</dependencies>
</project>

 

  3.准备好模板文件

  • 在当前项目目录下新建模板文件文件夹(用来放模板文件)

  • 在该文件夹内新建test.docx文件,并且另存为test.xml

 

  • 编辑test.xml文件,标记要替换的地方

  • 将xml文件改成ftl文件

 插入代码,并且运行

package com.freemaker_test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException, TemplateException
    {
    	final Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    	File file = new File("");
    	String templateFilePath = file.getCanonicalPath() + "\\template_file";//模板文件路径
		String outFilePath = templateFilePath + "\\out\\out.doc";//输出doc文件路径
		cfg.setDirectoryForTemplateLoading(new File(templateFilePath));//从模板文件目录获取模板文件
		cfg.setDefaultEncoding("UTF-8");//一定要设置编码,如果编码不统一会导致乱码问题
		Template template = cfg.getTemplate("test.ftl", "UTF-8");
		Map<String, String> root = new HashMap<String, String>();
		root.put("TestData", "test_data");
		Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFilePath), "UTF-8"));
		template.process(root, out);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值