请教C#调用WPS打开word,excel并转成PDF文件

yj5723187 2011-07-28 07:52:31
如题,,由于做的是政府相关部门的项目,,他们要支持国产...

都是安装金山的WPS。。

请教下高手,,有没有关于C#调用WPS打开word和excel模板(word和excel都是做好了的模板),将读取的数据替换到这些模板的标签中,然后再转成PDF文件保存在服务器端....

有实例代码最好.. 谢谢了..
...全文
1717 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
k__007 2012-05-10
  • 打赏
  • 举报
回复
楼主实现了么
yishield 2011-10-11
  • 打赏
  • 举报
回复
wps office本来就提供接口函数可以开发的啊。就像用ms office一样的。自己找找资料
shashouyouzhao 2011-10-03
  • 打赏
  • 举报
回复
帮顶一下,关注!
yj5723187 2011-07-29
  • 打赏
  • 举报
回复
to heaventohell17:
我就是要在服务器端转换成PDF啊,,我是想用WPS打开office,因为用office的word和excel做好了模板,,需要将数据库读取出的数据替换到模板中的标签,,然后再转成PDF到服务器磁盘上..
麻烦你给我贴下代码,,谢谢了啊..


to 子夜:
谢谢你的回答,,可是,貌似不是用WPS哦,,用户那边,服务端是不装office的.. - -
mail_ricklee 2011-07-29
  • 打赏
  • 举报
回复
Aspose.Word
Aspose.Cell
Aspose.Pdf

参考FortuneBase中的模板例子
参考地址www.cnblogs.com/mail-ricklee
yj5723187 2011-07-29
  • 打赏
  • 举报
回复
用户那边,服务器和客户端都是装WPS,,没装office,,所以我必须在服务端就把制作好的office模版用WPS打开,然后再把数据替换到模版中的标签位置上,再转换成PDF文件存在服务器的临时磁盘文件夹下,用PDF展现到客户端页面上,做打印等操作.。

刚调用WPS,,还出现检索类工厂 检索不到组件的错误,,郁闷..
heaventohell17 2011-07-29
  • 打赏
  • 举报
回复
至于转换pdf 我想问问,你最好提供给用户的是什么格式 ??


word? pdf??
heaventohell17 2011-07-29
  • 打赏
  • 举报
回复
...晕,因为我做的大部分都是在客户端往word书签里打内容

你在服务器端往书签插入内容的话,给你段脚本,你看看就行了,因为office操作,服务器端和客户端很相似



//调用word
var app = WordContainer.OpenWord();
var word=WordContainer.OpenDocument(strFileName);
var seltn=app.Selection;


try
{
seltn.GoTo(-1,0,0,"书签名");
seltn.TypeText("值");
}catch(e){}

try
{
seltn.GoTo(-1,0,0,"receiveDate");
seltn.TypeText(getAttrValue(childNode,"recieveDate"));
}catch(e){}
document.all.WordContainer.WordDocument.Save();
梦纷飞舞 2011-07-29
  • 打赏
  • 举报
回复
你试下可以把你的那个转化为HTML,然后我给你段代码可以转成PDF
yj5723187 2011-07-29
  • 打赏
  • 举报
回复
没人回答?? 我郁闷...

heaventohell17 2011-07-29
  • 打赏
  • 举报
回复
如果不行,找找wps开发文档吧。。。或者联系联系他们吧


至于转换pdf,俺这是用的在服务器端打开office然后采用虚拟打印,应该能借鉴,给你贴下吧,你试试


[WebMethod]
public bool ConvertWordTOPDF(string WordPath)
{
bool ret=false;
#region
//string dataName = collection[i].FileName;//本地文件名称(带路径)
//string ip=Request.UserHostAddress;
//string wordname=WordPath;
string wordPath=WordPath;
string pdfPath=wordPath.Split('.')[0]+".pdf";
oWord._Document m_Document = null;
oWord._Application m_wordApplication = null;
object oMissing = Type.Missing;
oWord.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
try
{
object obj = System.Reflection.BindingFlags.InvokeMethod;
Type wordType = word.GetType();
oWord.Documents docs = word.Documents;
Type docsType = docs.GetType();
object objDocName = wordPath;
oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
//打印输出到指定文件
Type docType = doc.GetType();
object printFileName =wordPath.Split('.')[0]+".ps";
docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
object saveasPath =wordPath.Split('.')[0]+"new.doc";
//必须另存为!
doc.SaveAs(ref saveasPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//必须关闭
doc.Close(ref savechanges, ref oMissing, ref oMissing);
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null,word, null);
//删除另存为文件
try
{
System.IO.File.Delete(saveasPath.ToString());
}
catch
{
}
string o1 = printFileName.ToString();//与生产的PS文件同步
string o2 = pdfPath;
string o3 = "";
//引用将PS转换成PDF的对象
try
{
ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
Type pdfType = pdf.GetType();
pdfType.InvokeMember("FileToPDF", System.Reflection.BindingFlags.InvokeMethod, null, pdf, new object[] { o1, o2, o3 });
pdf = null;
//System.IO.File.Delete( "123.log");//清除转换日志文件
}
catch
{ //throw new Exception("PS转PDF处出错!");
}
System.IO.File.Delete( printFileName.ToString());//清除ps文件

System.IO.File.Delete( printFileName.ToString().Split('.')[0]+".log");//清除转换日志文件

if(System.IO.File.Exists(pdfPath))
{
ret=true;
}
//为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
foreach (Process proc in System.Diagnostics.Process.GetProcesses())
{
int begpos;
int endpos;

string sProcName = proc.ToString();
begpos = sProcName.IndexOf("(") + 1;
endpos = sProcName.IndexOf(")");

sProcName = sProcName.Substring(begpos, endpos - begpos);

if (sProcName.ToLower().CompareTo("acrodist") == 0)
{
try
{
proc.Kill();
}
catch { }
break;
}
}
}
catch(Exception ex)
{

}

return ret;
#endregion
}
heaventohell17 2011-07-29
  • 打赏
  • 举报
回复
你在他服务器端,装个盗版的,他们又不知道......


wps的书签好不到.....俺以前弄过,用脚本找不到书签。。。。 转而用office设置书签,就可以


现在是服务器端office,客户端wps。。。。。。这样没问题
yj5723187 2011-07-29
  • 打赏
  • 举报
回复
= =

就是因为不装office撒,,政府要支持国产,,我能有啥办法哦
heaventohell17 2011-07-29
  • 打赏
  • 举报
回复
在服务器端装office ,这样一切就简单多了。。
子夜__ 2011-07-28
  • 打赏
  • 举报
回复
private string adobePdfPrint = "Adobe PDF";
private string adobeDisPrint = "Acrobat Distiller";
private string regRoot = "SOFTWARE//Adobe//Acrobat Distiller//";
private string printerFileName = "acrodist.exe";
private string regName = "InstallPath";


/// <summary>
/// 获取acrodist.exe的安装路径
/// </summary>
/// <returns></returns>
private string GetAdobeDisFilePath()
{
RegistryKey regKey = null;
RegistryKey acrodistKey = null;
string printerName = string.Empty;
int i;
string regRootVersion = string.Empty;

regKey = Registry.LocalMachine;
// acrodist的4-8版本适用
for (i = 4; i < 9; i++)
{
regRootVersion = string.Format("{0}{1}.0", regRoot, i);
acrodistKey = regKey.OpenSubKey(regRootVersion);

if (acrodistKey != null)
{
printerName = acrodistKey.GetValue(regName, "") + "//" + printerFileName;
if (File.Exists(printerName))
{
return printerName;
}
}
}
throw new Exception("Acrobat Distiller printer not found!");
}

/// <summary>
/// 获取Adobe Printer
/// "Adobe PDF" 或 "Acrobat Distiller"
/// </summary>
/// <returns></returns>
private string GetAdobePrinter()
{
foreach (string printername in PrinterSettings.InstalledPrinters)
{
if (printername.ToUpper().IndexOf(adobePdfPrint.ToUpper()) != -1 || printername.ToUpper().IndexOf(adobeDisPrint.ToUpper()) != -1)
{
return printername;
}
}
return string.Empty;
}

public void ConvertWord2Pdf(string sourceFile)
{
if (PrinterSettings.InstalledPrinters.Count == 0)
throw new Exception("Did not find the printer, please install the printer.");

if (!File.Exists(sourceFile))
throw new Exception(string.Format("File not found:{0}", sourceFile));



string strDir = Path.GetDirectoryName(sourceFile);

string strName = Path.GetFileNameWithoutExtension(sourceFile);

string prnFile = string.Format("{0}//{1}.prn",strDir,strName);
string pdfFile = string.Format("{0}//{1}.pdf",strDir,strName);
object objPrnFile = (object)prnFile;
object background = true;
object printToFile = true;
object collate = true;
object append = false;
object manualDuplexPrint = false;
object copies = false;
object range = Word.WdPrintOutRange.wdPrintAllDocument;
object missing = System.Reflection.Missing.Value;
string msg = string.Empty;


string adobePrinter = GetAdobePrinter();

if (adobePrinter.Length == 0)
throw new Exception("Did not find Adobe PDF printer or Acrobat Distiller printer, please install the printer.");

IWord word = new Word2003Factory().CreateWord();
// 打开Word文档
Word.Document doc = word.OpenDocument(sourceFile);
string oldPrint = doc.Application.ActivePrinter;
// 将Adobe Printer设置为默认打印机
doc.Application.ActivePrinter = adobePrinter;
DateTime start = DateTime.Now;
DateTime end = start.AddSeconds(20);
// Word文档打印到Prn文件
doc.PrintOut(ref background, ref append, ref range, ref objPrnFile, ref missing, ref missing,
ref missing, ref missing,ref missing, ref printToFile, ref collate, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);


while (!File.Exists(prnFile))
{
if (DateTime.Now > end)
{
throw new Exception("Word document print to prn document overtime");
}
else
{
Application.DoEvents();
}
}

doc.Application.ActivePrinter = oldPrint;
word.Close(false);
doc = null;
word = null;

// Prn装PDF
Process objProcess = new Process();
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.FileName = GetAdobeDisFilePath();
objProcess.StartInfo.Arguments = prnFile;

start = DateTime.Now;
end = start.AddSeconds(20);
objProcess.Start();


while (!File.Exists(pdfFile))
{
if (DateTime.Now > end)
{
throw new Exception("Word document print to prn document overtime");
}
else
{
Application.DoEvents();
}
}

objProcess.CloseMainWindow();

//if (File.Exists(prnFile))
//{
// File.Delete(prnFile);
//}

/*
ACRODISTXLib.PdfDistiller pdfDis = new ACRODISTXLib.PdfDistiller();

pdfDis.FileToPDF(prnFile, pdfFile, string.Empty);
start = DateTime.Now;
end = start.AddSeconds(20);
while(!File.Exists(pdfFile))
{
if (DateTime.Now > end)
{
throw new Exception("prn document print to pdf document overtime");
}
else
{
Application.DoEvents();
}
}
pdfDis = null;
*/
}

WORD转PDF
heaventohell17 2011-07-28
  • 打赏
  • 举报
回复
如果需要代码,说一下,有空俺给你贴出来,因为今天不想找了......
heaventohell17 2011-07-28
  • 打赏
  • 举报
回复
只要机器上装wps 没装office 你在服务器上放上office文件,他下载下来是默认wps打开的,寻找书签什么的应该是可以,我以前遇到用wps设置的书签,脚本找不到的问题



还有转换成pdf的,我只在服务器端实现过。。。客户端的当时实在搞不出来
yj5723187 2011-07-28
  • 打赏
  • 举报
回复
额,,自己顶一下..
yj5723187 2011-07-28
  • 打赏
  • 举报
回复
额,,大哥,不是吧?? WPS不就是抄袭了office么? 听说惊人的相似啊,没有研究过的?
bdmh 2011-07-28
  • 打赏
  • 举报
回复
跟厂家联系,看看有没有接口,否则就自己去研究wps的数据结构
WebOffice是一款由北京点聚信息技术有限公司提供的完全免费(商业用途也免费)且功能强大的在线Word/excel/wps编辑辅助控件,可以实现: 1.在线编辑WordExcel、PPT、WPS... ... 2.全面支持MS Office的界面定制,包括对于Office2007的全面支持 3.修订留痕 4.限制打印、保存、复制 5.直接保存到服务器,支持标准Http Post协议 6.强大的书签管理 7.套红、文档保护 8.模板管理 9.其他功能扩展 10.提供开发论坛http://www.dianju.cn/forum/,在其中提供软件更新及问题回答服务 本控件不同于其它同类软件,不是基于微软的开放源码DsoFramer,也不基于OLE,可以解决DsoFramer及其派生产品的一些稳定性问题 本控件不同于其它同类收费软件,本控件完全免费和界面完全可控制。 点聚会定期更新本控件,并解答论坛上的询问,免费不代表免服务,每一个使用者都能得到及时服务。 安装包内包含: 1.WebOffice安装包(仅包含WebOffice控件) 2.WebOffice接口SDK 3.WebOffice网页接口调用例子 4.DES手写及签章系统(Office签章系统)试用版。 5.演示章及证书 6.点聚产品白皮书 WebOffice组件可以无缝集成点聚公司的电子签章和手写审批类产品。 点聚信息(http://www.dianju.com.cn)是国内专业的安全和文档中间件软件制造商。公司注重自主知识产权产品的研发,始终坚持技术为本、服务为先的原则,在电子印章、手写签批、安全版式文档和电子表单等领域处于国内领先水平。 点聚自主研发的版式文件系统独创性的将整个系统缩小为1M大小的组件,并在多项重要技术指标上领先PDF和其它版式格式。 点聚支持全系列手写及签章解决方案,可同时提供Office签章系统(Word/Excel/Wps)、网页签章系统、版式签章系统. 点聚是国内唯一一家同时拥有国密和军密资质的电子印章及安全文档产品提供商(国密、军密、公安部销售许可)。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧