数据导出之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文档,模板并没有写全,只是验证代码的正确性 这只是做一个小功能,故只做了后台代码关于导出的代码,并未与前台 ...
随机推荐
- Python之路(第五篇) Python基本数据类型集合、格式化、函数
一.变量总结 1.1 变量定义 记录某种状态或者数值,并用某个名称代表这个数值或状态. 1.2 变量在内存中的表现形式 Python 中一切皆为对象,数字是对象,列表是对象,函数也是对象,任何东西都是 ...
- linux 和 主机通信的另类方法
偶然发现,linux可以从github上直接下载代码.这样就能用windows写好代码,直接给linux来跑了.很方便. 当然是因为我还不会配置网络来让linux和windows通信.弄了一个下午也没 ...
- Desktop Central帮助您升级Windows 10,获取更新的五大增强功能
- canvas 实现弹跳效果
一:创建画布 <canvas width="600" height="600" id="canvas"></canvas& ...
- Django框架之models和不依赖Qquery的ajax请求
一.models表字段 1)class表字段的创建 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) ...
- BP神经网络的理论理论常识
BP神经网络的简单结构:输入层.一个或者多个隐层.输出层.图如下: 在图中,涉及到的参数有:X1--Xn为输入参数.输入参数通过输入层和隐层之间的的链接权重进行计算,到达隐层. 隐层的输入参数通过隐层 ...
- c++四舍五入的新方法
将原来的数加上0.5,如果是需要进位的加上0.5就进位了,如果不需要进位的加上0.5也小于1,被int型省略掉.
- Fiddler建好代理后,能连到手机,但手机不能上网了是什么原因
依次 tools(工具) >> fiddler options(fiddler选项) >> connections( 连接) >>allow remot ...
- .NET性能优化(文摘)
第1章 性能指标 1.1 性能目标 1.2 性能指标 第2章 性能度量 2.1 性能度量方式 白盒测试-小程序 黑盒测试-大型程序 2.2 Windows内置工具 2.2.1 性能计数器 2.2.2 ...
- mysql学习之路_外键
回顾4 连接查询: 连接多张表到一起,不管记录数如何,字段数一定会增加. 分类:内连接,外连接.自然连接,交叉连接, 交叉连接:cross join (笛卡尔积) 内连接:inner join,左右两 ...