700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java实现word文档的导出

java实现word文档的导出

时间:2023-04-03 18:25:04

相关推荐

java实现word文档的导出

第一步就是先建一个world文档,然后将World文档里面需要从数据库或者其他方式想要填充的部分用占位符替换

第二步:就是将此文档选择xml文件格式保存

第三步:随便在eclipse中找个位置放下,并右键properties属性,将其编码格式设置为Utf-8,本质上就是想办法将文件的编码格式改为utf-8(防止生成之后乱码)

第四步:将此文件后缀名改为ftl

第五步:需要导入freemarker相关jar包

传送门:/artifact/freemarker/freemarker

第六步:代码如下:小测试

public class App

{

public static void main( String[] args ) throws IOException, TemplateException

{

//1.创建配置类

Configuration configuration = new Configuration(Configuration.getVersion());

//2.设置模板所在的目录

configuration.setDirectoryForTemplateLoading(new File(“C:\Users\admin\Desktop\world”));

//3.设置字符集

configuration.setDefaultEncoding(“utf-8”);

//4.加载模板;

Template template = configuration.getTemplate(“username.ftl”);

//5.创建数据模型

Map map=new HashMap();

map.put(“username”, "吉川条野 ");

map.put(“password”,“666666”);

//6.创建 Writer 对象

Writer out =new FileWriter(new File(“C:\Users\admin\Desktop\world\bug.doc”));

//7.保存数据

template.process(map, out);

//8.关闭 Writer 对象

out.close();

}

}

结果如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。