Read excel wpf

  ///  <Summary> 
        /// convert excel document into PDF format
         ///  </ Summary> 
        ///  <param name = "the sourcePath"> original file path </ param> 
        ///  <param name = "The targetPath "> file conversion to PDF saved path </ param> 
        ///  <param name =" the targetType "> enumeration type parameter </ param> 
        ///  <Returns> </ Returns> 
        Private  BOOL ExcelConvert ( String the sourcePath, String The targetPath, XlFixedFormatType the 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 ); 
                        } 
                        the else 
                        { 
                            Xceed.Wpf.Toolkit.MessageBox.Show ( " system call error, Office software is installed " ); 
                        }

 

Guess you like

Origin www.cnblogs.com/wdd812674802/p/10947618.html