C#导出数据—使用Word模板
前言
本文主要介绍C#使用标签替换的方法导出数据,导出的数据模板使用Word文档。
模板建立
首先创建一个Word文档,然后建立一个基础模板。然后将上方菜单切换到插入菜单。
然后在想填充数据的地方添加书签,如下图,光标在年的前方,点击上方的书签按钮。

书签全部添加完如下图所示:

书签默认是看不到的,我们可以打开文件下的选项页面,然后在视图里勾选书签选项,让书签显示出来,如下图:

勾选后,书签位置会有一个竖线显示,结果如下图所示:

代码实现
新建一个项目WordExport。
然后Nuget添加引用Microsoft.Office.Interop.Word。

然后在页面里添加一个按钮,然后在点击事件里实现如下代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
string wordTemplatePath = System.Windows.Forms.Application.StartupPath + @"\Word模板.docx";
if (File.Exists(wordTemplatePath))
{
System.Windows.Forms.FolderBrowserDialog dirDialog = new System.Windows.Forms.FolderBrowserDialog();
dirDialog.ShowDialog();
if (dirDialog.SelectedPath != string.Empty)
{
string newFileName = dirDialog.SelectedPath + @"\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".docx"; Dictionary<string, string> wordLableList = new Dictionary<string, string>();
wordLableList.Add("年", "2021");
wordLableList.Add("月", "9");
wordLableList.Add("日", "18");
wordLableList.Add("星期", "六");
wordLableList.Add("标题", "Word导出数据");
wordLableList.Add("内容", "我是内容——Kiba518");
Export(wordTemplatePath, newFileName, wordLableList);
MessageBox.Show("导出成功!");
}
else
{
MessageBox.Show("请选择导出位置");
}
}
else
{
MessageBox.Show("Word模板文件不存在!");
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString());
return;
}
}
public static void Export(string wordTemplatePath, string newFileName, Dictionary<string, string> wordLableList)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
string TemplateFile = wordTemplatePath;
File.Copy(TemplateFile, newFileName);
_Document doc = new Document();
object obj_NewFileName = newFileName;
object obj_Visible = false;
object obj_ReadOnly = false;
object obj_missing = System.Reflection.Missing.Value; doc = app.Documents.Open(ref obj_NewFileName, ref obj_missing, ref obj_ReadOnly, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_Visible,
ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing);
doc.Activate();
if (wordLableList.Count > 0)
{
object what = WdGoToItem.wdGoToBookmark;
foreach (var item in wordLableList)
{
object lableName = item.Key;
if (doc.Bookmarks.Exists(item.Key))
{
doc.ActiveWindow.Selection.GoTo(ref what, ref obj_missing, ref obj_missing, ref lableName);//光标移动书签的位置
doc.ActiveWindow.Selection.TypeText(item.Value);//在书签处插入的内容
doc.ActiveWindow.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置插入内容的Alignment
}
}
}
object obj_IsSave = true;
doc.Close(ref obj_IsSave, ref obj_missing, ref obj_missing);
}
代码里我们模拟了一个标签要替换的内容字典,然后调用Microsoft.Office.Interop.Word命名空间下的类,实现对Word模板的书签的替换。
运行项目,如下图:

点击导出按钮,导出Word文档如下:

----------------------------------------------------------------------------------------------------
到此,C#导出数据—使用Word模板就已经介绍完了。
代码已经传到Github上了,欢迎大家下载。
Github地址: https://github.com/kiba518/WordExport
----------------------------------------------------------------------------------------------------
注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!
https://www.cnblogs.com/kiba/p/15309344.html

C#导出数据—使用Word模板的更多相关文章
- java 导出数据为word文档(保持模板格式)
导出数据到具体的word文档里面,word有一定的格式,需要保持不变 这里使用freemarker来实现: ①:设计好word文档格式,需要用数据填充的地方用便于识别的长字符串替换 如 aaaaa ...
- 一个很好的用C#导出数据到Excel模板的方法
/// <summary> /// 导数据到Excel模板 /// </summary> /// <param name="tab">要输出内容 ...
- 导出数据到word
打野的时候,碰到一个需求,导出简历信息. 两条思路: 第一条,直接画所有的表格,填充数据. 第二条,加载一个空的模板,然后填充数据. 因为导出的有格式的,所以最后选择了使用模板进行替换,然后填充数据. ...
- c# 导出数据到Excel模板
最近在做一个发邮件的功能,客户要求需要导出一个Excel附件,并给了附件的格式, eg: Last Name 姓 First Name 名 Chinese Characters汉字书写(仅大陆人填写) ...
- java用freemarker导出数据到word(含多图片)
一.制作word模版 新建word文档,按照需要设置好字体等各种格式:这里为了显得整齐使用了无边框的表格. 将word文档另存为xml文件(注意不是word xml文档,我吃了这家伙的大亏了) 然后用 ...
- C#导出数据至excel模板
开源分享最近一个客户要做一个将数据直接输出到指定格式的Excel模板中,略施小计,搞定 其中包含了对Excel的增行和删行,打印预览,表头,表体,表尾的控制 using System; using S ...
- C# 导出数据到Excel模板中(转)
今天做报表的时候遇到了多表头的问题,而且相应的报表的格式都一样.所以就采用了报表模板的方式来进行. 第一步:在开发的当前项目中引入:Microsoft.Office.Interop.Excel:Sys ...
- C#导出数据的EXCEL模板设计
一:将如下图中,查询出来的数据导出到EXCEL中 二:Excel的状态 三:设计的背后工作 四:最后一步,隐藏
- PHP:导出数据到word(包含图片)
1.方法 public function word() { $xlsModel = M('api_aliucheng'); $Data = $xlsModel->Field('id,u_name ...
随机推荐
- dubbo学习实践(4)之Springboot整合Dubbo及Hystrix服务熔断降级
1. springboot整合dubbo 在provider端,添加maven引入,修改pom.xml文件 引入springboot,版本:2.3.2.RELEASE,dubbo(org.apache ...
- Apache虚拟web主机构建
目录 一.构建虚拟web主机 1.1.虚拟web主机概述 二.搭建虚拟web主机步骤 2.1.基于域名搭建虚拟主机 ①为虚拟主机提供域名解析 ②为虚拟主机准备网页文档 ③添加虚拟主机配置 ④设置访问路 ...
- MOOC大学计算机课程推荐
转自:https://zhuanlan.zhihu.com/p/30659834 这个是大佬总结的, 大学计算机课程 以下课程是我在MOOC上找到的所有我认为讲的好的. 主要是中国大学MOOC,学堂 ...
- brew换源
转自:https://blog.csdn.net/gorwayne/article/details/107359912 第一步,替换brew.git cd "$(brew --repo)&q ...
- NOIP 模拟 $11\; \rm english$
题解 本题有一定代码难度 对于需要区间最大值,可以反过来考虑,先预处理出每个数所能扩展的最大边界,也就是说,求出一个最大的区间,其最大值为这个数,单调栈 \(\mathcal O(n)\) 求解 那么 ...
- Docker运行PostgreSQL
docker-compose.yml version: '3.1' services: db: image: postgres restart: always ports: - 5432:5432 e ...
- ANSI C说明了三个用于存储空间动态分配的函数
1.1 malloc的全称是memory allocation,中文叫动态内存分配.原型:extern void *malloc(unsigned int num_bytes);说明:分配长度为num ...
- 【ArcGIS】 设置管段的流向
在排水管网或者燃气管网中对管段进行几何网络分析,常常用到设置管段流向,一般有三种方法: 1,有流向字段的,直接进行唯一值渲染, 2,没有流向字段的需要建立几何网络, 2.1 在几何网络存在的情况下,设 ...
- CSS样式下border的几种线型
在用border的时候经常会忘记它有多少种线型以及各种线型的写法:每次都得从头开始,或是用到Google.百度之类的,有空整理了一下 (1)none (没有边框,无论边框宽度设为多大) (2)dott ...
- Qt5获取系统文件图标,文件路径
获取系统图标: QFileIconProvider icon_provider; QIcon icon = icon_provider.icon(QFileIconProvider::Folder); ...