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

开发环境: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引用问题,网上太多的代码,要总结调试,挺麻烦。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

laizhixue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值