這篇文章將為大家詳細講解有關C#中怎么將Excel轉成PDF,文章內(nèi)容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
創(chuàng)新互聯(lián)長期為上千多家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為曹妃甸企業(yè)提供專業(yè)的做網(wǎng)站、網(wǎng)站建設,曹妃甸網(wǎng)站改版等技術服務。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
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 { //構造函數(shù) 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之后才有這函數(shù) 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; } //主動激活垃圾回收器,主要是避免超大批量轉文檔時,內(nèi)存占用過多,而垃圾回收器并不是時刻都在運行! GC.Collect(); GC.WaitForPendingFinalizers(); } return true; } } }
關于C#中怎么將Excel轉成PDF就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)站標題:C#中怎么將Excel轉成PDF
分享鏈接:http://aaarwkj.com/article26/iggjcg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、、虛擬主機、網(wǎng)站設計公司、ChatGPT、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)