一、业务逻辑的要求
我们要实现在界面上的word导出
需要把一些特定的数据数据导出word中
二、实现
1、先建立一个word模板,先用一些汉字占位。方便以后修改xml
把word文件另存为xml文件,然后再把xml文件改成ftl文件
用Notepad++打开,编辑
2、填写数据
这里我们用的是汇总表的数据作为数据进行导出
我们首先把这些通过学生id查到的学生信息先用HashMap存起来
3、固定板块
一些固定的的位置我们直接就可以加进去
4、拿到数据
下面是评分标准和分数一些的数据
我们先新建几个数组,把对应位置的数据放进取,这样一个L代表一个区域
像l4,l10这样的是一定只有一个的数据(总分)所以直接把它们的AllVaue值存存进map
就可以拿到数据
L1中list类型的是一串数据
5、调用方法
防止服务器路径暴露在前端 用的是服务器IP+ 映射路径地址
6、wordExport方法
public class WordExport {
public static String wordExport(HashMap<String,Object> dataMap) throws Exception {
/** 初始化配置文件 加载了frimank的一个版本**/
Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
/** 设置编码 **/
configuration.setDefaultEncoding("utf-8");
/** 我的ftl文件是放在D盘的**/
String fileDirectory = "D:/zPerson/niuaniu/word/";
/** 加载资源路径**/
configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
/** 加载模板 **/
Template template = configuration.getTemplate("real.ftl");
/** 准备数据 **/
// Map<String, Object> dataMap = new HashMap<>();
/** 在ftl文件中有${textDeal}这个标签**/
// dataMap.put("name", "syh");
// dataMap.put("department", "骨科666");
// dataMap.put("image", WordDocExportTest.getImageStr());
// dataMap.put("guidence_content", new int[]{0, 2});
/** 指定输出word文件的路径 拿到profile的绝对路径 **/
String outFilePath = NiuaConfig.getProfile()+"word/"+dataMap.get("name")+".docx";
//String outFilePath = "D:/zPerson/niuaniu/word/考核成绩表.docx";
//相应的输出,给设定的真实文件路径的文件并生成文件
File docFile = new File(outFilePath);
FileOutputStream fos = new FileOutputStream(docFile);
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
template.process(dataMap, out);
//得到映射路径地址 /profile
String path = Constants.RESOURCE_PREFIX +"/"+"word/"+dataMap.get("name")+".docx";
if (out != null) {
out.close();
}
return path;
}
}
前台数据
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115351.html