C#将Excel转成PDF的方法
程序员文章站
2023-12-12 21:19:40
ps:公司的业务中有个超级大的作业就是把office文档转成pdf,我猜之前没程序猿们,公司那些人应该是一个个手动转。强烈为猿们感叹,帮你们做了这么多事,还在那抱怨.......
ps:公司的业务中有个超级大的作业就是把office文档转成pdf,我猜之前没程序猿们,公司那些人应该是一个个手动转。强烈为猿们感叹,帮你们做了这么多事,还在那抱怨....无法满足你们的需求啊;
微软net平台提供了对office文档非常好的支持;其中有com组件直接集成到了vs中。利用这些api可以快速的免去n多繁琐的工作;
以下代码是翻阅了公司的代码,一个个敲出来的;奉上代码:
using system; using system.collections.generic; using system.io; using system.linq; using system.text; using system.runtime.interopservices; using microsoft.office.interop.excel; //office 命名空间 namespace officetopdf { //excel 类 class excelconverter { //构造函数 public excelconverter() { } /// <summary> /// 转换excel 成pdf文档 /// </summary> /// <param name="_lstrinputfile">原文件路径</param> /// <param name="_lstroutfile">pdf文件输出路径</param> /// <returns>true 成功</returns> public bool convertertopdf(string _lstrinputfile,string _lstroutfile) { microsoft.office.interop.excel.application lobjexcelapp = null; microsoft.office.interop.excel.workbooks lobjexcelworkbooks = null; microsoft.office.interop.excel.workbook lobjexcelworkbook = null; string lstrtemp = string.empty; object lobjmissing = system.reflection.missing.value; try { lobjexcelapp = new microsoft.office.interop.excel.application(); lobjexcelapp.visible = true; lobjexcelworkbooks = lobjexcelapp.workbooks; lobjexcelworkbook = lobjexcelworkbooks.open(_lstrinputfile, true, true, lobjmissing, lobjmissing, lobjmissing, true, lobjmissing, lobjmissing, lobjmissing, lobjmissing, lobjmissing, false, lobjmissing, lobjmissing); //microsoft.office.interop.excel 12.0.0.0之后才有这函数 lstrtemp = system.io.path.gettemppath() + guid.newguid().tostring() + ".xls" + (lobjexcelworkbook.hasvbproject ? 'm' : 'x'); //lstrtemp = system.io.path.gettemppath() + guid.newguid().tostring() + ".xls"; lobjexcelworkbook.saveas(lstrtemp, microsoft.office.interop.excel.xlfileformat.xlexcel4workbook, type.missing, type.missing, type.missing, false, microsoft.office.interop.excel.xlsaveasaccessmode.xlnochange, type.missing, false, type.missing, type.missing, type.missing); //输出为pdf 第一个选项指定转出为pdf,还可以指定为xps格式 lobjexcelworkbook.exportasfixedformat(microsoft.office.interop.excel.xlfixedformattype.xltypepdf, _lstroutfile, microsoft.office.interop.excel.xlfixedformatquality.xlqualitystandard, type.missing, false, type.missing, type.missing, false, type.missing); lobjexcelworkbooks.close(); lobjexcelapp.quit(); } catch (exception ex) { //其他日志操作; return false; } finally { if (lobjexcelworkbook != null) { lobjexcelworkbook.close(type.missing,type.missing,type.missing); marshal.releasecomobject(lobjexcelworkbook); lobjexcelworkbook = null; } if(lobjexcelworkbooks != null) { lobjexcelworkbooks.close(); marshal.releasecomobject(lobjexcelworkbooks); lobjexcelworkbooks = null; } if(lobjexcelapp != null) { lobjexcelapp.quit(); marshal.releasecomobject(lobjexcelapp); lobjexcelapp = null; } //主动激活垃圾回收器,主要是避免超大批量转文档时,内存占用过多,而垃圾回收器并不是时刻都在运行! gc.collect(); gc.waitforpendingfinalizers(); } return true; } } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接