预准备:
1.安装microsoft office
2. 安装microsoft office生成pdf插件SaveAsPDFandXPS.exe,可以在msdn上免费得到。
3.在您的工程项目里添加引用:
COM : Microsoft Office 12.0 Object Library ;
.NET : Microsoft.Office.Interop.Excel 、Microsoft.Office.Interop.Word、Microsoft.Office.Interop.PowerPoint
开始编程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.IO;
public class ExportFormatPdf
{
/// <summary>
/// 转换生成pdf格式的文件
/// 支持的文件类型(.doc、.docx、.mht、.htm、.xls、.xlsx、.ppt、pptx)
/// </summary>
/// <param name="fileName">源文件</param>
/// <param name="outputFileName">目标文件</param>
/// <returns></returns>
public static bool ExportPdf(string fileName, string outputFileName)
{
if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(outputFileName))
return false;
if (!File.Exists(fileName))
return false;
string extension = Path.GetExtension(fileName);
string formatExtension = Path.GetExtension(outputFileName);
if (string.IsNullOrEmpty(extension) || string.IsNullOrEmpty(formatExtension))
return false;
if (formatExtension != ".pdf")
return false;
switch (extension)
{
case ".doc":
return WordExportAsPdf(fileName, outputFileName);
case ".docx":
return WordExportAsPdf(fileName, outputFileName);
case ".mht":
return WordExportAsPdf(fileName, outputFileName);
case ".htm":
return WordExportAsPdf(fileName, outputFileName);
case ".html":
return WordExportAsPdf(fileName, outputFileName);
case ".xls":
return ExcelExportAsPdf(fileName, outputFileName);
case ".xlsx":
return ExcelExportAsPdf(fileName, outputFileName);
case ".ppt":
return PowerPointExportAsPdf(fileName, outputFileName);
case ".pptx":
return PowerPointExportAsPdf(fileName, outputFileName);
default:
return false;
}
}
/// <summary>
/// 转换为pdf文件,适合(.doc、.docx、.mht、.htm文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool WordExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
Word.WdExportFormat fileFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Word._Application wordApp = null;
if (wordApp == null) wordApp = new Word.Application();
Word._Document wordDoc = null;
try
{
wordDoc = wordApp.Documents.Open(fileName);
wordDoc.ExportAsFixedFormat(outputFileName, fileFormat);
isSucceed = true;
}
finally
{
if (wordDoc != null)
{
wordDoc.Close();
wordDoc = null;
}
if (wordApp != null)
{
wordApp.Quit();
wordApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return isSucceed;
}
/// <summary>
/// 转换为pdf文件,适合(.xls、.xlsx文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool ExcelExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
Excel.XlFixedFormatType fileFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
Excel.Application excelApp = null;
if (excelApp == null) excelApp = new Excel.Application();
Excel.Workbook workBook = null;
try
{
workBook = excelApp.Workbooks.Open(fileName);
workBook.ExportAsFixedFormat(fileFormat, outputFileName);
isSucceed = true;
}
finally
{
if (workBook != null)
{
workBook.Close();
workBook = null;
}
if (excelApp != null)
{
excelApp.Quit();
excelApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return isSucceed;
}
/// <summary>
/// 转换为pdf文件,适合(.ppt、pptx文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool PowerPointExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
PowerPoint.PpFixedFormatType fileFormat = PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF;
PowerPoint.Application pptxApp = null;
if (pptxApp == null) pptxApp = new PowerPoint.Application();
PowerPoint.Presentation presentation = null;
try
{
presentation = pptxApp.Presentations.Open(fileName, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
presentation.ExportAsFixedFormat(outputFileName, fileFormat);
isSucceed = true;
}
finally
{
if (presentation != null)
{
presentation.Close();
presentation = null;
}
if (pptxApp != null)
{
pptxApp.Quit();
pptxApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return isSucceed;
}
}
完毕 ,拿来一个上述支持的格式的文件来做试试,您将看到生成完美的pdf文件了。自从有了它,本人终于放松神经了,相信很多需要生成pdf的同仁们看完这篇文章后也该放松神经了。