Labwindows如何Excel转PDF

因项目需要PDF测试报告,一直以来都是WORD转PDF,网上搜索类似资料也没找到好的方法。于是百度了一波C#的excel转PDF,一大堆信息啊,因为使用的LabWindows/cvi 9.0比较老的版本没法使用vs生成的lib库。没办法只能使用直接调用exe模式了。
先贴上网上看来的一个接口,这个谁是原创也不知道,我一搜索一堆。

using Microsoft.Office.Interop.Excel;
     public static 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;
    }

那么如何与cvi进行数据交互呢?我这边采用的是ini文件,将需要转换的源文件、目标文件信息填入ini文件。然后

static void Main(string[] args)
  {
        string excelfilePath = ReadIni(@"c:\\file_important\\inicfg.ini", "INFO_R", "epath", "DefaultName");
        string pdffilePath = ReadIni(@"c:\\file_important\\inicfg.ini", "INFO_R", "ppath", "DefaultName");

       //  bool result = Program.Convert("C:\\report.xls", "C:\\测试报告", XlFixedFormatType.xlTypePDF);
        bool result = Program.Convert(excelfilePath, pdffilePath, XlFixedFormatType.xlTypePDF);
        //如果转换成功
        if (result)
        {
           
        }

    }

生成的exe在cvi中使用system(“”*.exe“”);即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值