码迷,mamicode.com
首页 > 其他好文 > 详细

WPS 操作Word 替代Office的功能

时间:2017-11-05 14:27:23      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:final   soft   rpe   finalize   false   object   pdf   private   sele   

private void RunWps_Click(object sender, EventArgs e)         {

            object myMissing = System.Reflection.Missing.Value; WPS.Application WpsApp = new WPS.ApplicationClass();             WpsApp.Visible = true;             WpsApp.WindowState = WPS.WpsWindowState.wpsWindowStateMaximize;             WPS.Document WpsDoc = WpsApp.Documents.Add(ref myMissing, false, 1, true);             WpsDoc.Content.Text = "Hello World!";             WPS.Range myRange = WpsDoc.Range(0, 0);             WpsDoc.Paragraphs.Add(myRange);             WpsDoc.Range(0,0).Select();             WpsApp.Selection.Text = "欢迎你学习C#调用WPS";         }

 

C#调用WPS ET 转换pdf

 

先安装WPS

然后添加引用安装目录下的etapp.dll

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.IO; using System.Collections; using ET; namespace ExcelToPdf {public partial class Form1 : Form{int ifsucess = 0;int iffail = 0;int allcount = 0;int num = 0;public Form1(){InitializeComponent();}

public void ConvertExcelToPDF(string xlspath,string pdfpath){try{object type = System.Reflection.Missing.Value;ET.ApplicationClass application = null;ET.workbook book = null;try{application = new ET.ApplicationClass();book = (ET.workbook)application.Workbooks.Open(xlspath, type, type, type, type, type, type, type, type, type, type, type, type);book.ExportPdf(pdfpath, "", "");//this.GetFilePath(path)是获取文件路径+文件名(不含后缀)}catch(Exception ex){MessageBox.Show(ex.Message);}finally{if (book != null){book.Close(true, type, type);book = null;}if (application != null){application.Quit();application = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();}}catch (Exception ex){MessageBox.Show(ex.Message);}}

 

private void button1_Click(object sender, EventArgs e){ConvertExcelToPDF("c:\\test.xls","c:\\test.pdf");}

}

 

}

 

 

 

C#利用WPS生成Word文档:

  1. 首先服务器必须安装WPS Office
  2. 添加引用 -> COM -> Kingsoft WPS 2.0 Object Library
  3. 在.cs文件中添加 using KSO;和using WPS;
  4. 生成一个简单一个doc文件:
           string FileName = Server.MapPath("files/") + "tmp.doc";
   object Nothing = System.Reflection.Missing.Value;
 
   _Application WordApp = new ApplicationClass();
   _Document WordDoc = WordApp.Documents.Add(ref Nothing, false, 0, true);
 
   WordDoc.SaveAs(FileName, ref Nothing, false, "", false, "", false, false, false, false, false, 1, false, false, 20, false);
   WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
   WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

WPS 操作Word 替代Office的功能

标签:final   soft   rpe   finalize   false   object   pdf   private   sele   

原文地址:http://www.cnblogs.com/91loveme/p/7787262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!