2010-11-15 116 views
2

我需要提取Word文檔的格式化文本片段並將其存儲在SQL Server表格中,以便稍後處理,然後使用C#將其重新插入到Word文檔中。 (),Document.Save()和Range.Copy(),Range.Paste()方法的組合,我似乎需要使用Document.Load爲每個片段創建一個文件,然後將其加載到數據庫中。如何在sql server中存儲格式化的Microsoft Word文檔片段

難道沒有更簡單(更有效的方法)嗎?

順便說一句,代碼片段可以隱藏文本,我正在考慮將片段存儲爲RTF。

+1

如果你使用的是Office 2007或更新的版本,Word文檔基本上是一個XML結構 - 所以一個片段(甚至格式化的)將是一個XML片段,真的...... – 2010-11-15 13:23:09

+0

基本上是的。但是docx文檔的規範長達幾千頁。我沒有提到的是,片段必須由最終用戶在Web瀏覽器中進行編輯。 – 2010-11-16 09:01:54

回答

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對象選擇範圍,然後使用範圍對象的XML屬性來獲取該範圍的XML並將其存儲。

您可以稍後使用相反過程將XML插入到另一個文檔中。

雖然編輯代碼片段可能會很有趣,因爲我不知道任何基於Web的WORD兼容編輯器。

+0

XML屬性是隻讀的。此外,你會得到一大堆的Word XML,我不知道如何解釋在文本編輯器中顯示。然而,我正在研究Aspose.Words和Tx文本控制,他們可能有我需要的東西。 – 2010-11-17 12:57:56

相關問題