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 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();
}
}
}
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;
}