首页 文章

如何在sql server中存储Microsoft Word文档的格式化片段

提问于
浏览
2

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

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

是不是更容易(更有效的方式)?

顺便说一下,代码片段可以是隐藏文本,我正在考虑将片段存储为RTF .

2 回答

  • 1

    最后我使用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文本控件中对其进行编辑 .

  • 0

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

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

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

相关问题