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



  • 相关阅读:
    python基础篇-wordcloud库的使用
    python基础篇-数据格式化和处理
    python基础篇-文件(读取,操作,关闭)
    python基础篇-jieba库的使用
    python基础篇-组合数据类型-3.字典
    mbStringLength 获取javascript字符串字节数
    JS 字符unicode转换函数
    jar命令解析--转自百度知道
    JBOSS7.0 热部署及开启远程调试的方法
    WampServer 安装心得
  • 原文地址:https://www.cnblogs.com/xiaoyao095/p/3740811.html
Copyright © 2011-2022 走看看