{"id":20348695,"url":"https://github.com/jevonsflash/doctemplatetool","last_synced_at":"2025-04-12T01:21:02.774Z","repository":{"id":205729469,"uuid":"713325796","full_name":"jevonsflash/DocTemplateTool","owner":"jevonsflash","description":"指定一个模板生成word或Pdf文件","archived":false,"fork":false,"pushed_at":"2023-11-08T07:09:01.000Z","size":1991,"stargazers_count":24,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T21:02:45.514Z","etag":null,"topics":["msoffice","msword","npoi","pdf-generation"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jevonsflash.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-02T09:46:04.000Z","updated_at":"2025-03-07T06:51:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"b4751132-0c13-4404-ac1b-a358a9347f87","html_url":"https://github.com/jevonsflash/DocTemplateTool","commit_stats":null,"previous_names":["jevonsflash/wordtemplategenerator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jevonsflash%2FDocTemplateTool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jevonsflash%2FDocTemplateTool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jevonsflash%2FDocTemplateTool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jevonsflash%2FDocTemplateTool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jevonsflash","download_url":"https://codeload.github.com/jevonsflash/DocTemplateTool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248501855,"owners_count":21114684,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["msoffice","msword","npoi","pdf-generation"],"created_at":"2024-11-14T22:21:45.586Z","updated_at":"2025-04-12T01:21:02.763Z","avatar_url":"https://github.com/jevonsflash.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Document Template Tool\n\n指定一个模板生成word或Pdf文件\n\n![DocTemplateTool.Word](./Assets/p1.png)\n\n**运行单元测试以查看示例！**\n\n\n## 功能 \u0026 特点\n* 基于占位符的模板，可继承原有样式；\n* 支持表格；\n* 支持数据集合；\n* 支持图片；\n* 提供Cli版本程序；\n* 可扩展的接口封装和组件。\n\n\n\n\n## 快速开始\n\n\n### 使用Cli\n\n进入可执行文件所在目录，在命令提示符中运行`DocTemplateTool.exe`\n\n\n参数列表:\n\n| 参数  |    含义     | 用法                                                                                                                                                                                                  |\n| :---: | :---------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n|  -p   | PatternFile | 指定一个Object文件(Json), 作为数据源件                                                                                                                                                         |\n|  -i   |    Input    | 指定一个docx文件作为模板 |\n|  -o   |   Output    | 指定一个路径，作为导出目标 |\n|  -s   |   Source    | 值为`json`                                                                                                                                                         |\n|  -d   | Destination | 值为`word`,` pdf`                                                                                                                                                         |\n|  -w   |  WaitAtEnd  | 指定时，程序执行完成后，将等待用户输入退出                                                                                                                                                            |\n|  -h   |    Help     | 查看帮助                                                                                                                                                                                              |\n\n\n示例\n```\n.\\wtt.exe -p .\\sample\\data.json -i .\\sample\\template.docx -o .\\output\\test.docx -s json -d word\n```\n\n\n### 使用DocTemplateTool.Word类库\n\n在项目中引用[DocTemplateTool.Word]( https://www.nuget.org/packages/DocTemplateTool.Word)\n\n\n```\ndotnet add package DocTemplateTool.Word\n```\n\n\n\n由于Exporter返回的NPOI对象，你需要自行根据业务来处理结果，以及处理IO异常\n\n```\nbyte[] docFileContent;\n\nvar docinfo = GetDocInfo(); // 准备数据\nvar result = Exporter.ExportDocxByObject(\"D:\\\\Template.docx\", docinfo); //生成NPOI文档对象\n\n//处理结果\nusing (var memoryStream = new MemoryStream())\n{\n    result.Write(memoryStream);\n    memoryStream.Seek(0, SeekOrigin.Begin);\n    docFileContent = memoryStream.ToArray();\n}\n\n//写入文件或返回接口\nFile.WriteAllBytes(\"D:\\\\Result.docx\", docFileContent);\n\n```\n\n\n### 使用DocTemplateTool.Pdf类库\n\n在项目中引用[DocTemplateTool.Pdf]( https://www.nuget.org/packages/DocTemplateTool.Pdf)\n\n\n```\ndotnet add package DocTemplateTool.Pdf\n```\n\n\n\n由于Exporter返回的PdfSharp.PdfDocument对象，你需要自行根据业务来处理结果，以及处理IO异常\n\n```\nvar docinfo = GetDocInfo(); // 准备数据\nvar result = Exporter.ExportDocxByObject(\"D:\\\\Template.docx\", docinfo);\n//处理结果\nresult.Save(\"D:\\\\Result.docx\");\n```\n\n\n## 占位符\n\n占位符是指在模板中的一个标记，用于标记需要替换的文本或图片，\n文本占位符的格式为：`$字段名称[附加属性]$`\n图片占位符的格式为：`#字段名称[附加属性]#`\n\nExporter将扫描文档中所有占位符，然后根据占位符的名称，从数据源中获取对应的文本值或图片内容，替换到文档中。\n\n\n## 文本\n\n例如\n\n```\n姓名：$ClientName$\n```\n\n将被替换为\n\n```\n姓名：张三\n```\n\n## 图片\n\n### 确认大小\n\n图片占位符支持附加属性，用于指定图片的宽度和高度，格式为：`#字段名称[宽度,高度]#`\n\n```\n#Graphic[431,266]#\n```\n将被替换为一个宽度为431，高度为266的图片，单位为像素。\n若不指定宽度和高度，则使用默认图片尺寸556*262。\n在Word文档中，因为像素大小是个相对值，页面视图100%时的大小为实际像素尺寸，你可以使用截图工具或标尺工具确认图片的大小。\n\n![DocTemplateTool.Word](./Assets/p5.png)\n\n\n### 设置图片\n\n图片源支持本地文件和网络图片以及Base64编码的图片。\n\n![Alt text](./Assets/image.png)\n\n从不同图片来源生成文档的示例运行如下：\n\n\n![DocTemplateTool.Word](./Assets/p6.png)\n\n\n## 数据集合\n\n暂不支持表格的嵌套。\n数据集合将以表格的形式呈现在文档中，因此你需要在模板中预留一个表格，Exporter将根据表中单元格的占位符，填充表格各列的内容。\n包含占位符的行称之为模板行。\n\n定义\n\n```\npublic class HealthReportDocInfo\n{\n    ...\n    public List\u003cDetailList\u003e BloodPressureList { get; set; }\n}\n\npublic class DetailList\n{\n    public string Name { get; set; }\n    public string Dept { get; set; }\n    public string Value { get; set; }\n    public string Result { get; set; }\n}\n```\n配置模板表格：\n\n![Alt text](./Assets/image-1.png)\n\n默认以第二行作为模板行（通常第一行为表头），你也可以根据实际情况跳过表头，\n\n例如在工资登记表示例中，表头占两行的情况下，第三行为模板行，那么你需要在配置中指定模板行的索引为2（索引从0开始）。\n```\n$Details[2]$\n```\n\n模板行的样式将决定表格的样式，因此你可以在模板行中设置表格的样式，例如设置表格的字体，颜色，大小等。\n\n示例运行如下图：\n\n![DocTemplateTool.Word](./Assets/p7.png)\n\n## 其他示例\n\n企业员工健康管理周报\n\n![Alt text](./Assets/image-2.png)\n\n心电图报告\n\n![DocTemplateTool.Word](./Assets/p3.png)\n\n\n\n\n### 数据源\n\n数据源支持从哈希表（字典）或对象中获取数据。\n\nExporter提供了ExportDocxByDictionary和ExportDocxByObject两个方法，分别用于从哈希表和对象中获取数据。\n\n从哈希表中获取数据：\n```\nvar docinfo = new Dictionary\u003cstring, object\u003e()\n{\n    {\"Dept\", \"XX科技股份有限公司\" },\n    {\"Date\",  DateTime.Now     },\n    {\"Number\",  \"凭 - 202301111\"     },\n    {\"Details\",  new List\u003cDictionary\u003cstring, object\u003e\u003e(){\n\n        new Dictionary\u003cstring, object\u003e(){\n            { \"Type\",\"销售收款\"},\n            { \"Name\",\"应收款\"},\n            { \"DeptorAmount\",0},\n            { \"LenderAmount\",50000}\n        },\n            new Dictionary\u003cstring, object\u003e(){\n            { \"Type\",\"销售收款\"},\n            { \"Name\",\"预收款\"},\n            { \"DeptorAmount\",30000},\n            { \"LenderAmount\",0}\n        },\n        new Dictionary\u003cstring, object\u003e(){\n            { \"Type\",\"销售收款\"},\n            { \"Name\",\"现金\"},\n            { \"DeptorAmount\",20000},\n            { \"LenderAmount\",0}\n        },\n\n    }},\n    { \"DeptorSum\",  50000     },\n    { \"LenderSum\",  50000     },\n    { \"ClientName\",  \"XX科技股份有限公司\"     },\n    { \"Teller\",  \"张三\"     },\n    { \"Maker\",  \"李四\"     },\n    { \"Auditor\",  \"王五\"     },\n    { \"Register\",  \"赵六\"     },\n};\nvar result = Word.Exporter.ExportDocxByDictionary(Path.Combine(templatePath_Doc, $\"AccountingTemplate.docx\"), docinfo, (s) =\u003e s);\n\n\n```\n\n从匿名对象中获取数据：\n\n```\nvar docinfo = new\n{\n\n    Dept = \"XX科技股份有限公司\",\n    Date = DateTime.Now,\n    Number = \"凭 - 202301111\",\n    Details = new List\u003cdynamic\u003e() {\n\n        new\n        {\n            Type = \"销售收款\",\n            Name = \"应收款\",\n            DeptorAmount = 0,\n            LenderAmount = 50000\n        },\n        new\n        {\n            Type = \"销售收款\",\n            Name = \"预收款\",\n            DeptorAmount = 30000,\n            LenderAmount = 0\n        },\n        new\n        {\n            Type = \"销售收款\",\n            Name = \"现金\",\n            DeptorAmount = 20000,\n            LenderAmount = 0\n        },\n    },\n    DeptorSum = 50000,\n    LenderSum = 50000,\n    ClientName = \"XX科技股份有限公司\",\n    Teller = \"张三\",\n    Maker = \"李四\",\n    Auditor = \"王五\",\n    Register = \"赵六\",\n};\n\nvar result = Word.Exporter.ExportDocxByObject(Path.Combine(templatePath_Doc, $\"AccountingTemplate.docx\"), docinfo, (s) =\u003e s);\n```\n\n它们将得到同样的结果：\n\n![Alt text](./Assets/image-3.png)\n\n\n\n## 已知问题\n\n\n\n## 作者信息\n\n作者：林小\n\n邮箱：jevonsflash@qq.com\n\n\n\n## License\n\nThe MIT License (MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjevonsflash%2Fdoctemplatetool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjevonsflash%2Fdoctemplatetool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjevonsflash%2Fdoctemplatetool/lists"}