关于WPS集成大家都了解MS Office在国内办公领域的位置,不过最近国内很多机关、部门、公司都选择了金山的WPS Office。我也在项目中多次碰到与WPS Office集成的问题。 本以为需要花大量时间了解WPS Office API以及其对办公系统的支持力,但发现WPS Office API、对象模型与MS Office惊人的相似。此外,WPS本身也是一个庞大的OLE控件,支持VBA、Add-In、OLE链接与嵌入等。在项目从MS Office到WPS Office的代码迁移中,代码稍做调整即可。 基于WPS是OLE控件的原因,使我在.NET调用WPS有了一些想法,并取得成功,嘿嘿!下面与大家分享一下。 前期绑定:a) 首先安装WPS,再引用WPS库,
b) 在窗体中添加二个按钮,代码如下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using WPS;
namespace CSharp_WPSStartKit { /// <summary> /// Form1 的摘要说明。 /// </summary> public class frmMain : System.Windows.Forms.Form { public WPS.Application WPSApp; private System.Windows.Forms.Button btnStart; private System.Windows.Forms.Button btnClose; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public frmMain() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TODO: 在InitializeComponent 调用后添加任何构造函数代码 // }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法- 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.btnStart = new System.Windows.Forms.Button(); this.btnClose = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnStart // this.btnStart.Location = new System.Drawing.Point(40, 88); this.btnStart.Name = "btnStart"; this.btnStart.Size = new System.Drawing.Size(88, 32); this.btnStart.TabIndex = 0; this.btnStart.Text = "启动WPS"; this.btnStart.Click += new System.EventHandler(this.btnStart_Click); // // btnClose // this.btnClose.Location = new System.Drawing.Point(176, 88); this.btnClose.Name = "btnClose"; this.btnClose.Size = new System.Drawing.Size(88, 32); this.btnClose.TabIndex = 1; this.btnClose.Text = "关闭WPS"; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // frmMain // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(336, 205); this.Controls.Add(this.btnClose); this.Controls.Add(this.btnStart); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "frmMain"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "C#操作WPS示例"; this.ResumeLayout(false);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() {
System.Windows.Forms.Application.Run(new frmMain()); }
/// <summary> /// 启动WPS文字,并添加居中文字,以及插入一幅图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, System.EventArgs e) { object MissingValue=Type.Missing; object lleft = 100, ltop = 50, lwidth = 148, lheight = 60;
// 启动WPS WPSApp = new WPS.ApplicationClass();
// 使WPS可见 WPSApp.Visible = true;
// 在Documents中新建一篇文档 WPS.Document WPSDocument = WPSApp.Documents.Add(ref MissingValue, false, 0, true);
// 设置文字居中对齐 WPSApp.Selection.ParagraphFormat.Alignment = WpsParagraphAlignment.wpsAlignParagraphCenter;
// 插入文字,该文字是居中显示的 WPSApp.Selection.Range.Text = "hello,world";
// 插入图片 WPSDocument.Shapes.AddPicture("http://img./publish/kingsoft/images/gb/sy/logo.gif", ref lleft, ref ltop, ref lwidth, ref lheight, ref MissingValue, ref MissingValue, ref MissingValue); }
/// <summary> /// 关闭WPS /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnClose_Click(object sender, System.EventArgs e) { object MissingValue=Type.Missing; object bSave = false; if (WPSApp != null) { // 首先调用WPS.Application的Quit方法,并且不保存修改 WPSApp.Quit(ref bSave, ref MissingValue, ref MissingValue); }
// 释放对象 System.Runtime.InteropServices.Marshal.ReleaseComObject(WPSApp); WPSApp = null; } } }
后期绑定:a) .不引用WPS库,调用WPS方法:
//使用WPS导出PDF,要运行此实例,需要C盘下有一个Demo.wps的文件.
//完成后的效果是在C盘下有一个Demo.pdf文件.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
//添加反射命名空间 using System.Reflection;
namespace CShapeWpsToPdf { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { object WpsApp; object[] args = new object[1]; Type wpsAppName; string progID = "Wps.Application"; wpsAppName = Type.GetTypeFromProgID(progID);
//创建一个WPS实例 WpsApp = Activator.CreateInstance(wpsAppName); args[0] = true;
//设置为可见 wpsAppName.InvokeMember("Visible", BindingFlags.SetProperty, null, WpsApp, args);
//得到Documents对象 Object wpsDocs = wpsAppName.InvokeMember("Documents", BindingFlags.GetProperty, null, WpsApp, null);
//设置关键参数即可,例如: 在打开的方法中,只要指定打开的文件名与是否可见 args = new object[15]; args[0] = @"C:/Demo.wps"; args[11] = true;
//打开C盘下的Demo.wps Object wpsDoc = wpsAppName.InvokeMember("Open", BindingFlags.InvokeMethod, null, wpsDocs, args); args = new object[3];
//生成PDF args[0] = @"C:/Demo.pdf"; wpsAppName.InvokeMember("ExportPdf", BindingFlags.InvokeMethod, null, wpsDoc, args); } } } |
|
来自: 姬风 > 《文档转换(C#)》