我必须将一个.xlsx文件导入到我的WPF应用程序中并查看它。我将文档转换为.xps,然后加载。在那之后,我打电话给GetFixedDocumentSequence(),在那里我得到了这个异常
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;
}发布于 2014-09-20 16:40:17
从Excel到XPS的转换似乎有问题。这通常与字形或字体问题有关。您可以打开XPS文件(我在Visual:http://visualstudiogallery.msdn.microsoft.com/450a00e3-5a7d-4776-be2c-8aa8cec2a75b?SRC=VSIDE上使用这个扩展名)来查看问题的来源。导航到生成错误的页面,并找到包含问题的符号。
<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>您还可以找到有关这个also:http://msdn.microsoft.com/en-us/library/ms748985.aspx的更多文档。
由于使用Excel生成XPS,您将几乎没有对转换过程的控制,所以我建议检查Excel文档中使用的字体。也许字体的使用有问题。
问候
https://stackoverflow.com/questions/24410317
复制相似问题