Search code examples
c#wpfexcel

How to open an Excel file and view in WPF?


Im creating some excel files using my c# application and save them in my pc. I want to open and view them inside my application how can i do it? My application is developed using WPF C#


Solution

  • You can use DocumentViewer WPF control.

    First you should convert your office document to XPS format. Please look at this post.

    And then bind it to the Document property of the DocumentViewer Control.

    • XAML
    <DocumentViewer Name="myDocumentViewer" Margin="0,0,0,59">
    </DocumentViewer>
    
    • Code Behind
    myDocumentViewer.Document = this.ConvertPptxDocToXPSDoc(
      this.FileName, 
      this.newXPSDocumentName).GetFixedDocumentSequence();
    
    • the code from the (now broken) link
    private Microsoft.Office.Interop.Word._Application wordApp;
    private Microsoft.Office.Interop.Excel._Application excelApp;
    private Microsoft.Office.Interop.PowerPoint._Application ppApp;
    
    public Converter()
    {
        // Initialize Office Apps in the beginning because it could take a while to load them into the memory
        wordApp = new Microsoft.Office.Interop.Word.Application();
        wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
        excelApp = new Microsoft.Office.Interop.Excel.Application();
        excelApp.DisplayAlerts = false;
        ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
        ppApp.DisplayAlerts = PpAlertLevel.ppAlertsNone;
    }
    
    /// 
    
    /// It uses word to save the specified input file in the xps format to the specified destination file
    /// 
    
    private void ConvertWordToXPSDoc(string wordDocName, string xpsDocName)
    {
        wordApp.Documents.Add(wordDocName);
    
        _Document doc = wordApp.ActiveDocument;
    
        try
        {
            doc.ExportAsFixedFormat(xpsDocName, WdExportFormat.wdExportFormatXPS);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
        finally
        {
            doc.Close(WdSaveOptions.wdDoNotSaveChanges);
        }
    }
    
    /// 
    
    /// It uses excel to save the specified input file in the xps format to the specified destination file.
    /// It considers all the worksheets.
    /// 
    
    private void ConvertExcelToXPSDoc(string excelDocName, string xpsDocName)
    {
        excelApp.Workbooks.Add(excelDocName);
    
        Microsoft.Office.Interop.Excel.Workbook wb = excelApp.ActiveWorkbook;
    
        try
        {
            wb.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS, xpsDocName);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
        finally
        {
            wb.Close(false);
        }
    }
    
    /// 
    
    /// It uses powerpoint to save the specified input file in the xps format to the specified destination file.
    /// It considers all the slides but no notes.
    /// 
    
    private void ConvertPowerPointToXPSDoc(string ppDocName, string xpsDocName)
    {
        ppApp.Presentations.Open(ppDocName, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
    
        Microsoft.Office.Interop.PowerPoint.Presentation p = ppApp.Presentations[ppApp.Presentations.Count];
    
        try
        {
            p.ExportAsFixedFormat(xpsDocName, PpFixedFormatType.ppFixedFormatTypeXPS);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
        finally
        {
            p.Close();
        }
    }