动态Word生成与java

本说明显示了如何使用简单的Java代码根据模板生成Word文档。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Hashtable;

/**
 * This code takes in a hashtable containing key fields required to populate
 * values into a Word template (XML) and output a Word document (also XML).
 * Template should contain ##KEY## fields for each hashtable key with same
 * name (without the ##s); the ##KEY## will be replaced by the value.
 * The main() method is written as an example.
 * Modified from code found at http://dinoch.dyndns.org:7070/WordML/AboutWordML.jsp
 * @author C. Peter Chen of http://dev-notes.com
 * @date 20080327
 */

public class msWordUtils {

    /**
     * This main() method is used for demonstration purposes only.
     * @param args
     * @author C. Peter Chen of http://dev-notes.com
     * @date 20080327
     */
    public static void main(String[] args) {
        String templatePathFilename = "c:\\word_template.xml";
        String outputPathFilename = "c:\\word_output.xml";

        Hashtable ht = new Hashtable();
        ht.put("INVOICENUMBER","384123");
        ht.put("CUSTOMERNAME","Some Company, LLC.");
        ht.put("ITEMNAME1","Coffee");
        ht.put("UNITPRICE1","1.50");
        ht.put("QTY1","1");
        ht.put("LINETOTAL1","1.50");
        ht.put("ITEMNAME2","Donut");
        ht.put("UNITPRICE2","1.00");
        ht.put("QTY2","2");
        ht.put("LINETOTAL2","2.00");
        ht.put("INVOICETOTAL","3.50");
        ht.put("DUEDATE","4/1/2008");

        generateWordDoc(ht, templatePathFilename, outputPathFilename);
    }

    /**
     * 
     * @param ht
     * @param templatePathFileName
     * @param outputPathFileName
     * @author C. Peter Chen of http://dev-notes.com
     * @date 20080327
     */
    public static void generateWordDoc(Hashtable ht, String templatePathFilename, String outputPathFilename) {  
        try {
            BufferedReader reader = new BufferedReader(new FileReader(templatePathFilename));

            File destination = new File(outputPathFilename);
            BufferedWriter writer = new BufferedWriter(new FileWriter(destination));

            String thisLine;
            int i = 0;

            while ((thisLine = reader.readLine()) != null) {
                System.out.println(i);

                for (java.util.Enumeration e = ht.keys(); e.hasMoreElements();) {
                    String name = (String) e.nextElement();
                    String value = ht.get(name).toString();
                    // Use this if we need to XML-encode the string in hashtable value...
                    thisLine = thisLine.replaceAll("##" + name.toUpperCase() + "##", XmlEncode(value));
                    // ... or this if we do not need to do XML-encode.
                    //thisLine= thisLine.replaceAll("##" + name.toUpperCase() + "##", value);
                }
                writer.write(thisLine);
                writer.newLine();
                i++;
            }
            writer.close();
            System.out.println("done");
        }
        catch (Exception e) {
            System.out.println("exception!=" + e);
        }
    }

    /**
     * Encodes regular text to XML.
     * @param text
     * @return string
     * @author http://dinoch.dyndns.org:7070/WordML/AboutWordML.jsp
     * @date 20050328
     */
    private static String XmlEncode(String text) {
        int[] charsRequiringEncoding = {38, 60, 62, 34, 61, 39};
        for(int i = 0; i < charsRequiringEncoding.length - 1; i++) {
            text = text.replaceAll(String.valueOf((char)charsRequiringEncoding[i]),"&#"+charsRequiringEncoding[i]+";");
        }
        return text; 
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生活中的思索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值