一:excle转换pdf
首先引入using Microsoft.Office.Interop.Excel,如果没有引用里没有,需要从VS里的NuGet程序包里进行下载
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SwitchType
{
public class SwitchPDFType
{
/// <summary>
/// 将excel文档转换成PDF格式
/// </summary>
/// <param name="sourcePath">原文件路径</param>
/// <param name="targetPath">文件转换为PDF保存的路径</param>
/// <param name="targetType">枚举类型参数</param>
/// <returns></returns>
public bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType)
{
bool result;
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Application application = null;
Workbook workBook = null;
try
{
application = new Microsoft.Office.Interop.Excel.Application();
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, 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;
}
}
}
调用Excle转换PDF方法:
Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = (Microsoft.Office.Interop.Excel.XlFixedFormatType)excle;
bool result = switchPDF.Convert(sourcePath, targetPath, targetType);
//如果转换成功
if (result)
{
///if语句里面写自己想做的业务
}