首页 新闻 会员 周边 捐助

用微软的COM组件在将Excel转换成PDF时报错

0
悬赏园豆:5 [已解决问题] 解决于 2018-11-20 11:11

public static bool ExcelConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
            object missing = Type.Missing;
            Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbooks workBook = application.Workbooks;
            try
            {
                application.Visible = false;
                Microsoft.Office.Interop.Excel.Workbook wb= application.Workbooks.Open(sourcePath);
                string tempPath = Path.Combine(Path.GetTempPath(), Path.GetFileName(sourcePath));
                //System.IO.DirectoryInfo DirInfo = new DirectoryInfo(Path.GetTempPath());
                // DirInfo.Attributes = FileAttributes.Archive & FileAttributes.Directory;
                //System.IO.File.SetAttributes(tempPath, System.IO.FileAttributes.Archive);
                wb.SaveAs();
                wb.ExportAsFixedFormat(targetType, targetPath);
                result = true;
            }
            catch (Exception e)
            {
                SystemLogHelper.Logger.Error(e.Message, e.InnerException ?? e);
                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;
        }

丶改的主页 丶改 | 初学一级 | 园豆:78
提问于:2016-08-05 08:50
< >
分享
最佳答案
0

你是老胡吗?

收获园豆:5
小熊vs | 初学一级 |园豆:20 | 2016-08-05 09:30
其他回答(1)
0

推荐你使用 Spire.XLS.dll 的系列组件,比微软的好用多了

顽皮大叔 | 园豆:318 (菜鸟二级) | 2016-08-05 11:30

能讲下具体怎样用吗?

支持(0) 反对(0) 丶改 | 园豆:78 (初学一级) | 2016-08-06 09:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册