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

发布于 2024-10-02 07:47:43 字数 270 浏览 0 评论 0原文

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

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

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

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

I need to extract formatted text snippets of a Word document and store it inside an SQL Server table, for later processing and then reinsertion in the Word document using C#.

I've had a look at the Word DOM and it seems that I need to use a combination of the Document.Load(), Document.Save() and Range.Copy(), Range.Paste() methods to create a file for each snippets that I then load into the DB.

Isn't there a easier (more efficient way)?

By the way the code snippets can be hidden text and I was thinking about storing the snippets as RTF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尽揽少女心 2024-10-09 07:47:43

最后我必须使用 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 文本控件中编辑它。

Finally I got to use Aspose.Words for .NET to extract the code snippets from the Word file I'm interested in and store them as 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();

One can then store the rtf into a text field of the database as usual and edit it in a RTF text control.

我早已燃尽 2024-10-09 07:47:43

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

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

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

Document.load, then select the range via a RANGE object, then use the XML property of the range object to get the XML of that range and store it.

You can later insert the XML into another document using the reverse process.

Editing the snippets might prove interesting though, because I'm not aware of any web based WORD compatible editors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文