大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关C#中怎么将Excel转成PDF,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联长期为上千多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为曹妃甸企业提供专业的做网站、网站建设,曹妃甸网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
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() { } ////// 转换excel 成PDF文档 /// /// 原文件路径 /// pdf文件输出路径 ///true 成功 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; } } }
关于C#中怎么将Excel转成PDF就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。