I have to import an .xlsx
file into my WPF application and view it. I convert the document to .xps
and then loading. After that I call GetFixedDocumentSequence()
and there I get this Exception
XamlParseException:
{"UnicodeString property does not contain enough characters to correspond to the contents of Indices property."}.
here is my code:
private void LoadData()
{
string xpsPath = ViewDocumentViewer("D:\\test.xlsx");
DisplayXPSFile(xpsPath);
}
private string ViewDocumentViewer(string path)
{
try
{
string xpsPath;
var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;
excelApp.Visible = false;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(path);
xpsPath = ExportXPS(excelWorkbook, path);
excelWorkbook.Close(false, null, null);
excelApp.Quit();
Marshal.ReleaseComObject(excelApp);
excelApp = null;
return xpsPath;
}
catch
{
}
return string.Empty;
}
string ExportXPS(Microsoft.Office.Interop.Excel.Workbook excelWorkbook, string path)
{
string xpsFileName;
xpsFileName = (new DirectoryInfo(path)).FullName;
xpsFileName = xpsFileName.Replace(new FileInfo(path).Extension, "") + ".xps";
excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS,
Filename: xpsFileName,
OpenAfterPublish: false);
return xpsFileName;
}
void DisplayXPSFile(string xpsFileName)
{
XpsDocument xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);
FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();
DocView.Document = fixedDocumentSequence;
}