c#操作WPS_Excel 添加外部引用文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/laizhixue/article/details/82797085

开发环境:Windows7 64位+Word2007 +WPS Office 专业增强版+Visual Studio 2013

软件安装:安装Microsoft Office 2007 后再安装WPS2016 。

直接添加安装目录下的DLL引用

添加下面的引用后可以使用using WPS;(删除WORD后文件不会丢失)

C:\Windows\assembly\GAC_32\Kingsoft.Office.Interop.Ksoapiv8\98.1.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Ksoapiv8.dll

C:\Windows\assembly\GAC_32\Kingsoft.Office.Interop.Wpsapiv8\2.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wpsapiv8.dll


添加下面的引用后可以使用using Word;(删除WORD后文件会丢失)
C:\Windows\assembly\GAC_32\Kingsoft.Office.Interop.Ksoapi\99.1.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Ksoapi.dll 

C:\Windows\assembly\GAC_32\Kingsoft.Office.Interop.Wpsapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wpsapi.dll

添加安装目录下的WPS对应DLL引用(D:\Program Files (x86)\Kingsoft\WPS Office\10.8.2.6726为本机的WPS2016的安装目录)

D:\Program Files (x86)\Kingsoft\WPS Office\10.8.2.6726\office6\etapi.dll //实现excel对PDF转换(Excel文件对应的API)

D:\Program Files (x86)\Kingsoft\WPS Office\10.8.2.6726\office6\wppapi.dll //PPT对PDF转换(PPT文件对应的API)

D:\Program Files (x86)\Kingsoft\WPS Office\10.8.2.6726\office6\wpsapi.dll //WORD对PDF转换(Word文件对应的API)

本例开发直接添加D:\Program Files (x86)\Kingsoft\WPS Office\10.8.2.6726\office6\wpsapi.dll引用即可。

添加引用完成后,项目引用会多出来四个引用如下:

3、新增一个简单的打开WPS文档的类WpsHelper.cs文件代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WpsTestProject
{
    public class WpsHelper
    {

        #region 构造函数
        public WpsHelper()
        { 
            //这里创建wps实例本机安装的是wps2016
            Type type = Type.GetTypeFromProgID("KWps.Application");
            dynamic wps = Activator.CreateInstance(type);
        }
        #endregion

        #region 在WPS2016中打开指定路径的文档
        /// <summary>
        /// 在WPS2016中打开指定路径的文档
        /// </summary>
        /// <param name="strFilePath">文件路径</param>
        public void OpenWpsFile(string strFilePath)
        {
            try
            {
                Word.Application wordApp = new Word.Application();//应用对象 
                wordApp.NormalTemplate.Saved = true;
                object fileName = strFilePath;
                object confirmConversions = Type.Missing;
                object readOnly = false;
                object addToRecentFiles = Type.Missing;
                object passwordDoc = Type.Missing;
                object passwordTemplate = Type.Missing;
                object revert = Type.Missing;
                object writepwdoc = Type.Missing;
                object writepwTemplate = Type.Missing;
                object format = Type.Missing;
                object encoding = Type.Missing;
                object visible = Type.Missing;
                object openRepair = Type.Missing;
                object docDirection = Type.Missing;
                object notEncoding = Type.Missing;
                object xmlTransform = Type.Missing;
                Word.Document doc = wordApp.Documents.Open(
                    ref fileName, ref confirmConversions, ref readOnly, ref addToRecentFiles,
                    ref passwordDoc, ref passwordTemplate, ref revert, ref writepwdoc,
                    ref writepwTemplate, ref format, ref encoding, ref visible, ref openRepair,
                    ref docDirection, ref notEncoding, ref xmlTransform);

                wordApp.Visible = true;
                wordApp.Activate();//激活文档使文档为当前处理  
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开文件时出错:"+ex);
            }
        }
        #endregion
    }
}

开发总结:WPS软件要安装专业版的;同一项目中无法同时引用WORD和WPS,可以分开为两个项目;最关键的是WPS对应的DLL引用问题,网上太多的代码,要总结调试,挺麻烦。

猜你喜欢

转载自blog.csdn.net/laizhixue/article/details/82797085