数据导出之winfrom导出word(二)
本篇文章介绍了根据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(二)的更多相关文章
- 数据导出之winfrom导出word(一)
我们常会用winfrom程序开发小工具,使用dataGridView控件展示数据.同时,我们也会有将这些数据导出的需求. 本篇文章介绍了开发过程中遇到的问题. 一.引用组件 首先,需要在窗体程序中引用 ...
- Java实现PDF和Excel生成和数据动态插入以及导出
一.序言 Excel.PDF的导出.导入是我们工作中经常遇到的一个问题,刚好今天公司业务遇到了这个问题,顺便记个笔记以防下次遇到相同的问题而束手无策. 公司有这么两个需求: 需求一.给了一个表单,让把 ...
- asp.net教程:GridView导出到Excel或Word文件
asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...
- asp.net 将word文档进行编辑并导出一个新的word
最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...
- 基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理
在很多系统模块里面,我们可能都需要进行一定的数据交换处理,也就是数据的导入或者导出操作,这样的批量处理能给系统用户更好的操作体验,也提高了用户录入数据的效率.我在较早时期的EasyUI的Web框架上, ...
- C#变成数据导入Excel和导出Excel
excel 基础 •整个excel 表格叫工作表:workbook:工作表包含的叫页:sheet:行:row:单元格:cell. •excel 中的电话号码问题,看起来像数字的字符串以半角单引号开头就 ...
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm
Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本 ...
- Oracle 数据的导入和导出(SID service.msc)
一:版本号说明: (1)(Oracle11 32位系统)Oracle - OraDb11g_home1: (2)成功安装后显演示样例如以下:第一个图是管理工具.创建连接.创建表:第二个是数据库创建工 ...
- freemarker导出带图片的word文档
最近做一个关于文档导出功能, 顺便学习了下freemarker,做了个关于导出带图片的word文档,模板并没有写全,只是验证代码的正确性 这只是做一个小功能,故只做了后台代码关于导出的代码,并未与前台 ...
随机推荐
- 事务ACID如何定义,事务隔离性解决的问题
挚享科技 2018.4.8 事务的四个特性: 1. 原子性: 同一个事务的多个操作,要么都成功,要么全部失败回滚. 2. 一致性: 事务必须确保数据库从一个一致性状态变换为另一个一致性状态. 其实就是 ...
- 编译sgbm_ros中遇到的问题
出现的问题 这个会报错 1.解决方法是在文件sudo gedit /usr/local/cuda/include/crt/common_functions.h中注释掉如下 #define __CUDA ...
- HACK字体安装
参考:https://github.com/source-foundry/Hack Linux的 下载最新版本的Hack. 从存档中提取文件(.zip). 将字体文件复制到系统字体文件夹(通常/usr ...
- ''TclError: no display name and no $DISPLAY environment variable''解决方法
在模块前写入一下代码: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt 具体解释见 http://m ...
- HDU - 5658
题意:给你一个字符串,给你Q次询问,每一次问你从l-r里有多少个回文串. 思路:len很小,所以直接遍历区间求就好了. /* gyt Live up to every day */ #include& ...
- canvas 实现赛车小游戏
一:样式 <style> #btn{ width: 60px; height: 30px; line-height: 30px; background: #7EC0EE; border: ...
- kbmmw 中虚拟文件操作入门
kbmmw 中一直有一个功能,但是基本上都没有提过,但是在实际应用中,却非常有用,这个功能就是 虚拟文件包功能,他可以把一大堆文件保存到一个文件里面,方便后台管理. kbmmw 的虚拟文件在单元kbm ...
- Spring MVC和Struts2的比较[转]
虽然说没有系统的学习过Spring MVC框架, 但是工作这么长时间, 基本上在WEB层使用的都是Spring MVC, 自己觉得Struts2也是一个不错的WEB层框架, 这两种框架至今自己还未有比 ...
- Oracle 除数为0的处理(decode)
select (a/b*100)per from aa; 当b为0时,提示除数为0, 本人想当除数为0时,不让系统提示出错,结果显示0即可? 解决:select d ...
- 锋利的jQuery(第二版)学习总结
通过对<锋利的jQuery>(第二版)一书的学习,发现此书讲解通俗易懂,是学习jQuery的一本很好的指导书,特作如下总结. 此书主要讲解了jQuery的常用操作,包括认识jQuery,j ...