博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
动态创建word后下载(二)
阅读量:7075 次
发布时间:2019-06-28

本文共 6471 字,大约阅读时间需要 21 分钟。

首先:做一下基础工作:

导入COM库:Microsoft word 11.0 Object Library.

引入命名空间:

using Microsoft.Office.Interop.Word;

其次:分别阐述每个知识点的使用

实例化一个word对象:

实例化Code
1 killWordProcess();//进程中会有word进程,杀掉上次的 2 object missingValue = System.Reflection.Missing.Value; 3                 object myTrue = false;                  //不显示Word窗口 4                 Microsoft.Office.Interop.Word._Application oWord = new  5  6 Microsoft.Office.Interop.Word.ApplicationClass(); 7                 Microsoft.Office.Interop.Word._Document doc; 8                 doc = oWord.Documents.Open(ref Obj_FileName, ref missingValue, 9                    ref myTrue, ref missingValue, ref missingValue, ref missingValue,10                    ref missingValue, ref missingValue, ref missingValue,11                    ref missingValue, ref missingValue, ref missingValue,12                    ref missingValue, ref missingValue, ref missingValue,13                    ref missingValue);

 

 

 动态插入书签并赋值(图片、文字)

   前提:1.通过模板中一个来定位其他书签的位置 (aa:书签是模板中的)
           2.循环插入(循环产生:例如:bookmark1 bookmark11 bookmark32)

动态书签 Code
1 if (intTestType == 1) 2                             { 3                                 aa = "scoreBar"; 4                             } 5                              6 //注意:书签是相连的 7                             Microsoft.Office.Interop.Word.Range r = doc.Bookmarks.get_Item(ref aa).Range; 8                             object rng = (object)r; 9                             oWord.ActiveDocument.Bookmarks.Add("bookmark" + intTestType, ref rng); //bookmark110                             oWord.ActiveDocument.Bookmarks.DefaultSorting = 0;11                             oWord.ActiveDocument.Bookmarks.ShowHidden = true;12 13                             object o = "bookmark" + intTestType;14 15                             Microsoft.Office.Interop.Word.Range r2 = doc.Bookmarks.get_Item(ref o).Range;16                             object rng2 = (object)r2;17                             oWord.ActiveDocument.Bookmarks.Add("bookmark1" + intTestType, ref rng2); //bookmark1118                             oWord.ActiveDocument.Bookmarks.DefaultSorting = 0;19                             oWord.ActiveDocument.Bookmarks.ShowHidden = true;20                             21 //以下对书签赋值                           22                             doc.Bookmarks.get_Item(ref aa).Range.InlineShapes.AddPicture(@"c:\scorebar.png", ref missingValue, ref missingValue); //插入图片23                             o2 = "bookmark1" + intTestType;24                             doc.Bookmarks.get_Item(ref o).Range.Text = NumberHelper.ConvertSum(intTestType.ToString()) + "、" + dr["itemName"].ToString() + "\r\n"; //插入文字25 26 //循环变量27                     intTestType++;28                              intTestType1++;29 30                             Microsoft.Office.Interop.Word.Range r3 = doc.Bookmarks.get_Item(ref o2).Range;31                           32                             object rng3 = (object)r3;33                             oWord.ActiveDocument.Bookmarks.Add("bookmark3" + intTestType, ref rng3); //bookmark3234                             oWord.ActiveDocument.Bookmarks.DefaultSorting = 0;35                             oWord.ActiveDocument.Bookmarks.ShowHidden = true;36 37                             aa = "bookmark3" + intTestType;

 

 

 循环遍历模板中的书签,显示相应内容(图片、文字、表格)

 

循环遍历书签 Code
1 foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks) 2                 { 3                     if (bm.Name == "bookmark1") 4                     { 5                         object aa = "bookmark1"; 6                         doc.Bookmarks.get_Item(ref aa).Range.InlineShapes.AddPicture(@"c:\scorebar.png", ref missingValue, ref missingValue); //插入图片 7                     }else if (bm.Name == "mainTitle") 8                     { 9 10                         bm.Select();11                         bm.Range.Text = strmaintitle;//插入文本12                     }else13     {14          object tmp = "copyScore";15                         Microsoft.Office.Interop.Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;16 17                         int intTestNum = 10;//todo 从DB中获取该试卷的总题型18                         int intSumNum = intTestNum + 2;19                         //添加表格20                         doc.Tables.Add(startRange, 2, intSumNum, ref missingValue, ref missingValue);21 22 23                         doc.Tables[1].Cell(1, 1).Range.Text = "题号";//设置文本24                         doc.Tables[1].Cell(2, 1).Range.Text = "得分";25                         doc.Tables[1].Cell(1, intSumNum).Range.Text = "总分";//设置文本26 27                         for (int i = 1; i <= intTestNum; i++)28                         {29                             doc.Tables[1].Cell(1, i + 1).Range.Text = NumberHelper.ConvertSum(i.ToString());//设置文本30                         }31                         //为表格划线32                         startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;33                         startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;34                         startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;35                         startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;36                         startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;37                         startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;38 39                         doc.Tables[1].Select();//选中表格40                         oWord.Selection.Tables[1].Rows.Alignment = WdRowAlignment.wdAlignRowCenter;//表格居中41 42 }43 }

 

 

 

// 每当产生新word 之前,先关闭以前的word.exe进程

关闭 进程 Code
1  protected void killWordProcess() 2         { 3             System.Diagnostics.Process[] myPs; 4             myPs = System.Diagnostics.Process.GetProcesses(); 5             foreach (System.Diagnostics.Process p in myPs) 6             { 7                 if (p.ProcessName.ToUpper() == "WINWORD.EXE") 8                 { 9                     try10                     {11                         p.Kill();12                     }13                     catch (Exception)14                     {15 16                     }17 18                 }19             }20         }

 

 

 

转载于:https://www.cnblogs.com/lei2007/archive/2012/07/16/2594065.html

你可能感兴趣的文章
薏米食品领军品牌先天元,借新零售之力创造十亿级产业
查看>>
传统电视综艺垂垂老矣,尴尬期的网络综艺如何破局前行?
查看>>
必学数据结构整理
查看>>
Android Studio如何减小APK体积
查看>>
Git Personal Manual
查看>>
Android爬坑之旅之FileProvider(Failed to find configured root that contains)
查看>>
Markdown 基础语法--神兵利器
查看>>
使用Spring Request-Reply实现基于Kafka的同步请求响应
查看>>
git常用命令记录(持续更新)
查看>>
react篇---redux的使用
查看>>
Siri shortcuts 指北
查看>>
FMI人工智能与大数据线下沙龙第869期北京场圆满落幕
查看>>
vue+elementUi 实现密码显示/隐藏+小图标变化(js一共三行代码,其中一行为了美观)...
查看>>
面试:Semaphore(信号量)的成长之路
查看>>
2019年程序员跳槽找工作避坑指南
查看>>
2018年终总结
查看>>
华为云学院新春大礼包:场景化微认证上新了!
查看>>
DAppDiscover | 盘点2018年度十大DAPP
查看>>
百度地图-矩形编辑的模拟实现
查看>>
一个有趣的问题: 如何用HashSet来存储重复的字符串?
查看>>