Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XpsDocument.GetFixedDocumentSequence引发XamlParseException_C#_Wpf_Documentviewer_Xamlparseexception - Fatal编程技术网

C# XpsDocument.GetFixedDocumentSequence引发XamlParseException

C# XpsDocument.GetFixedDocumentSequence引发XamlParseException,c#,wpf,documentviewer,xamlparseexception,C#,Wpf,Documentviewer,Xamlparseexception,我必须将.xlsx文件导入我的WPF应用程序并查看它。我将文档转换为.xps,然后加载。然后我调用GetFixedDocumentSequence(),在那里我得到了这个异常 XamlParseException: {“UnicodeString属性未包含足够的字符,无法 与Indexes属性“”的内容相对应 这是我的密码: private void LoadData() { string xpsPath = ViewDocumentViewer("D:\\test.xlsx");

我必须将
.xlsx
文件导入我的WPF应用程序并查看它。我将文档转换为.xps,然后加载。然后我调用
GetFixedDocumentSequence()
,在那里我得到了这个异常

XamlParseException:

{“UnicodeString属性未包含足够的字符,无法 与Indexes属性“”的内容相对应

这是我的密码:

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上使用此扩展名:)以查看问题的来源。导航到生成错误的页面并找到包含问题的图示符

<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>

您还可以找到有关此ar MSDN的更多文档:

由于use使用Excel Interop生成XPS,您几乎无法控制转换过程,因此我建议您查看Excel文档中使用的字体。 也许使用的字体有问题

问候