zoukankan      html  css  js  c++  java
  • Asp.Net Word、Excel转PDF在线预览

    第一步建一个类(ConvertPdf.cs)存放此代码

    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转换方法 /// </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; } }
     第二步:在事件或者方法下面调用即可
    string TempValue = Server.MapPath("../../../UploadFiles/OAFiles") + "\" + this.FileName; string SaveValue = Server.MapPath("../../../UpConvertPDF/OAFiles") + "\"; string PdfFileName = Guid.NewGuid().ToString() + ".pdf"; try { string fileExtension = Path.GetExtension(this.FileName); if (fileExtension == ".doc" || fileExtension == ".docx") { ConvertPdf.Convert(TempValue, SaveValue + PdfFileName, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF); OpenOnlineFile(PdfFileName); } else if (fileExtension == ".xls" || fileExtension == ".xlsx") { ConvertPdf.Convert(TempValue, SaveValue + PdfFileName, Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF); OpenOnlineFile(PdfFileName); } } catch { Response.Write("请稍后文件等待转换!"); }

          说明:在引用Microsoft.Office.Interop.Word 或者Microsoft.Office.Interop.Excel的时候可能还会报错,这时打开引用,选中Microsoft.Office.Interop.Word 右键-属性 将嵌入互操作类型 设为:false 。这时就操作完成了!



  • 相关阅读:
    dapper 批量删除、新增、修改说明
    android 加载assets目录下的静态html文件案例
    webstorm中使用git提交代码时出现unversioned files错误
    windows server 2008 R2 x64 部署.net core 3.1项目
    asp.net core 项目添加nlog日志(loggerFactor.AddNLog 过时处理(.net core 3.1))
    机器学习笔记之一步步教你轻松学主成分分析PCA降维算法
    机器学习笔记之类别特征处理
    机器学习笔记之range, numpy.arange 和 numpy.linspace的区别
    机器学习笔记之Numpy的random函数
    机器学习笔记之矩阵分解 SVD奇异值分解
  • 原文地址:https://www.cnblogs.com/xiaoyao095/p/3740811.html
Copyright © 2011-2022 走看看