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:
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.