本篇文章介绍了根据word模板导出word文档的方法。

一、获取模板地址

  WordDocFileHelper WordTem = new WordDocFileHelper();
string path = @"TempleteWord\ReportRepair.dotx";
string fullName = System.Windows.Forms.Application.StartupPath.Substring(, System.Windows.Forms.Application.StartupPath.LastIndexOf("\\"));
fullName = fullName.Substring(, fullName.LastIndexOf("\\")) + "\\" + path;

二、通过模板创建新文档

 WordTem.CreateNewDocument1(fullName);
   public void CreateNewDocument1(string filePath)
{
wordApp = new ApplicationClass();//创建一个word应用程序实例
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.Visible = false;//设置为不可见
object missing = System.Reflection.Missing.Value;
object format = WdSaveFormat.wdFormatDocument;
object templateName = filePath;//模板文件地址
wordDoc = wordApp.Documents.Add(ref templateName, ref missing,ref missing, ref missing);
}

三、模板书签处插入值

WordTem.InsertValue("XMMC", ID);
   public bool InsertValue(string bookmark, string value)
{
object bkObj = bookmark;
if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
{
wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
wordApp.Selection.TypeText(value);
return true;
}
return false;
}

四、选择保存位置

  FolderBrowserDialog path = new FolderBrowserDialog();
path.Description = "选择需要保存的文件夹";
path.ShowDialog();
SaveFilePath = path.SelectedPath;

五、自动保存文档

WordTem.SaveDocument(SaveFilePath + "\\" + name + ".doc");
   public void SaveDocument(string Filename)
{
object fileName = Filename;
object format = WdSaveFormat.wdFormatDocument;//保存格式
object miss = System.Reflection.Missing.Value;
wordDoc.SaveAs(ref fileName, ref format, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss);
//关闭wordDoc,wordApp对象
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}

六、替换为word中选择框

   public string ReplaceCheckCell(string text)
{
wordApp.Selection.Find.Replacement.Font.Name = "Wingdings 2";
//这里设置字体类型.Wingdings 2字体下的\u0052代表打钩的选择框,\u00A3代表未打钩的选择框
string Normal = "\u0052正常\u00A3不正常";
string UnNormal = "\u00A3正常\u0052不正常";
if (text == "正常")
{
wordApp.Selection.Find.Replacement.Text = Normal;
return Normal;
}
else if (text == "不正常")
{
wordApp.Selection.Find.Replacement.Text = UnNormal;
return UnNormal;
}
else { return ""; }
}

七、导出时需要默认打开文件,由用户自己保存

  public void CreateNewDocument0(string filePath, string filename)
{
//killWinWordProcess();
wordApp = new ApplicationClass();//创建一个word应用程序实例
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.Visible = false;//设置为不可见
object missing = System.Reflection.Missing.Value;
object format = WdSaveFormat.wdFormatDocument;
object fileName = filename;
object templateName = filePath;//模板文件地址
wordDoc = wordApp.Documents.Open(ref templateName, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing); //保存文档
wordDoc.SaveAs(ref fileName, ref format, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing);
}
  public void OpenDocument(string Filename)
{
object fileName = Filename;
object Missing = System.Reflection.Missing.Value;
object readOnly = true;
wordDoc = wordApp.Documents.Open(ref fileName, ref Missing, ref readOnly, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing);
////wordApp.Visible = true;
////wordDoc.PrintPreview();//打印预览
}

数据导出之winfrom导出word(二)的更多相关文章

  1. 数据导出之winfrom导出word(一)

    我们常会用winfrom程序开发小工具,使用dataGridView控件展示数据.同时,我们也会有将这些数据导出的需求. 本篇文章介绍了开发过程中遇到的问题. 一.引用组件 首先,需要在窗体程序中引用 ...

  2. Java实现PDF和Excel生成和数据动态插入以及导出

    一.序言 Excel.PDF的导出.导入是我们工作中经常遇到的一个问题,刚好今天公司业务遇到了这个问题,顺便记个笔记以防下次遇到相同的问题而束手无策. 公司有这么两个需求: 需求一.给了一个表单,让把 ...

  3. asp.net教程:GridView导出到Excel或Word文件

    asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...

  4. asp.net 将word文档进行编辑并导出一个新的word

    最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...

  5. 基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理

    在很多系统模块里面,我们可能都需要进行一定的数据交换处理,也就是数据的导入或者导出操作,这样的批量处理能给系统用户更好的操作体验,也提高了用户录入数据的效率.我在较早时期的EasyUI的Web框架上, ...

  6. C#变成数据导入Excel和导出Excel

    excel 基础 •整个excel 表格叫工作表:workbook:工作表包含的叫页:sheet:行:row:单元格:cell. •excel 中的电话号码问题,看起来像数字的字符串以半角单引号开头就 ...

  7. Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm

    Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本 ...

  8. Oracle 数据的导入和导出(SID service.msc)

    一:版本号说明: (1)(Oracle11  32位系统)Oracle - OraDb11g_home1: (2)成功安装后显演示样例如以下:第一个图是管理工具.创建连接.创建表:第二个是数据库创建工 ...

  9. freemarker导出带图片的word文档

    最近做一个关于文档导出功能, 顺便学习了下freemarker,做了个关于导出带图片的word文档,模板并没有写全,只是验证代码的正确性 这只是做一个小功能,故只做了后台代码关于导出的代码,并未与前台 ...

随机推荐

  1. SCM_SVN_CVS

    SCM_SVN_CVS SCM:一种用于记录并控制软件数据的工具.比如有:CVS(有过时趋势)和SVN(更加常用). 版本控制的概念: Respository:仓库 Workspace:工作台 Del ...

  2. 通过代理上网时,qq等应用程序连网出错

    虽然现在基本上都用无线,有线宽带等,但是有时候还是避免不了通过代理上网时,于是就发生浏览器可以正常浏览网页,qq等应用程序连接出错等问题,上网搜了好长时间,    都没解决问题,后来慢慢琢磨(其实是乱 ...

  3. BS架构如何实现即时消息提醒

    转载地址:https://blog.csdn.net/cyjch/article/details/51506434

  4. sql server profiler 工具使用 sql 语句性能分析

    https://www.cnblogs.com/knowledgesea/p/3683505.html

  5. OneZero第三周第五次站立会议(2016.4.8)

    1. 时间: 15:10--15:25  共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  6. 2018.11.03 NOIP模拟 树(长链剖分优化dp)

    传送门 考虑直接推式子不用优化怎么做. 显然每一个二进制位分开计算贡献就行. 即记录fi,jf_{i,j}fi,j​表示距离iii这个点不超过jjj的点的每个二进制位的0/10/10/1个数. 但直接 ...

  7. 2018.10.24 NOIP模拟 小 C 的数组(二分+dp)

    传送门 考试自己yyyyyy的乱搞的没过大样例二分+dp二分+dp二分+dp过了606060把我自己都吓到了! 这么说来乱搞跟被卡常的正解比只少101010分? 那我考场不打其他暴力想正解血亏啊. 正 ...

  8. zookeeper 单机集成部署

    概述 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等,是很多分布式的基础设置,比如dubbo,k ...

  9. error C2065: 'IDD_DIALOG1' : undeclared identifier

    添加资源文件 #include "resource.h"

  10. 第31章:MongoDB-索引--复合索引

    ①复合索引 对多个字段创建索引被称为复合索引或者组合索引 ②创建组合索引 创建组合索引(以后台模式创建) db.user.ensureIndex({"username": 1, & ...