2012-02-24 4 views
2

単語の読み込みに関連する記事を読んでいますが、C#を使用して単語のドキュメント(Office 2007)を埋め込む必要があります。たとえば、[NAME]というラベルの付いたワードドキュメントを作成し、そのラベルをC#で使用して値を設定し、これをASP.NET MVC3コントローラで実行したいとします。何か案が?ASP.NET MVC3でC#を使用して単語テンプレートを埋め込む

答えて

3

Microsoftが提供するOpenXML SDKを使用してWord文書を操作できます。そして、ここにはnice article(実際には3つの記事のうちの3番目の記事です)といくつかの例があります。

+0

Cool。私はまた[このリンク](http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/)が非常に有用であることを見出します。しかし、それでも[これ](http://stackoverflow.com/questions/9433332/open-xml-sdk-2-0)のために仕事を得ない:( –

0

あなたは次のように行うことができます: - Word文書テンプレート に「signets」を導入 - あなたのワードテンプレート のコピーで作業 - C#コードからsignets値を変更し、ファイルを保存または印刷します。アプリケーションで複数のドキュメントを扱う場合

は正しく単語プロセスを解放して気をつけてください:)質問から抽出された

0

OPのソリューション:

私が見つけた解決策はこれです:

static void Main(string[] args) 
{ 
    Console.WriteLine("Starting up Word template updater ..."); 

    //get path to template and instance output 
    string docTemplatePath = @"C:\Users\user\Desktop\Doc Offices XML\earth.docx"; 
    string docOutputPath = @"C:\Users\user\Desktop\Doc Offices XML\earth_Instance.docx"; 

    //create copy of template so that we don't overwrite it 
    File.Copy(docTemplatePath, docOutputPath); 

    Console.WriteLine("Created copy of template ..."); 

    //stand up object that reads the Word doc package 
    using (WordprocessingDocument doc = WordprocessingDocument.Open(docOutputPath, true)) 
    { 
     //create XML string matching custom XML part 
     string newXml = "<root>" + 
      "<Earth>Outer Space</Earth>" + 
      "</root>"; 

     MainDocumentPart main = doc.MainDocumentPart; 
     main.DeleteParts<CustomXmlPart>(main.CustomXmlParts); 

     //MainDocumentPart mainPart = doc.AddMainDocumentPart(); 

     //add and write new XML part 
     CustomXmlPart customXml = main.AddCustomXmlPart(CustomXmlPartType.CustomXml); 
     using (StreamWriter ts = new StreamWriter(customXml.GetStream())) 
     { 

      ts.Write(newXml); 
     } 

     //closing WordprocessingDocument automatically saves the document 
    } 

    Console.WriteLine("Done"); 
    Console.ReadLine(); 
} 
関連する問題