XpsDocument.GetFixedDocumentSequence throws XamlParseExcepti
本文关键字:XamlParseExcepti throws GetFixedDocumentSequence XpsDocument | 更新日期: 2023-09-27 18:10:18
我必须将.xlsx
文件导入我的WPF应用程序并查看它。我将文档转换为。xps
,然后加载。然后我调用GetFixedDocumentSequence()
然后我得到这个Exception
下面是我的代码:XamlParseException:
{"UnicodeString属性没有包含足够的字符来{}.
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;
}
看起来Excel到XPS的转换有问题。这通常与字形或字体问题有关。你可以打开你的XPS文件(我在Visual Studio上使用这个扩展:http://visualstudiogallery.msdn.microsoft.com/450a00e3-5a7d-4776-be2c-8aa8cec2a75b?SRC=VSIDE),看看问题来自哪里。导航到生成错误的页面,找到包含问题的Glyphs。
<Canvas Clip="F 1 M137.710006714,208.58001709 L476.861022949,208.58001709 L476.861022949,208.58001709 L476.861022949,219.83001709 L476.861022949,219.83001709 L137.710006714,219.83001709 Z">
<Glyphs OriginX="0" OriginY="0" UnicodeString="D" Indices=",55.6" Fill="#FF000000" FontRenderingEmSize="1" FontUri="/Resources/076a5115-0000-0000-0000-000000000000.odttf" RenderTransform="8.949999809,0,0,8.949999809,185.458496094,218.330078125" />
</Canvas>
你也可以找到更多关于这个的文档MSDN: http://msdn.microsoft.com/en-us/library/ms748985.aspx
由于我们使用Excel互操作来生成XPS,您几乎无法控制转换过程,因此我建议查看Excel文档中使用的字体。可能是使用的字体有问题。
对