[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軟件");
                        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM