首先安装WORD和WPS(2005个人版就OK了)
操作WPS配置说明
using WPS;
1.运行Dcomcnfg.exe
2.组件服务――计算机――我的电脑――DCOM配置――找到WPS文档
3.点击属性
4.选择“安全性”
5.选定“使用自定义访问权限”和“使用自定义启动权限”
6.分别编辑权限,添加Everyone(ASPNET,VS Developers,Debugger User)
7.选择“身份标识”,在选定“交互式用户” 即可
9.在Web.config里加 <identity impersonate="true"/>
转换为WPS打开,转化为PDF
WPS.Application WPSApp;
object MissingValue=Type.Missing;
WPSApp = new WPS.ApplicationClass();
WPSApp.Visible = false;
object Unknown =Type.Missing;
String upPath;
upPath=Server.MapPath("tempup\\" +Request.Cookies["code"].Value +"\\");
if(!Directory.Exists(upPath))
Directory.CreateDirectory(upPath);
object wpsurl = upPath +upfile.PostedFile.FileName.Substring(upfile.PostedFile.FileName.LastIndexOf("\\"),upfile.PostedFile.FileName.Length - upfile.PostedFile.FileName.LastIndexOf("\\"));
//string wpsurl=Source2.ToString();
object wpsurls=wpsurl+".pdf";
WPS.Document WPSDocument = WPSApp.Documents.Add(ref wpsurl, false, 0, true);
WPSDocument.ExportPdf(wpsurl+".pdf","","");
db.dbconn conn = new dbconn();
string sql;
sql=@"update maindata set pdf_furl='"+wpsurls+"'";
conn.doexecsql(sql);
object save = false;
WPSApp.Quit(ref save,ref Unknown,ref Unknown);
System.Runtime.InteropServices.Marshal.ReleaseComObject(WPSApp);
//使用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);
}
}
}
该对象代表 ET 应用程序。包含可返回最高级对象的属性和方法。
Class Application
说明
如要使用另一模块的自动功能来控制WPS,要使用 CreateObject 或者 GetObject 函数来返回 WPS Application对象。
属性列表
| 属性 | 描述 |
|---|---|
| ActiveCell | |
| ActivePrinter | |
| ActiveSheet | |
| ActiveWindow | |
| ActiveWorkbook | |
| AutoRecover | |
| Calculation | |
| Caption | |
| Cells | |
| Columns | |
| COMAddIns | |
| CommandBars | |
| CopyObjectsWithCells | |
| Cursor | |
| CustomListCount | |
| CutCopyMode | |
| DefaultFilePath | |
| DefaultWorkbookName | |
| DefaultWorksheetName | |
| DisplayAlerts | |
| DisplayFormulaBar | |
| DisplayRecentFiles | |
| DisplayStatusBar | |
| FixedDecimal | |
| FixedDecimalPlaces | |
| Iteration | |
| MaxChange | |
| MaxIterations | |
| MoveAfterReturn | |
| MoveAfterReturnDirection | |
| Name | |
| Names | |
| Range | |
| RecentFiles | |
| ReferenceStyle | |
| Rows | |
| SaveAsFileType | |
| SaveBackupFileOnFirstSave | |
| Selection | |
| Sheets | |
| SheetsInNewWorkbook | |
| ShowChartTipNames | |
| ShowChartTipValues | |
| ShowStartupDialog | |
| StandardFont | |
| StandardFontSize | |
| StatusBar | |
| UserControl | |
| UserName | |
| Version | |
| Visible | |
| Windows | |
| Workbooks | |
| Worksheets |
方法列表
| 方法 | 描述 |
|---|---|
| AddCustomList | |
| DeleteCustomList | |
| GetCustomListContents | |
| GetCustomListNum | |
| GetSaveAsFilename | |
| InchesToPoints | |
| OnKey | |
| Quit | |
| Redo | |
| Run | |
| SetDefaultChart | |
| Terminate | |
| Undo |
事件列表
| 事件 | 描述 |
|---|---|
| WorkbookOpen | |
| WorkbookBeforeClose | |
| WorkbookBeforeSave | |
| WorkbookActivate | |
| WorkbookDeactivate | |
| SheetActivate | |
| SheetDeactivate | |
| SheetBeforeDoubleClick | |
| SheetBeforeRightClick | |
| WindowActivate | |
| WindowDeactivate |
该对象代表 WPS 的一篇文档。
Document 对象是 Documents 集合中的一个元素。Documents 集合包含 WPS 应用程序当前打开的所有文档。
Class Document
说明
用 Documents(index) 可返回单个的 Document 对象,其中 index 是文档的名称或索引序号。
属性列表
| 属性 | 描述 |
|---|---|
| ActiveWindow | |
| AttachedTemplate | |
| Bookmarks | |
| BuiltInDocumentProperties | |
| Comments | |
| Content | |
| CustomDocumentProperties | |
| DefaultTabStop | |
| Endnotes | |
| Fields | |
| Footnotes | |
| FullName | |
| GridDistanceHorizontal | |
| GridDistanceVertical | |
| GridOriginFromMargin | |
| GridOriginHorizontal | |
| GridOriginVertical | |
| GridSpaceBetweenHorizontalLines | |
| GridSpaceBetweenVerticalLines | |
| HasPassword | |
| Hyperlinks | |
| InlineShapes | |
| JustificationMode | |
| KerningByAlgorithm | |
| ListParagraphs | |
| Lists | |
| ListTemplates | |
| Name | |
| NoLineBreakAfter | |
| NoLineBreakBefore | |
| OpenEncoding | |
| PageSetup | |
| Paragraphs | |
| Password | |
| PasswordEncryptionAlgorithm | |
| PasswordEncryptionFileProperties | |
| PasswordEncryptionKeyLength | |
| PasswordEncryptionProvider | |
| Path | |
| PrintRevisions | |
| ProtectionType | |
| ReadOnly | |
| RemoveDateAndTime | |
| Revisions | |
| Saved | |
| SaveEncoding | |
| SaveFormat | |
| Sections | |
| Shapes | |
| ShowRevisions | |
| SnapToGrid | |
| SnapToShapes | |
| Styles | |
| Tables | |
| TablesOfContents | |
| TablesOfFigures | |
| TextLineEnding | |
| TrackRevisions | |
| UpdateStylesOnOpen | |
| UserControl | |
| Windows | |
| WordStat | |
| WritePassword |
方法列表
| 方法 | 描述 |
|---|---|
| AcceptAllRevisions | |
| AcceptAllRevisionsShown | |
| BeginJob | |
| Close | |
| ClosePrintPreview | |
| DeleteAllComments | |
| DeleteAllCommentsShown | |
| EndJob | |
| ExportPdf | |
| FitToPages | |
| PrintPreview | |
| Protect | |
| Range | |
| Redo | |
| RejectAllRevisions | |
| RejectAllRevisionsShown | |
| Repaginate | |
| Save | |
| SaveAs | |
| SendMail | |
| Undo | |
| Unprotect |
浙公网安备 33010602011771号