TemplateEngine.Docx 项目使用教程

TemplateEngine.Docx 项目使用教程

TemplateEngine.Docx Smart docx template engine for .NET TemplateEngine.Docx 项目地址: https://gitcode.com/gh_mirrors/te/TemplateEngine.Docx

1. 项目目录结构及介绍

TemplateEngine.Docx 项目的目录结构如下:

TemplateEngine.Docx/
├── src/
│   ├── TemplateEngine.Docx/
│   │   ├── Properties/
│   │   ├── bin/
│   │   ├── obj/
│   │   ├── TemplateEngine.Docx.csproj
│   │   ├── Program.cs
│   │   ├── TemplateProcessor.cs
│   │   └── ...
│   └── TemplateEngine.Docx.Example/
│       ├── Properties/
│       ├── bin/
│       ├── obj/
│       ├── TemplateEngine.Docx.Example.csproj
│       ├── Program.cs
│       └── ...
├── .gitignore
├── LICENSE
├── README.md
└── ...

目录结构说明:

  • src/: 包含项目的源代码。
    • TemplateEngine.Docx/: 核心库的源代码。
      • Properties/: 项目属性文件。
      • bin/: 编译后的二进制文件。
      • obj/: 编译过程中的中间文件。
      • TemplateEngine.Docx.csproj: 项目文件。
      • Program.cs: 主程序入口。
      • TemplateProcessor.cs: 模板处理器的实现。
    • TemplateEngine.Docx.Example/: 示例项目的源代码。
      • Properties/: 项目属性文件。
      • bin/: 编译后的二进制文件。
      • obj/: 编译过程中的中间文件。
      • TemplateEngine.Docx.Example.csproj: 示例项目文件。
      • Program.cs: 示例程序入口。
  • .gitignore: Git 忽略文件配置。
  • LICENSE: 项目许可证文件。
  • README.md: 项目说明文档。

2. 项目启动文件介绍

项目的启动文件位于 src/TemplateEngine.Docx.Example/Program.cs。该文件是示例项目的入口,展示了如何使用 TemplateEngine.Docx 生成 Word 文档。

Program.cs 文件内容概览:

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");

            var valuesToFill = new Content(
                new FieldContent("Report date", DateTime.Now.ToString())
            );

            using (var outputDocument = new TemplateProcessor("OutputDocument.docx")
                .SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            }
        }
    }
}

启动文件说明:

  • Main 方法: 程序的入口点,负责删除旧的输出文件、复制模板文件、填充内容并保存生成的文档。
  • TemplateProcessor: 用于处理模板文件的核心类,通过 FillContent 方法填充内容,并通过 SaveChanges 方法保存修改。

3. 项目配置文件介绍

TemplateEngine.Docx 项目没有独立的配置文件,其配置主要通过代码实现。项目的配置主要体现在 TemplateProcessor 类的使用上,通过该类的实例化及方法调用来完成模板文件的处理。

配置示例:

using (var outputDocument = new TemplateProcessor("OutputDocument.docx")
    .SetRemoveContentControls(true))
{
    outputDocument.FillContent(valuesToFill);
    outputDocument.SaveChanges();
}

配置说明:

  • TemplateProcessor 构造函数: 传入模板文件路径。
  • SetRemoveContentControls 方法: 设置是否在填充内容后移除内容控件。
  • FillContent 方法: 填充模板内容。
  • SaveChanges 方法: 保存修改后的文档。

通过以上配置,可以灵活地生成符合需求的 Word 文档。

TemplateEngine.Docx Smart docx template engine for .NET TemplateEngine.Docx 项目地址: https://gitcode.com/gh_mirrors/te/TemplateEngine.Docx

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郦嵘贵Just

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值