乔山办公网我们一直在努力
您的位置:乔山办公网 > office365 > java 调用word模板实现循环套打生成word文档-微软 office docx xml 用java 哪个解析,of

java 调用word模板实现循环套打生成word文档-微软 office docx xml 用java 哪个解析,of

作者:乔山办公网日期:

返回目录:office365


如果你经常编辑的Word 文档具有相似的格式或版面,那么使用模板会简化许多操作,省
去一些重复的步骤,大大提高工作效率。
模板是一个用来创建文档的大概模型,一个模板一般包含的信息有:本的格式信息,样式,
内容,宏按钮,自定义工具栏、宏、快捷键,自动图文集词条等。Word 默认情况下包含多种
模板,选择“文件”菜单下的“新键”命令,就会弹出新键对话框,其中就是包含的各种模板,
既包括许多中文模板,也包括英文向导模板。
当Word 自带的模板不能满足我们的需要时,我们可以e799bee5baa6e78988e69d83366自己建立模板,一般情况有两种创
建模板的方法。一种是,选择“文件”菜单下的“新建”选项,在弹出的对话框中选中“模板”
项,在其中加入模板所需信息,然后保存。另一种方法是,打开一篇已经编辑好的文档,并将
该文档另存为文档模板。
当每次进入Word 时,系统都会自动新建一个空白文档,并分配一个名称为“文档1”的
文档。其实大多数情况下,其默认设置比如说字体、字号、纸型等都不满足我们的需要。有没
有一种方法让其打开的就是我们经常使用的设置呢,答案是肯定的,可以利用Word 的模板功
能来实现。其实我们新建一个文档时,实际上是打开了一个名为“normal.doc”的文件。该文
件的位置一般位于:C:WindowsApplication DataMicrosoftTemplates 文件夹下,具体操作方
法如下:打开“normal.doc”文档,将各项设置改成自己所需的样式,点击文件下拉菜单中的
“另存为”,在“另存为”对话框中输入要保存的文件名,保存类型选择“文档模板”,单击
“确定”就可以了。以后每打开这一文件时,它的默认格式就是刚才设置的那样了。
一个模板并不一定是自己使用,或许是一个公司的所有人使用,这样,并不是所有人都能
看懂所有意思,在需要输入数据的地方加入一些提示信息一定会给使用者带来方便。其实这是
可以通过“域”来实现的,在提示用户的同时,还可以预先设置文字的字体、字号等。具体的
操作步骤如下:按下 Ctrl+F9 组合键,插入一对标明域代码的花括号{}; 在花括号之间键
入“MacroButton NoMacro [单击此处输入姓名]”; 对插入的域和文字进行必要的格式设置;
在域上方单击鼠标右键,并选择“切换域代码”。这样,当单击提示时提示就处于被选中状态,
输入新的文字将替换提示信息。

1,模版里做循环,需要循环的地方 在模版里加入 <#list reportListas a > </#list> 编辑好。
2,代码里
Map<String,Object> resMap = new HashMap<>();
resMap.put("reportList", list);
t.process(resMap,out);
  1. 首先打开word,然后点击左上角的文件按钮,选择新建

  2. 在新建文档中选择已安装的word模板,然后根据需要选择相应的模板,最后点击创建



先下载jacob_1.10.1.zip。
解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。
将jacob.jar加入项目。

/*
* Java2word.java
*
* Created on 2007年8月13日, 上午10:32
*
* To
change this template, choose Tools | Template Manager
* and open the template
in the editor.
*/

/*
* 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。
*
word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……,
以免执行错误的替换。
*
所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、
.jpg、.gif)。
*
要替换表格e799bee5baa6e997aee7ad94e58685e5aeb9361中的数据时,HashMap中的Key格式为“table$R@N”,其中:R代表从表格的第R行开始替换,N代表
word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代
表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、
第五列的数据,则第一条记录为String[3]
{“1”,”3”,”5”}。
*/

package com.word.util;

/**
*
* @author kdl
*/
import java.util.ArrayList;
import
java.util.HashMap;
import java.util.Iterator;

import com.jacob.activeX.ActiveXComponent;
import
com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Java2word {

private boolean saveOnExit;

/**
* word文档
*/
Dispatch doc = null;

/**
* word运行程序对象s
*/
private ActiveXComponent
word;
/**
* 所有word文档
*/
private Dispatch
documents;

/**
* 构造函数
*/

public Java2word() {
if(word==null){
word = new
ActiveXComponent("Word.Application");
word.setProperty("Visible",new
Variant(false));
}
if(documents==null)

documents = word.getProperty("Documents").toDispatch();
saveOnExit =
false;
}

/**
* 设置参数:退出时是否保存
* @param
saveOnExit boolean true-退出时保存文件,false-退出时不保存文件
*/
public void
setSaveOnExit(boolean saveOnExit) {
this.saveOnExit =
saveOnExit;
}
/**
* 得到参数:退出时是否保存
* @return
boolean true-退出时保存文件,false-退出时不保存文件
*/
public boolean
getSaveOnExit() {
return saveOnExit;
}

/**
* 打开文件
* @param inputDoc String 要打开的文件,全路径
*
@return Dispatch 打开的文件
*/
public Dispatch open(String inputDoc)
{
return
Dispatch.call(documents,"Open",inputDoc).toDispatch();
//return
Dispatch.invoke(documents,"Open",Dispatch.Method,new Object[]{inputDoc},new
int[1]).toDispatch();
}

/**
* 选定内容
*
@return Dispatch 选定的范围或插入点
*/
public Dispatch select()
{
return word.getProperty("Selection").toDispatch();

}

/**
* 把选定内容或插入点向上移动
* @param selection
Dispatch 要移动的内容
* @param count int 移动的距离
*/
public
void moveUp(Dispatch selection,int count) {
for(int i = 0;i <
count;i ++)
Dispatch.call(selection,"MoveUp");

}

/**
* 把选定内容或插入点向下移动
* @param selection
Dispatch 要移动的内容
* @param count int 移动的距离
*/
public
void moveDown(Dispatch selection,int count) {
for(int i = 0;i <
count;i ++)
Dispatch.call(selection,"MoveDown");

}

/**
* 把选定内容或插入点向左移动
* @param selection
Dispatch 要移动的内容
* @param count int 移动的距离
*/
public
void moveLeft(Dispatch selection,int count) {
for(int i = 0;i <
count;i ++) {
Dispatch.call(selection,"MoveLeft");

}
}

/**
* 把选定内容或插入点向右移动
* @param
selection Dispatch 要移动的内容
* @param count int 移动的距离
*/

public void moveRight(Dispatch selection,int count) {
for(int i =
0;i < count;i ++)

Dispatch.call(selection,"MoveRight");
}

/**
*
把插入点移动到文件首位置
* @param selection Dispatch 插入点
*/
public
void moveStart(Dispatch selection) {

Dispatch.call(selection,"HomeKey",new Variant(6));
}

/**
* 从选定内容或插入点开始查找文本
* @param selection Dispatch
选定内容
* @param toFindText String 要查找的文本
* @return boolean
true-查找到并选中该文本,false-未查找到文本
*/
public boolean find(Dispatch
selection,String toFindText) {
//从selection所在位置开始查询

Dispatch find = word.call(selection,"Find").toDispatch();

//设置要查找的内容
Dispatch.put(find,"Text",toFindText);

//向前查找
Dispatch.put(find,"Forward","True");

//设置格式
Dispatch.put(find,"Format","True");

//大小写匹配
Dispatch.put(find,"MatchCase","True");

//全字匹配
Dispatch.put(find,"MatchWholeWord","True");

//查找并选中
return Dispatch.call(find,"Execute").getBoolean();

}

/**
* 把选定内容替换为设定文本
* @param selection
Dispatch 选定内容
* @param newText String 替换为文本
*/
public
void replace(Dispatch selection,String newText) {

//设置替换文本
Dispatch.put(selection,"Text",newText);

}

/**
* 全局替换
* @param selection Dispatch
选定内容或起始插入点
* @param oldText String 要替换的文本
* @param newText
String 替换为文本
*/
public void replaceAll(Dispatch
selection,String oldText,Object replaceObj) {
//移动到文件开头

moveStart(selection);

if(oldText.startsWith("table") ||
replaceObj instanceof ArrayList)

replaceTable(selection,oldText,(ArrayList) replaceObj);
else
{
String newText = (String) replaceObj;

if(newText==null)
newText="";

if(oldText.indexOf("image") != -1&!newText.trim().equals("") ||
newText.lastIndexOf(".bmp") != -1 || newText.lastIndexOf(".jpg") != -1 ||
newText.lastIndexOf(".gif") != -1){

while(find(selection,oldText)) {

replaceImage(selection,newText);

Dispatch.call(selection,"MoveRight");
}

}else{
while(find(selection,oldText))
{
replace(selection,newText);

Dispatch.call(selection,"MoveRight");
}

}
}
}

/**
* 替换图片
* @param
selection Dispatch 图片的插入点
* @param imagePath String 图片文件(全路径)

*/
public void replaceImage(Dispatch selection,String imagePath)
{

Dispatch.call(Dispatch.get(selection,"InLineShapes").toDispatch(),"AddPicture",imagePath);

}

/**
* 替换表格
* @param selection Dispatch
插入点
* @param tableName String
表格名称,形如table$1@1、table$2@1...table$R@N,R代表从表格中的第N行开始填充,N代表word文件中的第N张表

* @param fields HashMap 表格中要替换的字段与数据的对应表
*/
public void
replaceTable(Dispatch selection,String tableName,ArrayList dataList)
{
if(dataList.size() <= 1) {

System.out.println("Empty table!");
return;

}

//要填充的列
String[] cols = (String[])
dataList.get(0);

//表格序号
String tbIndex =
tableName.substring(tableName.lastIndexOf("@") + 1);

//从第几行开始填充
int fromRow =
Integer.parseInt(tableName.substring(tableName.lastIndexOf("$") +
1,tableName.lastIndexOf("@")));
//所有表格
Dispatch tables =
Dispatch.get(doc,"Tables").toDispatch();
//要填充的表格

Dispatch table = Dispatch.call(tables,"Item",new
Variant(tbIndex)).toDispatch();
//表格的所有行
Dispatch rows =
Dispatch.get(table,"Rows").toDispatch();
//填充表格
for(int
i = 1;i < dataList.size();i ++) {
//某一行数据

String[] datas = (String[]) dataList.get(i);

//在表格中添加一行
if(Dispatch.get(rows,"Count").getInt() < fromRow +
i - 1)
Dispatch.call(rows,"Add");

//填充该行的相关列
for(int j = 0;j < datas.length;j ++)
{
//得到单元格
Dispatch cell =
Dispatch.call(table,"Cell",Integer.toString(fromRow + i -
1),cols[j]).toDispatch();
//选中单元格

Dispatch.call(cell,"Select");
//设置格式

Dispatch font = Dispatch.get(selection,"Font").toDispatch();

Dispatch.put(font,"Bold","0");

Dispatch.put(font,"Italic","0");
//输入数据

Dispatch.put(selection,"Text",datas[j]);
}
}

}

/**
* 保存文件
* @param outputPath String
输出文件(包含路径)
*/
public void save(String outputPath) {

Dispatch.call(Dispatch.call(word,"WordBasic").getDispatch(),"FileSaveAs",outputPath);

}

/**
* 关闭文件
* @param document Dispatch
要关闭的文件
*/
public void close(Dispatch doc) {

Dispatch.call(doc,"Close",new Variant(saveOnExit));

word.invoke("Quit",new Variant[]{});
word = null;

}

/**
* 根据模板、数据生成word文件
* @param inputPath
String 模板文件(包含路径)
* @param outPath String 输出文件(包含路径)
* @param
data HashMap 数据包(包含要填充的字段、对应的数据)
*/
public void toWord(String
inputPath,String outPath,HashMap data) {
String oldText;

Object newValue;
try {
if(doc==null)

doc = open(inputPath);

Dispatch selection =
select();

Iterator keys =
data.keySet().iterator();
while(keys.hasNext())
{
oldText = (String) keys.next();

newValue = data.get(oldText);

replaceAll(selection,oldText,newValue);

}

save(outPath);
} catch(Exception
e) {
System.out.println("操作word文件失败!");

e.printStackTrace();
} finally {
if(doc !=
null)
close(doc);
}
}

相关阅读

  • <em>Office</em> visio <em>2013</em>

  • 乔山办公网office365
  • 可能以前安装的office没卸干净,使用office专用卸载工具MicrosoftFixit把原来的office清理干净或者用微软自产卸载软件Windows Install Clean Up强力卸载原来安装的office,把C盘带有office的文件夹和文件
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网