0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# Word フィールドを更新

Posted at

フィールドはフィールドコードと呼ばれるWordへの命令を文書内に記述するための入れ物です。Wordのフィールドを自由自在に使える方はWord上級者と呼んでも過言ではないと思います。今回はSpire.Docを使ってWord フィールドを更新する方法を紹介します。

下準備

1.E-iceblueの公式サイトからFree Spire.Doc for .NET無料版をダウンロードしてください。

f:id:lendoris:20210220120858p:plain

2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.Doc.dllを参照に追加してください。(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→Spire.Doc.dll”というようです。)

 

f:id:lendoris:20210220122305p:plain

```C# using Spire.Doc;

namespace ConsoleApplication31
{
class Program
{
static void Main(string[] args)
{

        Document doc = new Document();

        //ファイルテンプレートをロードします。
        doc.LoadFromFile("リスト.docx");

        //リストのデータに更新します。
        doc.Replace("a.price", "5.5", true, true);
        doc.Replace("b.price", "11.9", true, true);
        doc.Replace("c.price", "5", true, true);
        doc.Replace("a.quantity", "1", true, true);
        doc.Replace("b.quantity", "2", true, true);
        doc.Replace("c.quantity", "4", true, true);

        //フィールドを更新します。
        doc.IsUpdateFields = true;

        //保存します。
        doc.SaveToFile("output.docx", FileFormat.Docx2013);
    }
}

}

<h4><strong>実行結果</strong></h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210220/20210220122319.png" alt="f:id:lendoris:20210220122319p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?