开发环境:Windows7 64位+Word2007 +WPS Office 专业增强版+Visual Studio 2010
软件安装:安装Microsoft Office 2007 后再安装WPS 2016。
异常说明:开发机测试先安装Microsoft Office 2013后,再安装Wps2016,系统正常。然后卸载Office2013 后,再到项目中引用WPS相关的DLL时会报错,只能卸载WPS2016后,重新安装OFFICE2007,再安装WPS2016继续开发。
开发步骤如下:
1、打开Visual Studio 2010后新建一个NET2.0应用程序项目。
2、添加WPS对应的引用:
方法一:根据实际需要科添加下面的COM引用
添加引用 -> COM -> Kingsoft Add-In Designer
添加引用 -> COM -> Microsoft Office 11.0 Object Library
添加引用 -> COM -> Upgrade WPS Office 3.0 Object Library(Beta)
添加引用 -> COM -> Upgrade WPS Presentation 3.0 Object Library(Beta)
添加引用 -> COM -> Upgrade Kingsoft WPS 3.0 Object Library(Beta)
添加引用 -> COM -> Kingsoft WPS Extend Apo 1.0 Object Library(Beta)
---------------------------------------------------------------------------------------------------------------------
方法二:直接添加安装目录下的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中打开指定路径的文档
///
/// 在WPS2016中打开指定路径的文档
///
/// 文件路径
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
}
}
4、窗体中新增一个按钮,添加事件,调用打开WPS文档的类中的方法如下:
代码如下:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件夹";
dialog.Filter = "WPS文件(*.wps)|*.wps";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
WpsHelper myHelper = new WpsHelper();
myHelper.OpenWpsFile(dialog.FileName);
}
}
5、运行程序后选择文件如下:
6、程序调用WPS2016打开文件效果如下:
开发总结:WPS软件要安装专业版的;同一项目中无法同时引用WORD和WPS,可以分开为两个项目;最关键的是WPS对应的DLL引用问题,网上太多的代码,要总结调试,挺麻烦。
源码下载地址:https://download.csdn.net/download/xqf222/10568235