博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET 用 Office COM 组件将 docx\pptx\xlsx 转换成 PDF 文件
阅读量:4922 次
发布时间:2019-06-11

本文共 6608 字,大约阅读时间需要 22 分钟。

本文内容

  • 软件环境
  • 解决方案结构
  • 演示 ASP.NET 用 Office COM 组件将 docx\pptx\xlsx 转换成 PDF 文件

 

软件环境


  • VS 2010 framework 4.0
  • Windows 7 旗舰版
  • Office 2007

 

解决方案结构


  • Upload 目录。客户端上传目录。
  • PDF 目录。上传后,转换成 PDF 文件的目录。

 

演示 ASP.NET 用 Office COM 组件将 docx\pptx\xlsx 转换成 PDF 文件


ConvertToPDF 类将 docx\pptx\xlsx 转换成 PDF 文件,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
 
namespace ConvertToPDF
{
public class ConvertToPDF
{
public ConvertToPDF()
{
}
/// 
/// DOC 2 PDF
/// 
/// 源路径
/// 目标路径
/// 
public bool DOC2PDF(string sourcePath, string targetPath)
{
bool result = false;
Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
object paramMissing = Type.Missing;
Word.ApplicationClass wordApplication = new Word.ApplicationClass();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
 
Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Word.WdExportCreateBookmarks paramCreateBookmarks = 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;
}
catch
{
result = false;
}
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;
}
/// 
/// XLS 2 PDF
/// 
/// 源路径
/// 目标路径
/// 
public bool XLS2PDF(string sourcePath, string targetPath)
{
bool result = false;
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new 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, 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;
}
/// 
/// PPT 2 PDF
/// 
/// 源路径
/// 目标路径
/// 
public bool PPT2PDF(string sourcePath, string targetPath)
{
bool result;
PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
PowerPoint.ApplicationClass application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
 
result = true;
}
catch
{
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
}
}

通过如下方式添加 Office COM 组件。

页面代码如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ConvertToPDF._Default" %>
 
 
 
 
 
 
 
background-color: #8ABAE3; padding: 20px 20px 20px 20px;">
          
选择列表框中的文件,查看:
 
 
 
 

后台代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
 
namespace ConvertToPDF
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
 
}
 
/// 
/// 文档上传
/// 
/// 
/// 
protected void Btn_Upload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string fileName = FileUpload1.FileName;
string fileExtention = FileUpload1.FileName.Substring(fileName.LastIndexOf(".") + 1);
string filePath = HttpContext.Current.Server.MapPath("~/Upload\\") + fileName;
FileUpload1.SaveAs(filePath);
ListBox1.Items.Add(new ListItem(fileName, fileName));
 
bool bl = Convert(fileName, fileExtention);
Page.ClientScript.RegisterStartupScript(this.GetType(), "", bl ? "" : "");
}
catch
{
 
}
}
}
 
private bool Convert(string fileName, string fileExtention)
{
string srcPath = HttpContext.Current.Server.MapPath("~/Upload\\") + fileName;
string targetPath = HttpContext.Current.Server.MapPath("~/PDF\\") + fileName.Replace(".","-") + ".pdf";
 
ConvertToPDF c = new ConvertToPDF();
bool bl = false;
switch (fileExtention.ToUpper())
{
case "DOC":
case "DOCX":
bl = c.DOC2PDF(srcPath, targetPath);
break;
case "XLS":
case "XLSX":
bl = c.XLS2PDF(srcPath, targetPath);
break;
case "PPT":
case "PPTX":
bl = c.PPT2PDF(srcPath, targetPath);
break;
default:
bl = false;
break;
}
return bl;
}
}
}

运行结果:

o_%E5%8E%9F%E5%88%9B.jpg

转载于:https://www.cnblogs.com/liuning8023/archive/2013/03/04/2943482.html

你可能感兴趣的文章
https和http共存的nginx简单配置
查看>>
利用WGET下载文件,并保存到指定目录
查看>>
AO代码:VB6代码迁移到NET的几大理由(摘自ESRI文档片段)
查看>>
HDOJ 1518 Square
查看>>
Finance 网站收集
查看>>
如何使用Git 下载GitHub的东西
查看>>
C#实现联合体
查看>>
[BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]
查看>>
nignx 502错误不能使用/的路径方式 即pathinfo
查看>>
java APIs for database -------- JDBC (2)<statement>
查看>>
Xpath
查看>>
LoadRunner 参数化之 连接数据库进行参数化
查看>>
bugku秋名山老司机+写博客的第一天
查看>>
小程序,用js获取当前系统时间并显示
查看>>
数据库sql的主要关键字的执行顺序
查看>>
Linux之Libcurl库的介绍与应用20170509
查看>>
通过sqlserver日志恢复误删除的数据
查看>>
adb连接手机的两种方式
查看>>
知识点
查看>>
CentOS7 安装Redis 3.2.3
查看>>