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 。这时就操作完成了!



  • 相关阅读:
    AS3 Signals
    Activate、Deactivate 事件 Activate ThrottleEvent;
    Git 单机版
    SVN 快速入门
    Git 简介
    SVN
    Git
    Python 对目录做遍历
    Python hashlib 模块
    __name__
  • 原文地址:https://www.cnblogs.com/xiaoyao095/p/3740811.html
Copyright © 2011-2022 走看看