经过Pdf预览Excel或者word或者Powerpoint (C#将Office转换为PDF)

下面代码是Excel转换为PDFweb

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ms = Microsoft.Office.Interop.Excel;
 
    public class Excel2Pdf
    {
        public static void ToPdf(string excelName, string pdfName)
        {
            ms.Application app = new ms.Application();
            ms.Workbook workBook; 
            app.ScreenUpdating = false;
            var workBooks = app.Workbooks;
            workBook = workBooks.Open(HttpContext.Current.Server.MapPath(excelName));
            if (workBook == null)
            {
app.Quit(); app = null; workBook = null; return ; } workBook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, HttpContext.Current.Server.MapPath(pdfName)); workBook.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBooks); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); }
}

 

下面代码是将Word转换为PDFsql

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ms=Microsoft.Office.Interop.Word;
 
    public class Word2Pdf
    {

        public static void ToPdf(string wordName, string pdfName)
        {                        
            ms.Application word=new ms.Application();
            word.ScreenUpdating = false; 
            ms.Document doc=word.Documents.Open(HttpContext.Current.Server.MapPath(wordName));
            doc.ExportAsFixedFormat(HttpContext.Current.Server.MapPath(pdfName),ms.WdExportFormat.wdExportFormatPDF);

            doc.Close();
            word.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
        }
    }
 

 用一样的方法,能够写出Powerpoint转换为PDF的方法安全

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 using ms = Microsoft.Office.Interop.PowerPoint;

    public class Ppt2Pdf
    {
        public static void ToPdf(string pptName, string pdfName)
        { 

            ms.Application app = new ms.Application();
             ms.Presentation pre = null;

            pre=app.Presentations.Open(
                 HttpContext.Current.Server.MapPath(pptName),
                 Microsoft.Office.Core.MsoTriState.msoTrue, 
                 Microsoft.Office.Core.MsoTriState.msoTrue,
                 Microsoft.Office.Core.MsoTriState.msoFalse
                );


            pre.SaveAs(
                HttpContext.Current.Server.MapPath(pdfName),                
                Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF,
                Microsoft.Office.Core.MsoTriState.msoCTrue);


            GC.Collect();
            GC.WaitForPendingFinalizers();



            // https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa679807(v=office.11)
          // System.Runtime.InteropServices.Marshal.ReleaseComObject(cell);

            pre.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pre);

            app.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
        

             //pre.ExportAsFixedFormat(
             //    HttpContext.Current.Server.MapPath(pdfName),
             //    Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF,
             //    Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint,

             //MsoTriState.msoFalse, Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,

             //Microsoft.Office.Interop.PowerPoint.PpPrintOutputType.ppPrintOutputSlides, MsoTriState.msoFalse, null,

             //Microsoft.Office.Interop.PowerPoint.PpPrintRangeType.ppPrintAll, string.Empty, true, true, true,

             //true, false, unknownType);

            
     }
    }
 

   

 上面代码,本机测试运行的很好,可是一旦部署到服务器上面,极可能出现以下错误:服务器

 Microsoft Office Excel 不能访问文件“D:\WWWRoot\\QUOTE5.xls”。

可能的缘由有:

1 文件名称或路径不存在。

2 文件正被其余程序使用。 3

 您正要保存的工做簿与当前打开的工做簿同名。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中致使错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: Microsoft Office Excel 不能访问文

  

 这是由于,默认IIS没法启动Office程序,因此,咱们还要修改IIS。具体以下app

1.以启明星电子文档看为例,系统默认使用edoc应用程序池。在edoc应用程序池上,修改应用程序池标识ide

 

 

2.  在组件服务,DOCM设置 Microsoft Excel Application的属性,由于是在64位系统上面操做,组件服务中DOCOM中默认是没有的,由于Microsoft Excel Application是32的DCOM配置,因此经过以下方式解决(参考第三步)测试

3.    1).开始--〉运行--〉cmd   ui

2)命令提示符下面,输入mmc -32,打开32的控制台   3d

3).文件菜单中,添加删除管理单元--〉组件服务   excel

4).在"DCOM配置"中找到"Microsoft Excel 应用程序",在它上面点击右键,而后点击"属性",弹出"Microsoft Excel 应用程序属性"对话框

 

5).点击"标识"标签,选择"交互式用户"  

6).点击"安全"标签,在"启动和激活权限"上点击"自定义",而后点击对应的"编辑"按钮,在弹出的"安全性"对话框中填加一个"NETWORK SERVICE"用户(注意要选择本计算机名),并给它赋予"本地启动"和"本地激活"权限   

7).依然是"安全"标签,在"访问权限"上点击"自定义",而后点击"编辑",在弹出的"安全性"对话框中也填加一个"NETWORK SERVICE"用户,而后赋予"本地访问"权限.

 

  重复上面步骤,把Word和Powerpoint也加入一样权限。

 

8)在web.config里增长模拟配置:

<identity impersonate="true" userName="系统管理员" password="系统管理员密码"/>

  

 9.从新启动IIS,测试经过

相关文章
相关标签/搜索