zoukankan      html  css  js  c++  java
  • java 模版式的 word

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    
    public class CreateWordTest { 
        // 参考 https://blog.csdn.net/chimaocai1302/article/details/100856467
        
        private Configuration configuration = null;
         
        public CreateWordTest() {
            configuration = new Configuration();
            configuration.setDefaultEncoding("utf-8");
        }
     
        /** * * @param dataMap 要填充的数据集合 * 
         * @param fileName 生成的word文档路劲与名称 * 
         * @throws UnsupportedEncodingException */
        public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException {
            System.out.println(dataMap);
            //读取模板   // /sdst4/src/com/jz/dzst/mail/zs_f.ftl
            configuration.setClassForTemplateLoading(this.getClass(), "/com/jz/dzst/mail");
            Template t=null;
            try {
                t = configuration.getTemplate("zs_f.ftl");
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            File outFile = new File(fileName);
            Writer out = null;
            FileOutputStream fos=null;
            try {
                fos = new FileOutputStream(outFile);
                //注意对流的编码
                OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");
                out = new BufferedWriter(oWriter); 
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
     
            try {
                t.process(dataMap, out);
                out.close();
                fos.close();
            } catch (TemplateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            CreateWordTest createWordTest =new CreateWordTest();
            // 太长 附件会变成  .bin 文件
            String file_name = "abc.doc";
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                createWordTest.createDoc(map, file_name);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    时刻告诉自己,自己是个菜鸡......
  • 相关阅读:
    JAVA中的SimpleDateFormat yyyy和YYYY的区别
    Mysql的MVCC
    SELECT语句中的for update的用法(锁的运用)
    今天简单说一下cdc 的使用
    sqlserver cdc用法
    JAVA | Java对象的内存分配过程是如何保证线程安全的?
    物联网(莹石云)WIFI一键配置原理分析(zz)
    Dell xps 13 9350待机时总是关机的处理方法
    Vue系列:在vux的popup组件中使用百度地图遇到显示不全的问题
    如何通过百度地图将经纬度转换为地址信息
  • 原文地址:https://www.cnblogs.com/mysterious-killer/p/14393546.html
Copyright © 2011-2022 走看看