excel转存pdf失败,报错
2015-07-01 12:34
381 查看
问题:Excel转存pdf失败,报错远程过程调用失败
转存方法:
public static bool XLSConvertToPDF(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);
logger.Info("开始转换成pdf");
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
logger.Info("转换成pdf完成");
result = true;
}
catch (Exception e)
{
result = false;
logger.Error(e.Message, e);
}
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;
}
解决思路:代码调试没有问题,发布环境问题,调用com组件的权限设置
解决方法:控制面板-组件服务—我的电脑—属性—COM安全—编辑默认值 添加各种用户权限,主要是ASP用户
转存方法:
public static bool XLSConvertToPDF(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);
logger.Info("开始转换成pdf");
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
logger.Info("转换成pdf完成");
result = true;
}
catch (Exception e)
{
result = false;
logger.Error(e.Message, e);
}
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;
}
解决思路:代码调试没有问题,发布环境问题,调用com组件的权限设置
解决方法:控制面板-组件服务—我的电脑—属性—COM安全—编辑默认值 添加各种用户权限,主要是ASP用户
相关文章推荐
- eclipse 配置maven web项目
- 我们怎样才能学会?做一个学者或学生?
- FTP规范
- 错误:指定的任务可执行文件位置 D:\Android\platform-tools\aapt.exe 无效
- 利用grub2制作多启动U盘
- 谈谈JAVA工程狮面试中经常遇到的面试题目------什么是MVC设计模式
- 多线程程序框架-简单多线程程序
- AndroidManifest.xml——intent-filter
- 网页设计、web前端、后台的开发流程和注意事项
- JavaScript每天定时更换皮肤样式的方法
- Unity3D Vector3.Dot 点乘 与 Vector3.Cross 叉乘
- Linux LVM学习总结——创建卷组VG
- 利用grub2制作多启动U盘
- Java 递归解决 "汉诺塔" 问题
- UIScrollView 和UIPageControl 实现app启动滑动图
- HDU 4828 (卡特兰数+逆)
- linux集群时间同步
- poj 1021 2D-Nim 模拟
- Java Jersey2使用总结
- sed 删除换行符