首页 > 编程 > C# > 正文

C# WORD操作实现代码

2019-10-29 21:48:31
字体:
来源:转载
供稿:网友
在当前项目开发过程中,客户有根据数据库数据生成WORD文档的需求,在和同事沟通的过程中,找到了两个解决方案
 
 
1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC。 
2.定制WORD文档的模板文件,在C#中操作WORD模板,生成新的WORD文档。 
第一方案简单,只需要改动文件的扩展名就行了,但是也存在了一些问题,譬如生成的WORD文档样式的丢失。这样对于客户来说可能是一个无法通过的方案。第二方案比较复杂,需要调用OFFICE的WORD组件通过C#来操作WORD,进而生成WORD。此方法类似于我们在c#中的后台拼接数据。虽然麻烦,但是能够灵活定制,只不过是操作WORD对象而已。 
经过再三考虑:决定用第二种方法来生成WORD报告文档。 
通过自己的实践,这个需求总算是搞定了,在实际开发的过程中,遇到了这样那样的问题,还好,通过不断的查找网络资源,结合实际开发中的情况,问题都得到了解决。现将本人在开发过程中的一些理解与经验总结一下: 
在VS2008平台下,引用.net-Microsoft.Office.Interop.Word.12,这样就可以在程序用操作WORD对象了。 
通过简单执行,报了80070005错误,这个错误是因为权限不够,需要在DCOM配置中更改.net和IIS用户的操作权限,具体修改过程如下: 解决方法一: 
1.控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档之后,单击属性打开此应 用程序的属性对话框。 
2.单击标识选项卡,然后选择交互式用户。 
3.单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后自定义->编辑->添加ASP.NET账户和IUSER_计算机 名。 
4. 确保允许每个用户访问,然后单击确定。 
5. 单击确定关闭 DCOMCNFG。 
如果上述方法不能解决问题,就应该是权限问题,请尝试用下面的方法: 
在web.config中使用身份模拟,在<system.web>节中加入 <identity impersonate="true" userName="你的用户名 " password="密码 "/> 
</system.web> 
解决了上述问题,开始考虑如何创建WORD模板文件,WORD的模板文件其实就是通过书签来添加内容的。也就是通过在WORD文档中创建书签,然后在程序中获取模板文件的所有书签,通过给书签赋值来进行文档生成的。 
在程序中的操作流程如下: 
声明WORD程序的对象 → 声明一个WORD文档对象 → 获取当前的操作文档对象 → 获取文档所有的书签 → 将数据库数据赋值到对应的书签 → 将文档另存为指定的文件夹下. 
下面将针对农业植物测试报告来分析具体的代码实现: 
复制代码代码如下:

//生成WORD程序对象和WORD文档对象 
Microsoft.Office.Interop.Word.Application appWord = new Application(); 
Microsoft.Office.Interop.Word.Document doc = new Document(); 
object oMissing = System.Reflection.Missing.Value;//这个是什么东西,我始终没搞明白-_- 
//打开模板文档,并指定doc的文档类型 
object objTemplate = Server.MapPath(p_TemplatePath); 
object objDocType = WdDocumentType.wdTypeDocument; 
doc = (Document)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); 
//获取模板中所有的书签 
Bookmarks odf = doc.Bookmarks; 
string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"}; 
string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",}; 
//循环所有的书签,并给书签赋值 
for (int oIndex = 0; oIndex < testTableremarks.Length; oIndex++) 

obDD_Name = WD + testTableremarks[oIndex]; 
doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = p_TestReportTable.Rows[0][testTablevalues [oIndex]].ToString();//此处Range也是WORD中很重要的一个对象,就是当前操作参数所在的区域 

//第四步 生成word,将当前的文档对象另存为指定的路径,然后关闭doc对象。关闭应用程序 
object filename = Server.MapPath(p_SavePath) + "//Testing_" + DateTime.Now.ToShortDateString() + ".doc"; 
object miss = System.Reflection.Missing.Value; 
doc.SaveAs(ref filename, 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, ref miss); 
object missingValue = Type.Missing; 
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges; 
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue); 
appWord.Application.Quit(ref miss, ref miss, ref miss); 
doc = null; 
appWord = null; 
this.Hid_ShowMessage.Value = "生成成功!";

上述代码就是一个通过模板文件生成WORD的过程。其实也就是一个替换书签内容的过程。 
在开发的过程中,有些数据是动态增加的,假如我要向一个表格中动态的添加几行数据,就无法用替换书签的方式来进行操作,需要通过程序在文档页面的表格中添加行。 
向表格中添加行,有两种操作形式:一种是在WORD模板中已经存在了一个表格。一种是我们在程序中直接添加一个表格对象。 
第一种情况下,需要注意:在WORD模板中要操作的表格中,不能有纵向合并的单元格,不然程序无法获取到当前要操作对象导致程序报错.单元格的合并,我们可以在程序中控制。 
第二种情况下就需要我们通过程序去直接添加表格了。 
生成表格的代码具体如下: 
1.获取文档中已存在的表格: 
Microsoft.Office.Interop.Word.Table characterTable = doc.Tables[2];//在document对象的集合操作中,起始点是从1开始,并不是从0开始的,此处需要注意。 
2.在文档中直接生成表格,首先要获取插入表格的位置,然后添加表格对象: 
object oEndOfDoc = "//endofdoc";//WORD中预定义的书签,还有很多,此处就不一一列举。 
object oMissing = System.Reflection.Missing.Value; 
Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//获取当前文档的末尾位置。 
wrdRng.InsertAfter(" ");//插入一行,此处不能用 wrdRng.InsertAfter(""),如果用这个,就不能换行,我也不知道为什么。 
复制代码代码如下:

object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd; 
object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//分页符 
wrdRng.Collapse(ref oCollapseEnd); 
wrdRng.InsertBreak(ref oPageBreak);//插入了一页 
wrdRng.Collapse(ref oCollapseEnd); 
wrdRng.InsertAfter("图片信息"); 
wrdRng.Font.Size = 20;//指定操作对象的文字大小 
wrdRng.Font.Bold = 1;//指定操作对象的粗体:1为粗体,0为正常 
wrdRng.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//指定操作区域的文字布局:居中对齐 
//上述代码的意思是:找到当前的末尾位置,然后插入一个分页符,相当于跳到了一个新页,在这个新页的顶端写入文字“图片信息”,并指定文字大小为20,粗体居中显示。 
wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
wrdRng.InsertAfter(" "); 
wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
wrdRng.InsertParagraphAfter();//插入一个段落,在此段落上插入一个2行一列的表格。 
Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(wrdRng, 2, 1, ref oMissing, ref oMissing);

我们还可以对表格进行格式设置,此处我们就不在一一列举。 
3.下面我们分析一下对表格的单元格的操作:合并,拆分。这个就需要我们根据实际表格来进行操作: 
//获取具体的某个单元格(1,1),获取第一行第一列的单元格 
Cell cell = doc.Tables[1].Cell(1,1); 
cell.Range.Text="Text";//指定当前单元格的内容为Text 
在Table的操作中,添加新行: 
object beforeRow = doc.Tables[1].Rows[2];//此行是先获取到第二行 
doc.Tables[1].Rows.Add(ref beforeRow);//效果类似于在WORD中此表格的第二行上进行【插入行】操作,插入的新行将会插入到当前行的上一行中,格式也是和此行一致的。 
//合并单元格:感觉在此处合并单元格挺傻瓜的,你只需要指定你要合并的起始单元格和结束单元格,然后通过Merge操作就行了 
Cell cell = doc.Tables[1].Cell(iRow, 2);//列合并 
cell.Merge(doc.Tables[1].Cell(iRow, 6)); 
Cell cell1 = doc.Tables[1].Cell(iRow - 1, 1);//行合并 
cell1.Merge(doc.Tables[1].Cell(iRow + 1, 1)); 
上述操作就是在此程序中用到的一些知识点,还有好多的东西需要去熟悉、理解。 
另外,在程序的测试过程中发现,当执行一次文档生成后,在资源管理器中始终有winword.exe进程杀不掉,目前的解决办法是:直接杀进程,代码如下: 
复制代码代码如下:

protected void killAllProcess() // 杀掉所有winword.exe进程 

System.Diagnostics.Process[] myPs; 
myPs = System.Diagnostics.Process.GetProcesses(); 
foreach (System.Diagnostics.Process p in myPs) 

if (p.Id != 0) 

string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString(); 
try 

if (p.Modules != null) 
if (p.Modules.Count > 0) 

System.Diagnostics.ProcessModule pm = p.Modules[0]; 
myS += "/n Modules[0].FileName:" + pm.FileName; 
myS += "/n Modules[0].ModuleName:" + pm.ModuleName; 
myS += "/n Modules[0].FileVersionInfo:/n" + pm.FileVersionInfo.ToString(); 
if (pm.ModuleName.ToLower() == "winword.exe") 
p.Kill(); 


catch 
{ } 
finally 






目前为止,一个WORD文档就生成了。上述为我在这个程序开发中遇到的问题和解决方法,可能有好多地方都是考虑不全的,如果在程序开发中对WORD的操作有新的认识的话,欢迎和我沟通交流,彼此提高! 
下边是在网上一些比较好的摘抄: 
创建新Word 
复制代码代码如下:

object oMissing = System.Reflection.Missing.Value; 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, 
ref oMissing, ref oMissing); 

打开文档: 
复制代码代码如下:

object oMissing = System.Reflection.Missing.Value; 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
object fileName = @"E:CCCXCXXTestDoc.doc"; 
oDoc = oWord.Documents.Open(ref fileName, 
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

导入模板 
复制代码代码如下:

object oMissing = System.Reflection.Missing.Value; 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
object fileName = @"E:XXXCCXTest.doc"; 
oDoc = oWord.Documents.Add(ref fileName, ref oMissing, 
ref oMissing, ref oMissing); 

.添加新表 
复制代码代码如下:

object oMissing = System.Reflection.Missing.Value; 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, 
ref oMissing, ref oMissing); 
object start = 0; 
object end = 0; 
Word.Range tableLocation = oDoc.Range(ref start, ref end); 
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); 

.表插入行 
复制代码代码如下:

object oMissing = System.Reflection.Missing.Value; 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, 
ref oMissing, ref oMissing); 
object start = 0; 
object end = 0; 
Word.Range tableLocation = oDoc.Range(ref start, ref end); 
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); 
Word.Table newTable = oDoc.Tables[1]; 
object beforeRow = newTable.Rows[1]; 
newTable.Rows.Add(ref beforeRow); 

.单元格合并 
复制代码代码如下:

object oMissing = System.Reflection.Missing.Value; 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, 
ref oMissing, ref oMissing); 
object start = 0; 
object end = 0; 
Word.Range tableLocation = oDoc.Range(ref start, ref end); 
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); 
Word.Table newTable = oDoc.Tables[1]; 
object beforeRow = newTable.Rows[1]; 
newTable.Rows.Add(ref beforeRow); 
Word.Cell cell = newTable.Cell(1, 1); 
cell.Merge(newTable.Cell(1, 2)); 

.单元格分离 
复制代码代码如下:

object oMissing = System.Reflection.Missing.Value; 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
oDoc = oWord.Documents.Add( oMissing, 
ref oMissing, ref oMissing); 
object start = 0; 
object end = 0; 
Word.Range tableLocation = oDoc.Range(ref start, ref end); 
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); 
Word.Table newTable = oDoc.Tables[1]; 
object beforeRow = newTable.Rows[1]; 
newTable.Rows.Add(ref beforeRow); 
Word.Cell cell = newTable.Cell(1, 1); 
cell.Merge(newTable.Cell(1, 2)); 
object Rownum = 2; 
object Columnnum = 2; 
cell.Split(ref Rownum, ref Columnnum); 

通过段落控制插入 
复制代码代码如下:

object oMissing = System.Reflection.Missing.Value; 
object oEndOfDoc = "/endofdoc"; /**//* endofdoc is a predefined bookmark */ 
//Start Word and create a new document. 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, 
ref oMissing, ref oMissing); 
//Insert a paragraph at the beginning of the document. 
Word.Paragraph oPara1; 
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); 
oPara1.Range.Text = "Heading 1"; 
oPara1.Range.Font.Bold = 1; 
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph. 
oPara1.Range.InsertParagraphAfter(); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表