XpsDocument.GetFixedDocumentSequence抛出XamlParseException。

3
我需要将一个 .xlsx 文件导入到我的 WPF 应用程序中并查看它。我将文档转换为 .xps 格式,然后进行加载操作。之后我调用 GetFixedDocumentSequence() 方法,但是出现了以下异常:

XamlParseException:

{"UnicodeString property does not contain enough characters to correspond to the contents of Indices property."}.

这是我的代码:

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;
}
1个回答

1

看起来Excel转换为XPS时出了问题。 这通常与字形或字体问题有关。 您可以打开XPS文件(我在Visual Studio上使用此扩展名: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>

您还可以在MSDN上找到有关此的更多文档:http://msdn.microsoft.com/en-us/library/ms748985.aspx

由于您正在使用Excel Interop生成XPS,几乎无法控制转换过程,因此建议您查看Excel文档中使用的字体。可能存在某些字体使用的问题。

敬礼


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接