动态Word生成与java
来源:互联网 发布:苹果手机健身软件 编辑:程序博客网 时间:2024/06/06 00:09
本说明显示了如何使用简单的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; }}
阅读全文
0 0
- 动态Word生成与java
- Java动态生成复杂word
- Java 动态生成复杂 Word
- java 生成动态word文档
- Java 动态生成复杂 Word
- 动态生成WORD内容
- poi动态生成word
- java动态生成word和PDF的几种方案
- 基于Java模板技术动态生成Word文档
- C# 动态生成Word文档
- 用jawin动态生成word
- 实现动态生成Word文档
- 动态数据生成Word文档
- java通过jacob调用word (根据Word模板生成动态内容)
- 关于用java编写生成word文档,动态添加数据到word文档的一些心得
- java生成word文件
- java生成word文件
- Java生成word文档
- Linux彻底删除mysql5.6
- Java构造方法_动力节点Java学院整理
- 解决phpmailer可以在windows下面发送成功, 在linux下面失败的问题
- Apache Maven 3.5.0版详述
- redis主从复制时错误问题
- 动态Word生成与java
- Android Studio SVN配置忽略文件 (三)
- 流程图的介绍以及制作全过程技巧分享
- 快速幂 矩阵快速幂
- C++构造函数与析构函数中的“异常”
- python 不同方式读取文件速度不同
- 一步一步解析H.264码流的NALU(SPS,PSS,IDR)获取宽高和帧率
- android 属性动画之 ObjectAnimator
- java类加载器