C# use Microsoft.Office.Interop.Excel.Application open excel but opened wps office excel

刘 麒辉 1 Reputation point
2021-04-23T07:02:22.843+00:00

use Microsoft.Office.Interop.Excel.Application open excel but opened wps office excel
90661-image.png

Microsoft Office 2016 and wps office is installed on my computer
WPS office is my default startup item
But I need to use Microsoft.Office.Interop.Excel.Application to operate the Microsoft Office
I checked it on Google and it seems that I didn’t find the same problem.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,703 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2021-04-27T03:02:23.623+00:00

    I will show my demo code for you, it can run well on the device which installed both WPS and Office. You need to install Microsoft.Office.Interop.Excel in Nuget and add ReachFramework.dll in project References. My ReachFramework.dll path is:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8\ReachFramework.dll
    Xaml code part:

     <Grid>  
            <DocumentViewer HorizontalAlignment="Left" Margin="0,42,0,0"  Name="documentViewer1" VerticalAlignment="Top" Height="508" Width="766" />  
            <TextBox Height="29" HorizontalAlignment="Left" Margin="6,6,0,0"  Name="SelectedFileTextBox" VerticalAlignment="Top" Width="276" />  
            <Button Content="Browse" Height="30" HorizontalAlignment="Right" Margin="0,6,353,0"   Name="BrowseButton" VerticalAlignment="Top" Width="122" Click="BrowseButton_Click" />  
        </Grid>  
    

    C# code part:

    using System;  
    using System.Windows;  
    using System.Windows.Xps.Packaging;  
    using Microsoft.Office.Interop.Excel;  
      
    namespace OpenExcel  
    {  
        public partial class MainWindow : System.Windows.Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
            }  
      
            private XpsDocument ConvertExcelToXPSDoc(string excelDocName, string xpsDocName)  
            {  
                Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();  
                Workbook excelWorkbook = excelApplication.Workbooks.Open(excelDocName);  
                try  
                {  
                    excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS, Filename: xpsDocName, OpenAfterPublish: false);  
                    excelApplication.Quit();  
                    XpsDocument xpsDoc = new XpsDocument(xpsDocName, System.IO.FileAccess.Read);  
                    return xpsDoc;  
                }  
                catch (Exception exp)  
                {  
                    string str = exp.Message;  
                }  
                return null;  
      
            }  
      
      
            private void BrowseButton_Click(object sender, RoutedEventArgs e)  
            {  
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();  
      
                dlg.DefaultExt = ".xls|.xlsx";  
                dlg.Filter = "(.xls)|*.xls|(.xlsx)|*.xlsx";  
      
                Nullable<bool> result = dlg.ShowDialog();  
      
                if (result == true)  
                {  
                    if (dlg.FileName.Length > 0)  
                    {  
                        SelectedFileTextBox.Text = dlg.FileName;  
                        string newXPSDocumentName = String.Concat(System.IO.Path.GetDirectoryName(dlg.FileName), "\\",  
                                       System.IO.Path.GetFileNameWithoutExtension(dlg.FileName), ".xps");  
                        documentViewer1.Document =  
                            ConvertExcelToXPSDoc(dlg.FileName, newXPSDocumentName).GetFixedDocumentSequence();  
                    }  
                }  
            }  
           
        }  
    }  
    

    The result picture is:

    91567-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments