我用jacob组件操作word,现在想要一个功能,就是要把指定位置的内容能复制出来,然后保存到另一个word中。
例如:word的内容如下所示
##1你好啊,你在哪里?##1
我只想复制##1中间的内容“你好啊,你在哪里?”这几个字符,还要能复制如下格式
##1
你好啊,你在哪里?
我不好,我很急
##1
说明:这##1中间还有可能有图片,表格什么的,这些内容也要能复制,请高手指点,小弟多谢了!!!
5 个解决方案
#1
楼主可以看下POI。
可以实现你的做法。
可以实现你的做法。
#2
嗯
#4
poi上没有找到可以把格式和字体大小复制过来,只有对文本的操作
#5
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.com.ComThread;
public class StudyJacob {
public static void main(String[] args) {
ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法
//Instantiate objWord //Declare word object
ActiveXComponent objWord = new ActiveXComponent("Word.Application");
//Assign a local word object
Dispatch wordObject = (Dispatch) objWord.getObject();
//Create a Dispatch Parameter to show the document that is opened
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见
//Instantiate the Documents Property
Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序)
//Add a new word document, Current Active Document
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容
Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落
Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
[color=#FF0000]// 找到刚输入的段落,设置格式
Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
new Variant(paragraphCount)).
toDispatch(); // 最后一段
Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").
toDispatch();
Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();
Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体
Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体
Dispatch.put(font, "Name", new Variant("宋体")); //
Dispatch.put(font, "Size", new Variant(12)); //小四 [/color]Dispatch.call(document, "SaveAs", new Variant("C:
abc.doc")); // 保存一个新文档
ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}
}
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.com.ComThread;
public class StudyJacob {
public static void main(String[] args) {
ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法
//Instantiate objWord //Declare word object
ActiveXComponent objWord = new ActiveXComponent("Word.Application");
//Assign a local word object
Dispatch wordObject = (Dispatch) objWord.getObject();
//Create a Dispatch Parameter to show the document that is opened
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见
//Instantiate the Documents Property
Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序)
//Add a new word document, Current Active Document
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容
Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落
Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
[color=#FF0000]// 找到刚输入的段落,设置格式
Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
new Variant(paragraphCount)).
toDispatch(); // 最后一段
Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").
toDispatch();
Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();
Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体
Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体
Dispatch.put(font, "Name", new Variant("宋体")); //
Dispatch.put(font, "Size", new Variant(12)); //小四 [/color]Dispatch.call(document, "SaveAs", new Variant("C:
abc.doc")); // 保存一个新文档
ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}
}
#1
楼主可以看下POI。
可以实现你的做法。
可以实现你的做法。
#2
嗯
#3
#4
poi上没有找到可以把格式和字体大小复制过来,只有对文本的操作
#5
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.com.ComThread;
public class StudyJacob {
public static void main(String[] args) {
ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法
//Instantiate objWord //Declare word object
ActiveXComponent objWord = new ActiveXComponent("Word.Application");
//Assign a local word object
Dispatch wordObject = (Dispatch) objWord.getObject();
//Create a Dispatch Parameter to show the document that is opened
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见
//Instantiate the Documents Property
Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序)
//Add a new word document, Current Active Document
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容
Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落
Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
[color=#FF0000]// 找到刚输入的段落,设置格式
Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
new Variant(paragraphCount)).
toDispatch(); // 最后一段
Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").
toDispatch();
Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();
Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体
Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体
Dispatch.put(font, "Name", new Variant("宋体")); //
Dispatch.put(font, "Size", new Variant(12)); //小四 [/color]Dispatch.call(document, "SaveAs", new Variant("C:
abc.doc")); // 保存一个新文档
ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}
}
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.com.ComThread;
public class StudyJacob {
public static void main(String[] args) {
ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法
//Instantiate objWord //Declare word object
ActiveXComponent objWord = new ActiveXComponent("Word.Application");
//Assign a local word object
Dispatch wordObject = (Dispatch) objWord.getObject();
//Create a Dispatch Parameter to show the document that is opened
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见
//Instantiate the Documents Property
Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序)
//Add a new word document, Current Active Document
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容
Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落
Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
[color=#FF0000]// 找到刚输入的段落,设置格式
Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
new Variant(paragraphCount)).
toDispatch(); // 最后一段
Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").
toDispatch();
Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();
Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体
Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体
Dispatch.put(font, "Name", new Variant("宋体")); //
Dispatch.put(font, "Size", new Variant(12)); //小四 [/color]Dispatch.call(document, "SaveAs", new Variant("C:
abc.doc")); // 保存一个新文档
ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}
}