1.示意代码
下面代码是Excel转换为PDF
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转换为PDF
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 不能访问文
2.权限设置
这是因为,默认IIS无法启动Office程序,所以,我们还要修改IIS。具体如下
1.以启明星电子文档看为例,系统默认使用edoc应用程序池。在edoc应用程序池上,修改应用程序池标识。使用内置的administrator账户。
2.2.1 在IIS应用程序池里找到 edoc,点击“高级设置”,在标识里选择 自定义用户,选择 “自定义账户”,选择 LocalSystem
2.2.2 在开始--〉运行--〉cmd
2.2.3 命令提示符下面,输入mmc -32,打开32的控制台
2.2.4 文件菜单中,添加删除管理单元--〉组件服务
2.2.5 在"DCOM配置"中找到"Microsoft Excel 应用程序", 在它上面点击右键,然后点击"属性",弹出"Microsoft Excel 应用程序属性"对话框
2.2.6 点击"标识"标签,选择"下列用户" ,然后选择“Administrator”
2.2.7 点击"安全"标签,在"启动和激活权限"上点击"自定义",然后点击对应的"编辑"按钮,在弹出的"安全性"对话框中填加一个"NETWORK SERVICE"用户(注意要选择本计算机名),并给它赋予"本地启动"和"本地激活"权限
2.2.8 依然是"安全"标签,在"访问权限"上点击"自定义",然后点击"编辑",在弹出的"安全性"对话框中也填加一个"NETWORK SERVICE"用户,然后赋予"本地访问"权限.
2.2.9 上面步骤主要是让启明星电子文档库能够操作Excel
2.2.10 重复上面2.2.5步骤,在"DCOM配置"中找到"Microsoft Word 97-2003 应用程序...点击属性。。。 以下步骤和上面设置Excel一样。
2.2.11 重复上面2.2.5步骤,在"DCOM配置"中找到"Microsoft PowerPoint 应用程序...点击属性。。。 以下步骤和上面设置Excel一样。
2.2.12 重启电脑
*在上面测试中,有时候能成功,有时候并未成功。