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 ...
随机推荐
- WPF listbox 实现动态滚轮下拉定位
private ObservableCollection<keymodel> _listlua; public ObservableCollection<keymodel> l ...
- Elasticsearch BM25相关度算法超详细解释
Photo by Pixabay from Pexels 前言:日常在使用Elasticsearch的搜索业务中多少会出现几次 "为什么这个Doc分数要比那个要稍微低一点?".&q ...
- noip33
T1 第一个猎人死的轮数等于在1号猎人之前死的猎人数+1,如果当前这个人没死,那么他死在一号猎人之前的概率为 \(\frac{w_{i}}{w_{1}+w_{i}}\),因为每死一个就会造成1的贡献, ...
- C#调用C++ dll中返回值为字符串的函数问题
C#调用C++ dll函数,如果返回值为字符串,我们使用string去接收就会报错,因为C++返回的是char*,是个指针,所以c# 要用 IntPtr 来接收. C++: //预编译的标头 .h e ...
- 异步编程之APM
一.APM概述 APM即异步编程模型的简写(Asynchronous Programming Model),我们平时经常会遇到类似BeginXXX和EndXXX的方法,我们在使用这些方法的时候,其实就 ...
- HttpURLconnection的介绍
一,HttpURLconnection的介绍 在Android开发中网络请求是最常用的操作之一, Android SDK中对HTTP(超文本传输协议)也提供了很好的支持,这里包括两种接口: 1.标准J ...
- springboot 2.0 整合 RestTemplate
首先导入springboot 的 web 包 <dependency> <groupId>org.springframework.boot</groupId> &l ...
- Flink DataStream API 中的多面手——Process Function详解
之前熟悉的流处理API中的转换算子是无法访问事件的时间戳信息和水位线信息的.例如:MapFunction 这样的map转换算子就无法访问时间戳或者当前事件的时间. 然而,在一些场景下,又需要访问这些信 ...
- 高德地图——控件的添加&删除
控件属性 visible //bool 默认true ov=new AMap.OverView(); ov.hide(); //ov.show(); 显示/隐藏---表示控件的添加与删除 <!D ...
- vue 基础入门(一)
app-1 :声明式渲染 app-2 :绑定元素特性 v-bind 特性被称为指令.指令带有前缀 v-,以表示它们是 Vue 提供的特殊特性. app-3 app-4 :条件与循环 app-5 ,ap ...