.net Execl转PDF 在线浏览
作者:互联网
在一些项目中经常会出现需要在系统中查看某些文档数据,这些数据可能又不是在系统数据库中,而是在Execl Word PDF等文件中数据。
PDF还好,但是Execl 跟Word 确是不怎么方便,浏览器并不能直接打开。这时候就需要用到转换,或者第三方插件来实现。
所以在此展示一个通过微软自带的插件来实现将Execl转为PDF的方式进行查看。
1、首先建立一个转换类 ConvertPdf
里面有两个方法,一个是将Execl转为PDF,一个是将Word转换为PDF
****** 需要引用:Microsoft.Office.Interop.Excel 和 Microsoft.Office.Interop.Word 两个DLL
***** 并且将该DLL属性里面的 嵌入互操作类型设置为 False
/// <summary> /// Execl Word 转换为PDF /// </summary> public static class ConvertPdf { /// <summary> /// Word转换Pdf /// </summary> /// <param name="sourcePath"></param> /// <param name="targetPath"></param> /// <param name="exportFormat"></param> /// <returns></returns> public static bool Convert(string sourcePath, string targetPath, Microsoft.Office.Interop.Word.WdExportFormat exportFormat) { bool result; object paramMissing = Type.Missing; Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document wordDocument = null; try { object paramSourceDocPath = sourcePath; string paramExportFilePath = targetPath; Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat; bool paramOpenAfterExport = false; Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint; Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument; int paramStartPage = 0; int paramEndPage = 0; Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent; bool paramIncludeDocProps = true; bool paramKeepIRM = true; Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; bool paramDocStructureTags = true; bool paramBitmapMissingFonts = true; bool paramUseISO19005_1 = false; wordDocument = wordApplication.Documents.Open( ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); if (wordDocument != null) wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); result = true; } finally { if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null; } if (wordApplication != null) { wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; } /// <summary> /// Excel转换PDF方法 /// </summary> /// <param name="sourcePath"></param> /// <param name="targetPath"></param> /// <param name="targetType"></param> /// <returns></returns> public static bool Convert(string sourcePath, string targetPath, Microsoft.Office.Interop.Excel.XlFixedFormatType targetType) { bool result; object missing = Type.Missing; Microsoft.Office.Interop.Excel.ApplicationClass application = null; Microsoft.Office.Interop.Excel.Workbook workBook = null; try { application = new Microsoft.Office.Interop.Excel.ApplicationClass(); object target = targetPath; object type = targetType; workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); result = true; } catch { result = false; } finally { if (workBook != null) { workBook.Close(true, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; } }
2、在控制器中调用转换方法(也可以在其他地方调用)
入参是这个文档的名称(包括后缀)。
ViewBag.PdfFileName = strPDFName; // 是将转换后的PDF的文件名称(自定义);
/// <summary> /// 在线查看Execl /// </summary> /// <returns></returns> public ActionResult SeeExeclInfo(string fileName) { //Execl源文件存储地址 D:\zny\XG\ReportSystem\项目文件\ string ExeclSourseFileUrl = ConfigurationManager.AppSettings["ExeclSourseFileUrl"].ToString(); //转换后PDF文件存储地址 并且是某个IIS站点指向的文件夹 D:\zny\IISItems\ExeclToPDF\ string ExeclToPdfFileUrl = ConfigurationManager.AppSettings["ExeclToPdfFileUrl"].ToString(); //string FileName = "A1DAA3A16.xlsx"; string TempValue = ExeclSourseFileUrl+ fileName; string strPDFName = "A1DAA3A16.pdf"; string PdfFileName = ExeclToPdfFileUrl + strPDFName; try { if (!System.IO.File.Exists(PdfFileName)) { //PDF不存在 则生成PDF放到指定文件夹下 string fileExtension = Path.GetExtension(fileName); if (fileExtension == ".doc" || fileExtension == ".docx") { //Word转PDF ConvertPdf.Convert(TempValue, PdfFileName, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF); ViewBag.PdfFileName = strPDFName; } else if (fileExtension == ".xls" || fileExtension == ".xlsx") { //Execl转PDF ConvertPdf.Convert(TempValue, PdfFileName, Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF); ViewBag.PdfFileName = strPDFName; } } else { ViewBag.PdfFileName = strPDFName; } } catch { Response.Write("请稍后文件等待转换!"); } return View(); }
3、页面展示PDF内容
@using Spire.Xls; @using System.Configuration; @{ Layout = "~/Views/Shared/_LayoutMESReport.cshtml"; } @{ //访问存放PDF文件的IIS站点地址 本机调试地址http://127.0.0.1:8091/ string IISStationPDFUrl = ConfigurationManager.AppSettings["IISStationPDFUrl"].ToString(); //需要新建一个站点专门用于存放转换后的PDF文件。 string pdfUrl = IISStationPDFUrl + ViewBag.PdfFileName; string aa = pdfUrl; } <div style=" width:100%; height:100%; padding-top:20px;"> <span class="layui-breadcrumb" style=" margin-bottom: 20px; float: left; padding-left:20px;width: 100%;"> <a>报表</a> <a href="/PurchaseOrders/ProductTest">款号工序列表</a> <a><cite>工序信息(PDF)</cite></a> </span> <br /> <iframe id="previewpdf" src="@pdfUrl" width="100%" height="1000" frameborder="0"></iframe> </div>
4、最终效果
******* 涉及部分客户信息(已打码)
标签:Execl,Office,missing,Interop,paramMissing,PDF,net,ref,Microsoft 来源: https://www.cnblogs.com/znyCoding/p/15984069.html