java导出 world

来源:互联网 发布:node.js版本 编辑:程序博客网 时间:2024/06/13 10:04



上图:是Word文档中的内容,也就是模板,为了下面步鄹做铺垫,所以在需要输入数据的地方改成了拼音,


将word文档另存为xml文件.




接下来,上面写的拼音就起到作用了.

打开xml文件.搜索 title.

将Title 改为 ${title}

其他地方一样的修改。

改完后,把文件的后缀名直接改成ftl。


java代码:

1:

 try {
       //创建配置实例 
       Configuration configuration = new Configuration();
       //设置编码
        configuration.setDefaultEncoding("UTF-8");
         //ftl模板文件统一放至 com.lun.template 包下面
        configuration.setClassForTemplateLoading(World.class,"/com/test/world/");
         String templateName="test.ftl";
          //获取模板 
           Template template = configuration.getTemplate(templateName);
           String filePath="D:/";
           String fileName="test.doc";
           //输出文件
           File outFile = new File(filePath+File.separator+fileName);
           //如果输出目标文件夹不存在,则创建
           if (!outFile.getParentFile().exists()){
               outFile.getParentFile().mkdirs();
           }
           //将模板和数据模型合并生成文件 
           Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
           //导入数据
           Test1 tt=new Test1();
int id=2;
People pp= tt.getPeople(id);
Map<String,Object> dataMap=new HashMap<String,Object>();
   //** 放置数据 **//*
tt.makeExportProWorkOrderData(pp,dataMap); 
           //生成文件
           template.process(dataMap, out);           
           //关闭流
           out.flush();
           out.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

2:

public void makeExportProWorkOrderData(People p,Map<String, Object> dataMap) {
if(p.getId() != 0){
dataMap.put("id",p.getId());//id
}else {
dataMap.put("id","无");  
}
if(p.getName() != null){
dataMap.put("name",p.getName());//name
}else {
dataMap.put("name","无");  
}
if(p.getAge() != 0){
dataMap.put("age",p.getAge());//age
}else {
dataMap.put("age","无");  
}
}

0 0
原创粉丝点击