繁体   English   中英

如何在SQL Server中存储Microsoft Word文档的格式化摘要

[英]How to store formatted snippets of Microsoft Word documents in sql server

我需要提取Word文档的格式化文本片段并将其存储在SQL Server表中,以供以后处理,然后使用C#重新插入Word文档中。

我看过Word DOM,看来我需要结合使用Document.Load(),Document.Save()和Range.Copy(),Range.Paste()方法来创建文件然后将每个片段加载到数据库中。

有没有更简单(更有效的方法)?

顺便说一句,代码片段可以是隐藏文本,我当时正在考虑将这些片段存储为RTF。

最后,我必须使用Aspose.Words for .NET从我感兴趣的Word文件中提取代码片段,并将其存储为RTF:

// Get insteresting code snippets (in this case text runs with 
// style "tw4winMark")
Document sourceDocument = new Document(fileName);
var runs = sourceDocument.GetChildNodes(NodeType.Run, true)
    .Select(r => r.Font.StyleName == "tw4winMark").ToList();

// Store snippets into temporary document
// Read Aspose documentation for details
Document document = new Document();
if (runs.Count > 0) {
    NodeImporter nodeImporter = new NodeImporter(
        runs[0].Document,
        document,
        ImportFormatMode.KeepSourceFormatting
    );

    foreach (Run run in runs) {
        Run importedRun = nodeImporter.ImportNode(run, true) as Run;
        importedRun.Font.Hidden = false;
        document.Sections[0].Body.Paragraphs[0].AppendChild(importedRun);
    }
}

// save temporary document in MemoryStream as RTF
RtfSaveOptions saveOptions = new RtfSaveOptions();
MemoryStream ms = new MemoryStream();
document.Save(ms, saveOptions);

// retrieve RTF from MemoryStream
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
string rtf = sr.ReadToEnd();

然后可以像往常一样将rtf存储到数据库的文本字段中,然后在RTF文本控件中对其进行编辑。

Document.load,然后通过RANGE对象选择范围,然后使用range对象的XML属性获取该范​​围的XML并将其存储。

您以后可以使用相反的过程将XML插入另一个文档中。

不过,编辑片段可能会很有趣,因为我不知道任何基于Web的WORD兼容编辑器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM