WPF app that display Excel file.

I tryed to display the Microsoft Excel file in a WPF app. but, I was not able to find it out by web about to do that. So I tryed next step which the Excel file converted to XPS file display in the DocumemtViewer of a WPF app.

That was run well as I expected, so I report a detail as below.

1. How to convert Excel file to XPS file (use PIA).

2. How to display XPS file in WPF app (use a feature in Windows OS).

This is the presentation file (Japanese ver. Microsoft PowerPoint file)http://t.co/KwMbkU96sI

This is the Visual Studio 2012 sample solution filehttp://sdrv.ms/16fTtHr

1. How to convert Excel file to XPS file.

Primary Interop Assemblies(PIA) can controls Excel file finely, but PIA depends on version of the Office. So we must decide on target version or use another architecture as VSTO.

This time, use the Microsoft Office 2013 PIA, because of want to create Windows 8 desktop app. the Microsoft Office 2013 PIA is download from Office Developer Tools for Visual Studio 2012 page (http://www.microsoft.com/visualstudio/eng/office-dev-tools-for-visual-studio).

figure. Office Developer Tools for Visual Studio 2012.

If you want to target the Microsoft Office 2010, use the 2013 PIA or download from below ( Office Developer Tools for Visual Studio 2012 includes dlls for 2010 and 2013 both ).


figure. Microsoft Office 2010: Primary Interop Assemblies Redistributable.

After download and installing that, create new WPF app solution from the Visual Studio file menu.

figure. VS 2012 image.

In order to enable drag and drop the Excel file to this app, open the project property window from right click at project name on the Solution Explorer. And put sample Excel file path of local PC into the Debug [Command line arguments] box.

figure. the Command line arguments box.

Create start up event handler from the App.xaml. Defines the event to call the handler at the tag attribute place, it’s complementary when type “Startup = “.

figure. the Startup attribute in the Application tag

To catch an argument at the Startup event handler and open the sample Excel file. First, add reference the Excel name space from the [Assemblies] – [Extentions] of the Reference Manager how to show right click on the References folder of the Solution Explorer.

figue. Add Microsoft.Office.Interop.Excel.dll reference.

Second, the Excel file open by Excel application object of PIA as below.

var excelApp = new Microsoft.Office.Interop.Excel.Application()*

excelApp.DisplayAlerts = false;

excelApp.Visible = false;

Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(path);

*Perfect name space is in order to distinguish Microsoft.Office.Interop.Excel.Application and System.Windows.Application.

*In the future, this app controls a contents of the Excel file by Open XML SDK, so perfect name space is in order to distinguish Microsoft.Office.Interop.Excel.Workbook and DocumentFormat.OpenXml.Spreadsheet.Workbook.

“path” create from the argument of Application_Startup_1 event handler of App class defined in App.xaml.cs.

public static string ExcelPath { get; set; }

private void Application_Startup_1(object sender, StartupEventArgs e)


foreach (string arg in e.Args)


ExcelPath = arg;



In MainWindow.xaml.cs, Static variable of App class “ExcelPath” input to variable “path” of class MainWindow. And add the Window Loaded event handler at MainWindow class constructor

private string path = App.ExcelPath; // add this line

public MainWindow()



Loaded += MainWindow_Loaded; // add this line


In MainWindow_Loaded event handler of MainWindow.xaml.cs, follow the code above, add code that the XPS file export from the Excel file..

void MainWindow_Loaded(object sender, RoutedEventArgs e)


var excelApp = new Microsoft.Office.Interop.Excel.Application();

excelApp.DisplayAlerts = false;

excelApp.Visible = false;

Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(path);

// add below

ExportXPS(excelWorkbook); // XPS Exporter is explained at next description

excelWorkbook.Close(false, null, null);


// release com object


excelApp = null;


Save the XPS file from the Excel file.

string xpsFileName = “”;

void ExportXPS(Microsoft.Office.Interop.Excel.Workbook excelWorkbook)


xpsFileName = (new DirectoryInfo(path)).FullName;

xpsFileName = xpsFileName.Replace(new FileInfo(path).Extension, “”) + “.xps”;


Filename: xpsFileName,

OpenAfterPublish: false);


2. How to display XPS file in WPF app.

To read XPS file for display to the DocumentViewer, placement the DocumentViewer component from the Tool box to MainWindow.xaml. If set the Window height size to 768 and width size to 1024, the DocumentViewer is stretched to same size by default.

Drag the DocumentViewer component from the All WPF Controls of Toolbox and drop into the Grid area and set x:Name property to “DocumentViewer”.

<Window x:Class=”ExcelViewer.MainWindow”



Title=”MainWindow” Height=”768″ Width=”1024″>


<DocumentViewer x:Name=”DocumentWindow” />



figure. Stretch every direction to parent element is default property of the DocumentViewer.

Read the XPS file that create from the Excel file and display into the DocumentViewer control “DocumentWindow” by feature of the Windows OS. Add reference the ReachFremework, several using and create handler “DisplayXPSFile” for that the GetFixedDocumentSequence method of the XpsDocument object provide the FixedDocumentSequence to the DocumentViewer.

figure. the ReachFramework for read XPS file.

using Microsoft.Office.Interop.Excel;

using System.IO;

using System.Runtime.InteropServices;

using System.Windows;

using System.Windows.Documents;

using System.Windows.Xps.Packaging;

using System.IO.Packaging;

namespace ExcelViewer


public partial class MainWindow : System.Windows.Window


void MainWindow_Loaded(object sender, RoutedEventArgs e)


excelApp = null;



void DisplayXPSFile()


XpsDocument xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);

FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();

DocumentWindow.Document = fixedDocumentSequence;




figure. Simple Excel file viewer created by WPF app.

About takao

I'm Microsoft MVP since June 2010.