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

posted @ 2013-05-23 19:53  kuailewangzi1212  阅读(2497)  评论(1编辑  收藏  举报