經過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,測試經過

相關文章
相關標籤/搜索