今天小编就为大家分享一篇关于C#将Excel转成PDF的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
PS:公司的业务中有个超级大的作业就是把OFFICE文档转成PDF,我猜之前没程序猿们,公司那些人应该是一个个手动转。强烈为猿们感叹,帮你们做了这么多事,还在那抱怨....无法满足你们的需求啊;
微软net平台提供了对Office文档非常好的支持;其中有com组件直接集成到了VS中。利用这些API可以快速的免去N多繁琐的工作;
以下代码是翻阅了公司的代码,一个个敲出来的;奉上代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
//Office 命名空间
namespace OfficeToPdf
{
//excel 类
class ExcelConverter
{
//构造函数
public ExcelConverter()
{ }
/// <summary>
/// 转换excel 成PDF文档
/// </summary>
/// <param name="_lstrInputFile">原文件路径</param>
/// <param name="_lstrOutFile">pdf文件输出路径</param>
/// <returns>true 成功</returns>
public bool ConverterToPdf(string _lstrInputFile,string _lstrOutFile)
{
Microsoft.Office.Interop.Excel.Application lobjExcelApp = null;
Microsoft.Office.Interop.Excel.Workbooks lobjExcelWorkBooks = null;
Microsoft.Office.Interop.Excel.Workbook lobjExcelWorkBook = null;
string lstrTemp = string.Empty;
object lobjMissing = System.Reflection.Missing.Value;
try
{
lobjExcelApp = new Microsoft.Office.Interop.Excel.Application();
lobjExcelApp.Visible = true;
lobjExcelWorkBooks = lobjExcelApp.Workbooks;
lobjExcelWorkBook = lobjExcelWorkBooks.Open(_lstrInputFile, true, true, lobjMissing, lobjMissing, lobjMissing, true,
lobjMissing, lobjMissing, lobjMissing, lobjMissing, lobjMissing, false, lobjMissing, lobjMissing);
//Microsoft.Office.Interop.Excel 12.0.0.0之后才有这函数
lstrTemp = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xls" + (lobjExcelWorkBook.HasVBProject ? 'm' : 'x');
//lstrTemp = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xls";
lobjExcelWorkBook.SaveAs(lstrTemp, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel4Workbook, Type.Missing, Type.Missing, Type.Missing, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
false, Type.Missing, Type.Missing, Type.Missing);
//输出为PDF 第一个选项指定转出为PDF,还可以指定为XPS格式
lobjExcelWorkBook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, _lstrOutFile, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, Type.Missing, false, Type.Missing, Type.Missing, false, Type.Missing);
lobjExcelWorkBooks.Close();
lobjExcelApp.Quit();
}
catch (Exception ex)
{
//其他日志操作;
return false;
}
finally {
if (lobjExcelWorkBook != null)
{
lobjExcelWorkBook.Close(Type.Missing,Type.Missing,Type.Missing);
Marshal.ReleaseComObject(lobjExcelWorkBook);
lobjExcelWorkBook = null;
}
if(lobjExcelWorkBooks != null)
{
lobjExcelWorkBooks.Close();
Marshal.ReleaseComObject(lobjExcelWorkBooks);
lobjExcelWorkBooks = null;
}
if(lobjExcelApp != null)
{
lobjExcelApp.Quit();
Marshal.ReleaseComObject(lobjExcelApp);
lobjExcelApp = null;
}
//主动激活垃圾回收器,主要是避免超大批量转文档时,内存占用过多,而垃圾回收器并不是时刻都在运行!
GC.Collect();
GC.WaitForPendingFinalizers();
}
return true;
}
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对编程学习网的支持。如果你想了解更多相关内容请查看下面相关链接
沃梦达教程
本文标题为:C#将Excel转成PDF的方法
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- C# windows语音识别与朗读实例 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22
- 一个读写csv文件的C#类 2022-11-06
- C# 调用WebService的方法 2023-03-09
- C# List实现行转列的通用方案 2022-11-02
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- ZooKeeper的安装及部署教程 2023-01-22
- winform把Office转成PDF文件 2023-06-14
- C#类和结构详解 2023-05-30