zoukankan      html  css  js  c++  java
  • [WPF]读取excel

      /// <summary>
            /// 将excel文档转换成PDF格式
            /// </summary>
            /// <param name="sourcePath">原文件路径</param>
            /// <param name="targetPath">文件转换为PDF保存的路径</param>
            /// <param name="targetType">枚举类型参数</param>
            /// <returns></returns>
            private bool ExcelConvert(string sourcePath, string targetPath, XlFixedFormatType targetType)
            {
                bool result;
                object missing = Type.Missing;
                Microsoft.Office.Interop.Excel.Application application = null;
                Workbook workBook = null;
                try
                {
                    if (!File.Exists(targetPath))
                    {
                        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;
                    }
                    else
                    {
                        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;
            }
    
    
    //页面调用
     bool result = ExcelConvert(path + filename, path + filename + ".xps", XlFixedFormatType.xlTypeXPS);
                            if (result)
                            {
                                DocumentViewer docViewer = new DocumentViewer();
                                docViewer.Document = new XpsDocument((path + filename + ".xps"), System.IO.FileAccess.Read).GetFixedDocumentSequence();
    
                                docViewer.FitToWidth();
                                grid1.Children.Add(docViewer);
                                docViewer.SetValue(Grid.RowProperty, 0);
                                docViewer.SetValue(Grid.ColumnProperty, 0);
                            }
                            else
                            {
                                Xceed.Wpf.Toolkit.MessageBox.Show("系统调用有误,是否安装Office软件");
                            }
  • 相关阅读:
    京东二面面经(07.17 11:30)
    招银三面手撕代码题(字符串连续子串)
    shein二面(31min)
    京东提前批一面
    两个链表的第一个公共结点
    Java并发机制的底层实现原理
    招银网络(二面07.09)
    黑盒测试与白盒测试
    求1+2+...+n(剑指offer-47)
    第一个只出现一次的字符(剑指offer-34)
  • 原文地址:https://www.cnblogs.com/wdd812674802/p/10947618.html
Copyright © 2011-2022 走看看