c#开发 Excel转PDF编程中ExportAsFixedFormat问题解决案

qqwmly 2014-11-04 05:20:01
利用c#把 excel转化成pdf格式,
从网上找了些代码,试验了下
结果调试时候报错,ex.message:出现异常值不在预期的范围内

出错的代码如下


private 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 Microsoft.Office.Interop.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);
workBook.ExportAsFixedFormat(targetType, target,
Excel.XlFixedFormatQuality.xlQualityStandard,
true, false, missing, missing, missing, missing);
result = true;
}
catch (Exception ex)
{
LogWriter.WriteToTxT(ex.Message);
LogWriter.WriteToTxT("PDF生成失败");
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;
}


错误的地方出现在这里,
if (excelWorkBook != null)
{
workBook.ExportAsFixedFormat(targetType, target,
Excel.XlFixedFormatQuality.xlQualityStandard,
true, false, missing, missing, missing, missing);
}

调试时候报错,ex.message:出现异常值不在预期的范围内

网上有人给的例子结果是

引用 1 楼 ray924922279 的回复:
Microsoft.Office.Interop.Excel.XlFixedFormatType paramExportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
Microsoft.Office.Interop.Excel.XlFixedFormatQuality paramExportQuality = Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard;

应该是enum类型

enum Microsoft.Office.Interop.Excel.XlFixedFormatType paramExportFormat = enum Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
enum Microsoft.Office.Interop.Excel.XlFixedFormatQuality paramExportQuality = enum Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard;


有知道具体解决方案的大虾吗
...全文
914 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
品铭工作室 2015-12-11
  • 打赏
  • 举报
回复
是由于excel中有计算表达式,把 .Excel.XlFixedFormatQuality.xlQualityStandard; 改成 XlFixedFormatQuality.xlQualityMinimum

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧