本篇文章介绍了根据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. sqli-labs:17,增删改

    增 insert into users values(','lcamry','lcamry'); 删 delete from users where id=16 删数据库:drop database ...

  2. 隐马尔科夫_HMM

    有向图 抽象:λ代表输入:输入一个句子,λi表示句子中的每个字 O代表输出:[B词首/M词中/E词尾/S单独成词] max=maxP(O1 O2 On/ λ1 λ2 λn) 假设: (1)当前观察值只 ...

  3. 转录组表达量计RPKM、FPKM、TPM说明

    在转录组测序(RNA-Seq)中,基因的表达量是我们关注的重点.基因表达量的衡量指标有:RPKM.FPKM.TPM. RPKM:Reads Per Kilobase Million:说实话,这个英文说 ...

  4. 华为QOS原理及配置

    http://www.tudou.com/programs/view/GWCiHfWC9FI/ FLASH : http://www.tudou.com/v/GWCiHfWC9FI/&reso ...

  5. 2018.11.18 spoj Triple Sums(容斥原理+fft)

    传送门 这次fftfftfft乱搞居然没有被卡常? 题目简述:给你nnn个数,每三个数ai,aj,ak(i<j<k)a_i,a_j,a_k(i<j<k)ai​,aj​,ak​( ...

  6. 2018.10.30 NOIP训练 【模板】树链剖分(换根树剖)

    传送门 纯粹是为了熟悉板子. 然后发现自己手生了足足写了差不多25min而且输出的时候因为没开long longWA了三次还不知所云 代码

  7. 菜品识别 SDK调用

    from aip import AipImageClassify import os """ 填入参数 """ APP_ID = 'your ...

  8. elasticsearch中文发行版 安装

    参见: https://github.com/medcl/elasticsearch-rtf 具体步骤参见:ubuntu安装elasticsearch-rtf elasticsearch-head 安 ...

  9. linux 定时器 定时执行php

    输入命令:    crontab -e 添加定时命令 每隔一分钟执行一次php //复制一下命令即可.  */1 * * * * /usr/bin/curl -o /home/logs/temp.lo ...

  10. Django开发环境搭建

    最近笔者使用了Django框架作为项目model层的数据对象处理. 关于Django的开发环境,需要安装以下内容: 1.安装python 2.安装VCForPython27.msi 3.安装pycha ...