Java ${}修改world内容 并转pdf,亲测可以

@world转pdf

前期准备工作:

1.先将world要修改的内容用 $ 表示
2.将world 另存为xml,再将xml文件后缀名改为.ftl
3.将用编辑器打开ftl文件,将修改的内容用${name}补充完整

java代码编写

要修改的内容参数

private static Map<String, Object> initWordMap() {
        Map<String, Object> wordData = new HashMap<String, Object>();
        wordData.put("contractId", "ht00011");
        wordData.put("person", "作者1111");
        wordData.put("userName", "张三22222");
        wordData.put("account", "账号001");
        wordData.put("sYear", "2022");
        wordData.put("sMonth", "10");
        wordData.put("sDay", "23");
        wordData.put("eYear", "2032");
        wordData.put("eMonth", "10");
        wordData.put("eDay", "23");
        wordData.put("bigMoney", "贰仟");
        wordData.put("smellMoney", "27890");
        wordData.put("signYear", "2022");
        wordData.put("signMonth", "11");
        wordData.put("signDay", "16");
        return wordData;
    }

** 修改world内容**

public static void downloadWord(Map<String, Object> wordData) throws Exception {
        String docName = "";
        Writer writer = null;
        String wordModePath = "F:\\";
        String wordModeFile ="财务合同.ftl";
        // 2. 设置配置内容
        // 设置版本
        Configuration configuration = new Configuration(new Version(VERSION));
        // 指定加载Word模板的路径
        configuration.setDirectoryForTemplateLoading(new File(wordModePath));
        // 以UTF-8的编码格式,读取模板文档
        Template template = configuration.getTemplate(wordModeFile, CHARSET);
        String contractId = wordData.get("contractId").toString();//合同编号
        // 3. 输出文档路径及名称
        docName = wordModePath+contractId+"_"+System.currentTimeMillis()  + ".doc";
        File outFile = new File(docName);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), CHARSET), 10240);
        // 输出
        template.process(wordData, writer);
        writer.flush();
        writer.close();
        System.out.println(docName+":导出已完成");
        String pdfName = wordModePath+contractId+"_"+System.currentTimeMillis()  + ".pdf";
        worldToPdf(docName,pdfName);
    }

world 转pdf
path:world 文件的存放路径
path2: pdf文件的存放路径

private static Boolean worldToPdf(String path,String path2) {
        Boolean flag = false;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
            PdfOptions pdfOptions = PdfOptions.create();
            fileOutputStream = new FileOutputStream(path2);
            PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
            flag = true;
            System.out.println("生成成功!"+path2);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if(fileInputStream != null) fileInputStream.close();
                if(fileOutputStream != null) fileOutputStream.close();
            }catch (IOException e){e.printStackTrace();}
        }
        return flag;
    }

全部代码

{

    /**
     * 设置使用的编码格式
     */
    private static final String CHARSET = "UTF-8";
    /**
     * 设置使用的版本
     */
    private static final String VERSION = "2.3.0";

    public static void main(String[] args) throws Exception{
        //将world转为pdf
        downloadWord("F:\\","财务合同。ftl");
    }

    public static void downloadWord(String wordModePath,String wordModeFile) throws Exception {
        String docName = "";
        Writer writer = null;
        // 2. 设置配置内容
        // 设置版本
        Configuration configuration = new Configuration(new Version(VERSION));
        // 指定加载Word模板的路径
        configuration.setDirectoryForTemplateLoading(new File(wordModePath));
        // 以UTF-8的编码格式,读取模板文档
        Template template = configuration.getTemplate(wordModeFile, CHARSET);
        // 3. 输出文档路径及名称
        docName = wordModePath+"_"+System.currentTimeMillis()  + ".doc";
        File outFile = new File(docName);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), CHARSET), 10240);
        // 输出
        template.process(initWordMap(), writer);
        writer.flush();
        writer.close();
        System.out.println(docName+":导出已完成");
        String pdfName = wordModePath+System.currentTimeMillis()  + ".pdf";
        worldToPdf(docName,pdfName);
    }


    private static Boolean worldToPdf(String path,String path2) {
        Boolean flag = false;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
            PdfOptions pdfOptions = PdfOptions.create();
            fileOutputStream = new FileOutputStream(path2);
            PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
            flag = true;
            System.out.println("生成成功!"+path2);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if(fileInputStream != null) fileInputStream.close();
                if(fileOutputStream != null) fileOutputStream.close();
            }catch (IOException e){e.printStackTrace();}
        }
        return flag;
    }

    private static Map<String, Object> initWordMap() {
        Map<String, Object> wordData = new HashMap<String, Object>();
        wordData.put("contractId", "ht00011");
        wordData.put("person", "作者1111");
        wordData.put("userName", "张三22222");
        wordData.put("account", "账号001");
        wordData.put("sYear", "2022");
        wordData.put("sMonth", "10");
        wordData.put("sDay", "23");
        wordData.put("eYear", "2032");
        wordData.put("eMonth", "10");
        wordData.put("eDay", "23");
        wordData.put("bigMoney", "贰仟");
        wordData.put("smellMoney", "27890");
        wordData.put("signYear", "2022");
        wordData.put("signMonth", "11");
        wordData.put("signDay", "16");
        return wordData;
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值