日期:2014-05-17  浏览次数:20642 次

请教C#调用WPS打开word,excel并转成PDF文件
如题,,由于做的是政府相关部门的项目,,他们要支持国产... 

都是安装金山的WPS。。  

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

有实例代码最好.. 谢谢了..

------解决方案--------------------
C# code
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 oldPrin