使用jacob调用office组件将word转换为pdf

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mutourenoo/article/details/84914718
使用jacob调用office组件将word转换为pdf

只能在windows平台运行, 效果最完美

参考文档{@link http://www.cnblogs.com/xxyfhjl/p/6773786.html}

https://www.cnblogs.com/yzuzhang/p/5134655.html
展开阅读全文

jacobword转换为html报错

03-18

package Test;rnrnimport com.jacob.com.*;rnimport com.jacob.activeX.*;rnrnpublic class WordToHtml rn /**rn *文档转换函数rn * rn * @param docfilern * word文档的绝对路径加文件名(包含扩展名)rn *@param htmlfilern * 转换后的html文件绝对路径和文件名(不含扩展名)rn */rn public static final int WORD_HTML = 8;rnrn public static final int WORD_TXT = 7;rnrn public static final int EXCEL_HTML = 44;rnrn /**rn * WORD转HTMLrn * rn * @param docfilern * WORD文件全路径rn * @param htmlfilern * 转换后HTML存放路径rn */rn public static void wordToHtml(String docfile, String htmlfile) rn ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动wordrn try rn app.setProperty("Visible", new Variant(false));rn Dispatch docs = app.getProperty("Documents").toDispatch();rn Dispatch doc = Dispatch.invoke(rn docs,rn "Open",rn Dispatch.Method,rn new Object[] docfile, new Variant(false),rn new Variant(true) , new int[1]).toDispatch();rn Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] rn htmlfile, new Variant(WORD_HTML) , new int[1]);rn Variant f = new Variant(false);rn Dispatch.call(doc, "Close", f);rn catch (Exception e) rn e.printStackTrace();rn finally rn app.invoke("Quit", new Variant[] );rn rn rnrn public static void main(String[] strs) rn WordToHtml.wordToHtml("e:/中百便民采购资金管理数据接口定义_V1.0.doc", "c:");rn rnrnrnrn报错信息:rncom.jacob.com.ComFailException: Invoke of: SaveAsrnSource: Microsoft WordrnDescription: rnrn at com.jacob.com.Dispatch.invokev(Native Method)rn at com.jacob.com.Dispatch.invokev(Dispatch.java:804)rn at com.jacob.com.Dispatch.invoke(Dispatch.java:483)rn at Test.WordToHtml.wordToHtml(WordToHtml.java:39)rn at Test.WordToHtml.main(WordToHtml.java:55)rnrn注:jar包已经导入到项目中,并且jacob.dll文件在项目的lib下面和tomcat 6.0的bin以及lib目录下都放入了rnrn请问是什么情况? 论坛

c#调用officewordpdf

10-16

[code=csharp]using System;rnusing System.Collections.Generic;rnusing System.Linq;rnusing System.Web;rnusing System.Web.UI;rnusing System.Web.UI.WebControls;rnusing O2S.Components.PDFRender4NET;rnusing Word = Microsoft.Office.Interop.Word;rnusing Excel = Microsoft.Office.Interop.Excel;rnusing PowerPoint = Microsoft.Office.Interop.PowerPoint;rnusing Microsoft.Office.Core;rnusing System.IO;rnusing System.Text;rnrnrnrnrnrnrn/*using System;rnusing System.Collections.Generic;rnusing System.Web;rnusing System.Web.UI;rnusing System.Web.UI.WebControls;*/rnusing System.Data;rnusing DTcms.Common;rnrnnamespace DTcms.Web.admin.downloadrnrn public partial class edit : Web.UI.ManagePagern rn private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型rn private int channel_id;rn private int id = 0;rnrn protected void Page_Load(object sender, EventArgs e)rn rn string _action = DTRequest.GetQueryString("action");rn this.channel_id = DTRequest.GetQueryInt("channel_id");rn if (this.channel_id == 0)rn rn JscriptMsg("频道参数不正确!", "back", "Error");rn return;rn rn if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())rn rn this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型rn this.id = DTRequest.GetQueryInt("id");rn if (this.id == 0)rn rn JscriptMsg("传输参数不正确!", "back", "Error");rn return;rn rn if (!new BLL.article().Exists(this.id))rn rn JscriptMsg("信息不存在或已被删除!", "back", "Error");rn return;rn rn rn if (!Page.IsPostBack)rn rn TreeBind(this.channel_id); //绑定类别rn if (action == DTEnums.ActionEnum.Edit.ToString()) //修改rn rn ShowInfo(this.id);rn rn rn rnrnrnrn /// rn /// word转换PDFrn /// rn /// rn /// rn /// rnrn private bool WordToPDF(string sourcePath, string targetPath)rn rn bool result = false;rn Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;rn object paramMissing = Type.Missing;rn Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();rn Microsoft.Office.Interop.Word.Document wordDocument = null;rn tryrn rn object paramSourceDocPath = sourcePath;rn string paramExportFilePath = targetPath;rn Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat;rn bool paramOpenAfterExport = false;rn Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;rn Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;rn int paramStartPage = 0;rn int paramEndPage = 0;rn Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;rn bool paramIncludeDocProps = true;rn bool paramKeepIRM = true;rn Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;rn bool paramDocStructureTags = true;rn bool paramBitmapMissingFonts = true;rn bool paramUseISO19005_1 = false;rnrn wordDocument = wordApplication.Documents.Open(rn ref paramSourceDocPath, ref paramMissing, ref paramMissing,rn ref paramMissing, ref paramMissing, ref paramMissing,rn ref paramMissing, ref paramMissing, ref paramMissing,rn ref paramMissing, ref paramMissing, ref paramMissing,rn ref paramMissing, ref paramMissing, ref paramMissing,rn ref paramMissing);rnrn if (wordDocument != null)rn wordDocument.ExportAsFixedFormat(paramExportFilePath,rn paramExportFormat, paramOpenAfterExport,rn paramExportOptimizeFor, paramExportRange, paramStartPage,rn paramEndPage, paramExportItem, paramIncludeDocProps,rn paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,rn paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing);rnrn result = true;rnrn if (wordDocument != null)rn rn wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);rn wordDocument = null;rn rn if (wordApplication != null)rn rn wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);rn wordApplication = null;rn rn GC.Collect();rn GC.WaitForPendingFinalizers();rn GC.Collect();rn GC.WaitForPendingFinalizers();rn rn catchrn rn result = false;rnrn if (wordDocument != null)rn rn wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);rn wordDocument = null;rn rn if (wordApplication != null)rn rn wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);rn wordApplication = null;rn rn GC.Collect();rn GC.WaitForPendingFinalizers();rn GC.Collect();rn GC.WaitForPendingFinalizers();rn rn return result;rn rn rn[/code]rn[img=https://img-bbs.csdn.net/upload/201310/16/1381893272_681638.jpg][/img]rn我office控件都调用了,为啥还这样啊? 论坛

没有更多推荐了,返回首页