Wps调用dll操作Excel表格转PDF
起始原因:wps编辑创建的文档在microsoft office 中打开,会报内容存在异常是否恢复,因此wps文件被微软设定为破损文件,无法对原有文档进行操作运行,故在此使用wps对Excel进行操作。
环境要求:首先安装wps专业版或企业版安装包,并注册,未注册的软件,程序调用时无法正常启动,它会启动注册界面,无法对Excel进行实际代码操作。
安装成功后,在程序中引用两个com组件,该组件为wps注册程序引用的dll文件
下边一个为Excel的dll文件,上面的是wps程序的关键dll文件
开发环境说明
Vs .net core集成操作wps
实质:通过程序调用wps客户端,启动程序后对其进行需要的操作,以加载Excel文件并保存为PDF为例
Source为源文件地址,target为目标PDF文件地址
public void newapp(string source, string target)
{
Excel.ApplicationClass mexcel = new Excel.ApplicationClass();
object missing = Type.Missing;
try
{
mexcel.Visible = false;
Excel.Workbook mdoc = mexcel.Workbooks.Open(source, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
mdoc.Save();
//保存为Pdf
mdoc.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
mdoc.Close(missing, missing, missing);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
//System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
mexcel.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
如果后期部署的话,无法找到对应dll文件,或者安装的wps版本不一致,那么可能会导致无法通过注册表启动程序,需要将对应的dll引用到发布包下
Wps必备引用ksoapi.dll
excel必备引用:etapi.dll
Dll的位置均在wps的安装路径下的\wps\WPS Office\11.1.0.12598\office6\
本文来自博客园,作者:zwbsoft,转载请注明原文链接:https://www.cnblogs.com/zwbsoft/p/16951809.html
电话微信:13514280351