Java操作word⽂档使⽤JACOB和POI操作word,Excel,PPT需要的
jar包
可参考⽂档:
下载jar包
如上是jacob-1.17-M2.jar对应的jar包和dll⽂件....但是我在maven仓库中并没有发现jacob-1.17版本的.
所以如果使⽤maven项⽬的话推荐下载jacob-1.14版本的jar包和dll⽂件.
使⽤⽅式:
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import com.jacob.activeX.ActiveXComponent;
public class WriteDoc2 {
public static void main(String[] args) {
//在正式批量跑之前,做单个word⽂档的测试.
WordUtils util = new WordUtils(true);
util.openDocument("C:\\Users\\ABC\\Desktop\\test.docx");
util.setSaveOnExit(true);
util.insertText("xxx444dddd4x");
util.saveAs("C:\\Users\\ABC\\Desktop\\test.docx");
util.closeDocument();
}
}
对应WordUtils.java⼯具类,我是使⽤的如下:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.Dispatch;
import com.jacob.Variant;
public class WordUtils {
// word运⾏程序对象
private ActiveXComponent word;
// 所有word⽂档集合
private Dispatch documents;
// word⽂档
private Dispatch doc;
// 选定的范围或插⼊点
private Dispatch selection;
// 保存退出
private boolean saveOnExit;
public WordUtils(boolean visible) {
word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(visible));
documents = Property("Documents").toDispatch();
}
/**
* 设置退出时参数
*
* @param saveOnExit
*            boolean true-退出时保存⽂件,false-退出时不保存⽂件   
*/
public void setSaveOnExit(boolean saveOnExit) {
this.saveOnExit = saveOnExit;
}
/**
* 创建⼀个新的word⽂档
*/
public void createNewDocument() {
doc = Dispatch.call(documents, "Add").toDispatch();
selection = (word, "Selection").toDispatch();
}
/**
* 打开⼀个已经存在的word⽂档
*
* @param docPath
*/
public void openDocument(String docPath) {
doc = Dispatch.call(documents, "Open", docPath).toDispatch();
selection = (word, "Selection").toDispatch();
}
/**
* 打开⼀个有密码保护的word⽂档
* @param docPath
* @param password
*/
public void openDocument(String docPath, String password) {
doc = Dispatch.call(documents, "Open", docPath).toDispatch();
unProtect(password);
selection = (word, "Selection").toDispatch();
}
jfinal jar包下载/**
* 去掉密码保护
* @param password
*/
public void unProtect(String password){
try{
String protectionType = (doc, "ProtectionType").toString();
if(!"-1".equals(protectionType)){
Dispatch.call(doc, "Unprotect", password);
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 添加密码保护
* @param password
*/
public void protect(String password){
String protectionType = (doc, "ProtectionType").toString();
if("-1".equals(protectionType)){
Dispatch.call(doc, "Protect",new Object[]{new Variant(3), new Variant(true), password});
}
}
/**
* 显⽰审阅的最终状态
*/
public void showFinalState(){
Dispatch.call(doc, "AcceptAllRevisionsShown");
}
/**
* 打印预览:
*/
public void printpreview() {
Dispatch.call(doc, "PrintPreView");
}
/**
* 打印
*/
public void print(){
Dispatch.call(doc, "PrintOut");
}
public void print(String printerName) {
word.setProperty("ActivePrinter", new Variant(printerName));
print();
}
/**
* 指定打印机名称和打印输出⼯作名称
* @param printerName
* @param outputName
*/
public void print(String printerName, String outputName){
word.setProperty("ActivePrinter", new Variant(printerName));
Dispatch.call(doc, "PrintOut", new Variant[]{new Variant(false), new Variant(false), new Variant(0), new Variant(outputName)});    }
/**
* 把选定的内容或插⼊点向上移动
*
* @param pos
*/
public void moveUp(int pos) {
move("MoveUp", pos);
}
/**
* 把选定的内容或者插⼊点向下移动
*
* @param pos
*/
public void moveDown(int pos) {
move("MoveDown", pos);
}
/**
* 把选定的内容或者插⼊点向左移动
*
* @param pos
*/
public void moveLeft(int pos) {
move("MoveLeft", pos);
}
/**
* 把选定的内容或者插⼊点向右移动
*
* @param pos
*/
public void moveRight(int pos) {
move("MoveRight", pos);
}
/**
* 把选定的内容或者插⼊点向右移动
*/
public void moveRight() {
Dispatch.call(getSelection(), "MoveRight");
}
/**
* 把选定的内容或者插⼊点向指定的⽅向移动
* @param actionName
* @param pos
*/
private void move(String actionName, int pos) {
for (int i = 0; i < pos; i++)
Dispatch.call(getSelection(), actionName);
}
/**
* 把插⼊点移动到⽂件⾸位置
*/
public void moveStart(){
Dispatch.call(getSelection(), "HomeKey", new Variant(6));
}
/**
* 把插⼊点移动到⽂件末尾位置
*/
public void moveEnd(){
Dispatch.call(getSelection(), "EndKey", new Variant(6));
}
/**
* 插⼊换页符
*/
public void newPage(){
Dispatch.call(getSelection(), "InsertBreak");
}
public void nextPage(){
moveEnd();
moveDown(1);
}
public int getPageCount(){
Dispatch selection = (word, "Selection").toDispatch();
return Dispatch.call(selection,"information", new Variant(4)).getInt();    }
/**
* 获取当前的选定的内容或者插⼊点
* @return当前的选定的内容或者插⼊点
*/
public Dispatch getSelection(){
if (selection == null)
selection = (word, "Selection").toDispatch();
return selection;
}
/**
* 从选定内容或插⼊点开始查⽂本
* @param findText 要查的⽂本
* @return boolean true-查到并选中该⽂本,false-未查到⽂本
*/
public boolean find(String findText){
if(findText == null || findText.equals("")){
return false;
}
// 从selection所在位置开始查询
Dispatch find = Dispatch.call(getSelection(), "Find").toDispatch();
// 设置要查的内容
Dispatch.put(find, "Text", findText);
// 向前查
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 findText
* @param newText
* @return boolean true-查到并替换该⽂本,false-未查到⽂本
*/
public boolean replaceText(String findText, String newText){
moveStart();
if (!find(findText))
return false;
Dispatch.put(getSelection(), "Text", newText);
return true;
}
/**
* 进⼊页眉视图
*/
public void headerView(){
//取得活动窗体对象
Dispatch ActiveWindow = Property( "ActiveWindow").toDispatch();
//取得活动窗格对象
Dispatch ActivePane = (ActiveWindow, "ActivePane").toDispatch();
//取得视窗对象
Dispatch view = (ActivePane, "View").toDispatch();
Dispatch.put(view, "SeekView", "9");
}
/**
* 进⼊页脚视图
*/
public void footerView(){
//取得活动窗体对象
Dispatch ActiveWindow = Property( "ActiveWindow").toDispatch();
//取得活动窗格对象
Dispatch ActivePane = (ActiveWindow, "ActivePane").toDispatch();
//取得视窗对象
Dispatch view = (ActivePane, "View").toDispatch();
Dispatch.put(view, "SeekView", "10");
}
/**
* 进⼊普通视图
*/
public void pageView(){
//取得活动窗体对象
Dispatch ActiveWindow = Property( "ActiveWindow").toDispatch();
//取得活动窗格对象
Dispatch ActivePane = (ActiveWindow, "ActivePane").toDispatch();
//取得视窗对象
Dispatch view = (ActivePane, "View").toDispatch();
Dispatch.put(view, "SeekView", new Variant(0));//普通视图
}
/
**
* 全局替换⽂本
* @param findText
* @param newText
*/
public void replaceAllText(String findText, String newText){
int count = getPageCount();
for(int i = 0; i < count; i++){
headerView();
while (find(findText)){
Dispatch.put(getSelection(), "Text", newText);
moveEnd();
}
footerView();
while (find(findText)){
Dispatch.put(getSelection(), "Text", newText);
moveStart();
}
pageView();
moveStart();
while (find(findText)){
Dispatch.put(getSelection(), "Text", newText);
moveStart();
}
nextPage();
}
}
/**
* 全局替换⽂本
* @param findText
* @param newText
*/
public void replaceAllText(String findText, String newText, String fontName, int size){ /****插⼊页眉页脚*****/
//取得活动窗体对象
Dispatch ActiveWindow = Property( "ActiveWindow").toDispatch();
//取得活动窗格对象
Dispatch ActivePane = (ActiveWindow, "ActivePane").toDispatch();
//取得视窗对象
Dispatch view = (ActivePane, "View").toDispatch();
/****设置页眉*****/
Dispatch.put(view, "SeekView", "9");
while (find(findText)){
Dispatch.put(getSelection(), "Text", newText);
moveStart();
}
/****设置页脚*****/
Dispatch.put(view, "SeekView", "10");
while (find(findText)){
Dispatch.put(getSelection(), "Text", newText);
moveStart();
}
Dispatch.put(view, "SeekView", new Variant(0));//恢复视图
moveStart();
while (find(findText)){
Dispatch.put(getSelection(), "Text", newText);
putFontSize(getSelection(), fontName, size);
moveStart();
}
}
/**
* 设置选中或当前插⼊点的字体
* @param selection
* @param fontName
* @param size
*/
public void putFontSize(Dispatch selection, String fontName, int size){
Dispatch font = (selection, "Font").toDispatch();
Dispatch.put(font, "Name", new Variant(fontName));
Dispatch.put(font, "Size", new Variant(size));
}
/**
* 在当前插⼊点插⼊字符串
*/
public void insertText(String text){
Dispatch.put(getSelection(), "Text", text);
}
/**
* 将指定的⽂本替换成图⽚
* @param findText
* @param imagePath
* @return boolean true-查到并替换该⽂本,false-未查到⽂本
*/

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。