asp.net将Excel文档转换成pdf
类别:编程学习 浏览量:3173
时间:2014-2-24 asp.net将Excel文档转换成pdf
asp.net将Excel文档转换成pdf一、添加引用
using Microsoft.Office.Interop.Excel;
二、转换方法
1、方法
/// <summary>
/// 把Excel文件转换成pdf文件
/// </summary>
/// <param name="sourcePath">需要转换的文件路径和文件名称</param>
/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
/// <returns></returns>
public static bool ExcelToPdf(string sourcePath, string targetPath)
{
bool result = false;
XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
Workbook workbook = null;
try
{
applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
string inputfileName = sourcePath;//需要转格式的文件路径
string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称
XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量
bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
bool openAfterPublish = false;//发布后不打开
workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
if (workbook!=null)
{
workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
}
result = true;
}
catch
{
result = false;
}
finally
{
if (workbook != null)
{
workbook.Close(true, missing, missing);
workbook = null;
}
if (applicationClass != null)
{
applicationClass.Quit();
applicationClass = null;
}
}
return result;
}
2、简洁方法
/// <summary>
/// 把Excel文件转换成pdf文件
/// </summary>
/// <param name="sourcePath">需要转换的文件路径和文件名称</param>
/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
/// <returns></returns>
public static bool ExcelToPdf(string sourcePath, string targetPath)
{
bool result = false;
XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
Workbook workbook = null;
try
{
applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
workbook = applicationClass.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
if (workbook != null)
{
workbook.ExportAsFixedFormat(xlTypePDF, targetPath, 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 (applicationClass != null)
{
applicationClass.Quit();
applicationClass = null;
}
}
return result;
}
三、调用
OfficeToPdf.ExcelToPdf("d:\\1234.xls", "d:\\1234.pdf");
您可能感兴趣
- php使用yield处理并发(Yii框架中使用PHPExcel的方法分析)
- Asp.net导出Excel乱码
- python调用excel教程(利用python在excel里面直接使用sql函数的方法)
- pythonexcel生成报表(python生成每日报表数据Excel并邮件发送的实例)
- ASP.NET将Excel数据导入到数据库
- vue原样表格导出excel(Vue导出Excel功能的全过程记录)
- python处理excel基础教程第3讲(Python Excel处理库openpyxl使用详解)
- python实现将txt转化为excel(python实现Excel文件转换为TXT文件)
- pythonexcel报表实例(对python生成业务报表的实例详解)
- pandas读取excel数据教程(详解pandas库pd.read_excel操作读取excel文件参数整理与实例)
- asp.net将Excel文档转换成pdf
- python excel 合并表格(Python实现合并excel表格的方法分析)
- python分析excel基础数据生成报表(Python实现定制自动化业务流量报表周报功能XlsxWriter模块)
- C#如何读取Excel
- 如何用python处理excel表格(零基础使用Python读写处理Excel表格的方法)
- python获取excelsheet名称(python查询文件夹下excel的sheet名代码实例)
- 泰国旅游攻略(泰国旅游攻略必去景点)
- 数字藏品市场有多乱 周杰伦丢了 一只猴 ,损失超300万(数字藏品市场有多乱)
- 这里输入关键词(怎么输入关键词搜索)
- 得这个 难治病 的人太多了,300个人赶到杭州商量怎么办(得这个难治病的人太多了)
- 经度,世界时间腕表的灵魂(世界时间腕表的灵魂)
- 阿里最新财报公布 三季度营收增长3 ,将增加150亿美元回购额度 在美股价小涨(阿里最新财报公布)
热门推荐
- sql中把一列数据相加(SQL实现相邻两行数据的加减乘除操作)
- dedecms搜索功能怎么设置详细(织梦dedecms文章列表页随机放入广告的方法)
- Sql Server常用系统存储过程
- pythonjson库(Python常用的json标准库)
- pythonselenium判断网页加载完成(python 实现selenium断言和验证的方法)
- 如何启动wampserver数据库服务(wampserver更改默认站点目录www方法教程)
- 少用iframe的原因
- react 查看word文件(React实现导入导出Excel文件)
- linuxmysql安装教程5.7.25学习(linux mysql5.5升级至mysql5.7的步骤与踩到的坑)
- html5菜单栏(html5写一个BUI折叠菜单插件的实现方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9