zoukankan      html  css  js  c++  java
  • asp.net访问dcom组件(wps或office)配置

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    using WPS;
    using System.IO;

    namespace dveda.Module
    {
    class Word2PDF : IDisposable
    {
    Application wps;

    public Word2PDF()
    {
    wps = new Application();
    }

    /// <summary>
    /// 合并文件
    /// </summary>
    /// <param name="firstfile">第一个文件</param>
    /// <param name="secodefile">第二个文件</param>
    /// <param name="outputfile">合并后输出的文件</param>
    /// <returns></returns>
    public bool mergerFile(string firstfile, string secodefile,string outputfile)
    {
    try
    {
    object objMissing = System.Reflection.Missing.Value;

    Document origDoc = wps.Documents.Open(firstfile, Visible: false);
    origDoc.Activate();

    wps.Selection.InsertFile(secodefile);
    wps.Selection.InsertBreak(WpsBreakType.wpsPageBreak);
    wps.ActiveDocument.SaveAs(outputfile);
    wps.ActiveDocument.Close();
    }
    catch (Exception ex)
    {
    throw ex;
    }


    //wps.Selection.Copy();
    //wps.Selection.Document;

    /*
    //object missing = Missing.Value;
    //string oFirstDoc = @"d:\1.doc";//word檔1
    //object oSecondDoc = @"d:\2.doc";//word檔2
    //object oOutputDoc = @"d:\3.doc";//合併檔s

    ////object oPageBreak = Word.WdBreakType.wdLineBreak;//接下行合併(LineBreak)
    //object oPageBreak = Word.WdBreakType.wdPageBreak;//接下頁合併(PageBreak)

    //Word._Application wordApp = new Word.Application();
    //Word._Document origDoc = wordApp.Documents.Open(ref oSecondDoc, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    //origDoc.Activate();
    //wordApp.Selection.InsertFile(oFirstDoc, ref missing, ref missing, ref missing, ref missing);
    //wordApp.Selection.InsertBreak(ref oPageBreak);

    //wordApp.ActiveDocument.SaveAs(ref oOutputDoc, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    //wordApp.ActiveDocument.Close(ref missing, ref missing, ref missing);
    //wordApp.Quit(ref missing, ref missing, ref missing);//加這行可以 Kill WINWORD.EXE process
    * */

    return true;
    }

    public bool ToPdf(string wpsFilename, string pdfFilename = null)
    {
    if (wpsFilename == null) { throw new ArgumentNullException("wpsFilename"); }

    if (pdfFilename == null)
    {
    pdfFilename = Path.ChangeExtension(wpsFilename, "pdf");
    }

    //Console.WriteLine(string.Format(@"正在转换 [{0}]-> [{1}]", wpsFilename, pdfFilename));

    Document doc = wps.Documents.Open(wpsFilename, Visible: false);
    doc.ExportPdf(pdfFilename);
    doc.Close();
    return true;
    }

    public void Dispose()
    {
    if (wps != null) { wps.Terminate();}
    }
    }
    }

    //dcom配置

    dcomcnfg.exe

    添加network service 读写权限

    标识-下列账号administrator/密码

    最后需要注意的是,如果wps损坏,wps自身带有重新注册工具,可以重新注册,注册完成后,要重新启动系统

  • 相关阅读:
    python测试开发django177.启动项目添加初始化数据(fixtures的使用) 上海
    python笔记70 Python中`__repr__`和`__str__`区别 上海
    python笔记69 什么是猴子补丁(Monkey Patch)? 上海
    pytest文档78 钩子函数pytest_runtest_makereport获取用例执行报错内容和print内容 上海
    python测试开发django176.数据库迁移数据(manage.py dumpdata) 上海
    python3面试题:如何用python实现栈(Stack)的操作? 上海
    python笔记71 traceback.print_exc()保存异常内容 上海
    测试驱动开发 Rss Reader Item Marker
    Rhino Mocks (RhinoMock)2
    继承的Singleton模式的实现
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/3095457.html
Copyright © 2011-2022 走看看