网上已有很多C#操作word 打开文件或者C#操作Excel 这类文章。
今天我们要做的就是利用C#操作WPS Office2013 。
WPS Office 的API分V8 V9 版本,其中V9版本兼容Office2007/2010,可以很好的移植和使用原来Office程序的代码,今天我用 word转pdf 为例讲述怎么用C#操作WPS Office2013。
在 Microsoft Office 中,可以通过使用主 interop 程序集 (PIA) 来使用由 Office 应用程序公开的 COM 对象。现在的WPS Office V9也可以采用这种方式操作,但要注意的是WPS Office 个人版没有这个功能,解决方法就是
方法1、安装WPS 专业版
方法2、 从WPS 专业版中提取WPSOfficePIA,然后拷贝到安装目录安装, 或这里下载http://down.51cto.com/data/1978972
引用程序集:
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
(注:目录可以自己到机器上 C:\Windows\assembly\GAC_32\找,可能文件夹不一样)
下面这个作为例子,它的功能就是把word转pdf,原来word版本,没有修改任何代码。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace Office
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//未能找到程序集“Kingsoft.Office.Interop.Wpsapi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=15d99fb7f8fe5cb4”或它的某一个依赖项。系统找不到指定的文件。
//【属性】-【生成】-【目标平台】中的"any cpu"修改为X86 如果选择"any cpu".net程序将自动根据平台的按X86还是按X64方式运行,因为我机器是64位的,但是WPS组件只有32的,所以造成程序不匹配而报错。
WordExportAsPdf(openFileDialog1.FileName, openFileDialog1.FileName + ".pdf");
}
}
/// <summary>
/// 转换为pdf文件,适合(.doc、.docx、.mht、.htm文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
public static string WordExportAsPdf(string fileName, string outputFileName)
{
string isSucceed = "OK";
Word.WdExportFormat fileFormat = Word.WdExportFormat.wdExportFormatPDF;
Word.Application wordApp = null;
///An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Office.exe Additional information: 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80040154。
///C:\Users\luozhuang\AppData\Local\Kingsoft\WPS Office\
if (wordApp == null) wordApp = new Word.Application();
Word._Document wordDoc = null;
try
{
wordDoc = wordApp.Documents.Open(fileName, false, true);
wordDoc.ExportAsFixedFormat(outputFileName, fileFormat);
}
catch (Exception ex)
{
isSucceed = ex.Message;
}
finally
{
if (wordDoc != null)
{
wordDoc.Close(false);
wordDoc = null;
}
if (wordApp != null)
{
wordApp.Quit(false);
wordApp = null;
}
}
return isSucceed;
}
}
}
在我的机器上出现两个错误:
未能找到程序集“Kingsoft.Office.Interop.Wpsapi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=15d99fb7f8fe5cb4”或它的某一个依赖项。系统找不到指定的文件。
原因:
1、没有装PIA
2、【属性】-【生成】-【目标平台】中的"any cpu"修改为X86 如果选择"any cpu".net程序将自动根据平台的按X86还是按X64方式运行,因为我机器是64位的,但是WPS组件只有32的,所以造成程序不匹配而报错。
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Office.exe Additional information: 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80040154。
原因:
1、没有装wps
2、因为个人版默认安装目录是C:\Users\luozhuang\AppData\Local\Kingsoft\WPS Office\ ,这个目录似乎有权限问题,我把wps重新安装到C:\WPSOffice,然后重新引用程序集(注:如果你直接引用COM组件会生成个Interop DLL在bin\Debug目录,需要重新引用)就好了。
一个最直接办法就是购买和安装WPS 专业版,因为WPS 专业版安装目录在C:\Program Files (x86) 而不是C:\Users\luozhuang\AppData\Local\