Skip to content

Instantly share code, notes, and snippets.

@jaydeepkarena
Created May 8, 2019 04:38
Show Gist options
  • Save jaydeepkarena/08608f5399de4720b1f97057f90a7f01 to your computer and use it in GitHub Desktop.
Save jaydeepkarena/08608f5399de4720b1f97057f90a7f01 to your computer and use it in GitHub Desktop.
C# - Show print preview of excel file

C# Print Preview using Microsoft.Office.Interop.Excel

Install Microsoft.Office.Interop.Excel using NuGet package

Install-Package Microsoft.Office.Interop.Excel

To show print preview use following code

// Import Excel package
// using Excel = Microsoft.Office.Interop.Excel;

string file = @"C:\Users\Weenggs\Desktop\x.xlsx";
var excelApp = new Excel.Application();

Excel.Workbooks books = excelApp.Workbooks;
Excel._Workbook sheet = books.Open(file);
excelApp.Visible = true; // true will open Excel
sheet.PrintPreview();
excelApp.Visible = false; // hides excel file when user closes preview
@nurullahguc
Copy link

I have a issue like this;

System.IO.FileNotFoundException: 'Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment