• 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自身带有重新注册工具,可以重新注册,注册完成后,要重新启动系统

  • 相关阅读:
    忽然背后冒冷汗
    随机获取中国境内ip地址的php代码
    复制粘贴的句子
    Winform使用BackGroundWorker代替线程执行后台代码
    在IE中测试调用Web Service
    在存储过程中编写正确的事务处理代码
    【转】使用HttpWebRequest POST图片等文件,带参数
    LINQ to Entities 不识别方法"System.String ToString()"
    Asp.net 出现:HTTP 错误 404.0 Not Found
    C# 用内存映射文件读取大日志文件(.log)
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/3095457.html
Copyright © 2020-2023  润新知