用jacob将一段文字写入到word中,包括页眉页脚
2010-12-13 16:38
465 查看
public String execute(){ openWord(false); //初始化word文档 createNewDocument(); //创建Word文档 insertText(content1+content2); //插入文本内容及页眉页脚 saveWordFile(fileName); //保存Word文档到指定的目录(包括文件名) return SUCCESS; } /** * 初始化word文档 * openWord * @param makeVisible * @author tf.li * @data Dec 11, 2010 */ public void openWord(boolean makeVisible) { //Open Word if we/'ve not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent("Word.Application"); } //Set the visible property as required. Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible)); } /** * 创建Word文档 * createNewDocument * @author tf.li * @data Dec 11, 2010 */ public void createNewDocument() { //Find the Documents collection object maintained by Word Dispatch documents = Dispatch.get(MsWordApp,"Documents").toDispatch(); //Call the Add method of the Documents collection to create //a new document to edit document = Dispatch.call(documents,"Add").toDispatch(); } /** * 插入文本内容及页眉页脚 * insertText * @param textToInsert * @author tf.li * @data Dec 11, 2010 */ public void insertText(String textToInsert) { // Get the current selection within Word at the moment. If // a new document has just been created then this will be at // the top of the new doc Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); //Put the specified text at the insertion point Dispatch.put(selection,"Text",textToInsert); //插入内容 /****插入页眉页脚*****/ //取得活动窗体对象 Dispatch ActiveWindow = MsWordApp.getProperty( "ActiveWindow").toDispatch(); //取得活动窗格对象 Dispatch ActivePane = Dispatch.get(ActiveWindow, "ActivePane").toDispatch(); //取得视窗对象 Dispatch View = Dispatch.get(ActivePane, "View").toDispatch(); /****设置页眉*****/ Dispatch.put(View, "SeekView", "9"); Dispatch.put(selection, "Text", ym); /****设置页脚,动态插入页码*****/ Dispatch.put(View, "SeekView", "10"); final Dispatch Sections = Dispatch.get(document, "Sections").toDispatch(); final Dispatch item = Dispatch.call(Sections, "Item", new Variant(1)).toDispatch(); final Dispatch footer = Dispatch.get(item, "Footers").toDispatch(); final Dispatch f1 = Dispatch.call(footer, "Item", new Variant(1)).toDispatch(); final Dispatch range = Dispatch.get(f1, "Range").toDispatch(); final Dispatch fields = Dispatch.get(range, "Fields").toDispatch(); Dispatch paragraphFormat=(Dispatch) Dispatch.get(selection,"ParagraphFormat").getDispatch(); Dispatch.put(paragraphFormat, "Alignment", 1); Dispatch.call(fields, "Add", Dispatch.get(selection, "Range").toDispatch(), new Variant(-1), "Page", true).toDispatch(); Dispatch.call(selection, "TypeText", "/"); Dispatch.call(fields, "Add", Dispatch.get(selection, "Range").toDispatch(), new Variant(-1), "NumPages",true).toDispatch(); Dispatch font = Dispatch.get(range, "Font").toDispatch(); Dispatch.put(font,"Name",new Variant("宋体_GB2312")); //Dispatch.put(font, "Bold", new Variant(true)); Dispatch.put(font, "Size", 9); } /** * 保存Word文档到指定的目录(包括文件名) * saveWordFile * @param filePath * @author tf.li * @data Dec 11, 2010 */ public void saveWordFile(final String filePath) { //保存文件 Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] {filePath, new Variant(0)}, new int[1]); //作为word格式保存到目标文件 Variant f = new Variant(false); Dispatch.call(document, "Close", f); ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理 MsWordApp.invoke("Quit", new Variant[0]); }
相关文章推荐
- java导出word、pdf之添加页眉----页眉(指定格式,包括图片和文字)
- 如何快速的导出WORD(包括图片,页眉,页脚,水印)
- 导出word的实例,包括页眉页脚还有图片
- C# 替换Word文字【包含页眉、页脚、文本框、普通文字的替换】
- 如何把WORD里的文本转到coreLDRAW里去,包括页眉页脚,所有文本不变
- Word中页眉文字下横线如何变化粗细线型,页脚文字上方如何添加横
- word分节符设置与不同页眉页脚页码设置
- Word 文档 自定义页眉页脚
- 一个长文档里,包括封面、不同的章节,如果我想封面不设置页眉页脚,每个章节的页眉都不同,请问应该如何设置页眉页脚?
- Word页眉,页脚边框自定义
- 原创: 开题报告中摘要部分快速将一段文字插入到word的表格中
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- 如何用word 2007在指定页插入页码和页眉页脚?
- 如何删除07版word页眉页脚的横线
- Word中设置不同的页眉或页脚
- NTKO word在线文本编辑控件写页眉页脚
- QAxObject word文档添加页码(页眉和页脚)
- word之怎么对每节或每章使用不同的页眉或页脚?
- Aspose系列(二)Word增加页眉和页脚
- VC 6下word操作心得-将文字写入word