【发布时间】:2015-01-02 16:43:11
【问题描述】:
我必须将.xlsx 文件导入我的 WPF 应用程序并查看它。我将文档转换为 .xps 然后加载。之后我打电话给GetFixedDocumentSequence(),我得到了这个异常
XamlParseException:
{"UnicodeString 属性没有包含足够的字符 对应于 Indices 属性的内容。"}.
这是我的代码:
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;
}
【问题讨论】:
标签: c# wpf documentviewer xamlparseexception