C#在Word文档指定位置处理表格
2007-08-15 20:19
836 查看
正在做的项目里,需要开发一个小工具,将需要的数据插入到Word文档中。这当中有一项需求,要求能够在Word文档中某处插入表格,或者删除该处表格。
这个小工具是在VS.Net2005、Office2007下开发的。
1、在Word文档中插入一个书签,书签名称为“tl”;
2、在VS2005新建一个C#项目,然后在引用中添加Word类库;由于我使用的是Office2007,因此选择的是"Microsoft Word 12.0 Object Library",如果你使用的是Office2003,就应该选择11.0;
3、在代码顶部添加对Word类库的引用;
using Word = Microsoft.Office.Interop.Word;
4、打开Word文档
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
5、找到刚才添加的书签
object tmp = "t1";
Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
6、删除在该位置的表格
Word.Table tbl = startRange.Tables[1];
tbl.Delete();
如果书签所在的位置并没有插入表格,程序并不会删除该位置下面的表格,而是会抛出异常,报错。
7、插入表格,并划线
//添加表格
oDoc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);
//为表格划线
startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
全部的代码如下:
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
try
catch
object bSaveChange = true;
oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
oDoc = null;
oWord = null;
代码很简单,在写这实例的过程中我参考了如下资料:
Word对象模型概述
Word任务
这个小工具是在VS.Net2005、Office2007下开发的。
1、在Word文档中插入一个书签,书签名称为“tl”;
2、在VS2005新建一个C#项目,然后在引用中添加Word类库;由于我使用的是Office2007,因此选择的是"Microsoft Word 12.0 Object Library",如果你使用的是Office2003,就应该选择11.0;
3、在代码顶部添加对Word类库的引用;
using Word = Microsoft.Office.Interop.Word;
4、打开Word文档
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
5、找到刚才添加的书签
object tmp = "t1";
Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
6、删除在该位置的表格
Word.Table tbl = startRange.Tables[1];
tbl.Delete();
如果书签所在的位置并没有插入表格,程序并不会删除该位置下面的表格,而是会抛出异常,报错。
7、插入表格,并划线
//添加表格
oDoc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);
//为表格划线
startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
全部的代码如下:
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
try
catch
object bSaveChange = true;
oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
oDoc = null;
oWord = null;
代码很简单,在写这实例的过程中我参考了如下资料:
Word对象模型概述
Word任务
相关文章推荐
- C#在Word文档指定位置处理表格
- C#在Word文档指定位置处理表格
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- C#操作Word总结(三)——表格处理、把Word文档转化成为HTML
- C#在Word文档指定位置增加内容
- C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
- C#打开WORD文档内容并显示(未完善表格图片等排版显示的处理)
- C#打开WORD文档内容并显示(未完善表格图片等排版显示的处理)
- 使用ASPOSE.WORD组件,在word文档指定的位置动态生成表格,支持单元格合并及背景填充
- C# 在word文档指定位置插入图片
- 根据word模板生成word表格报表文档(C#)
- 用C#动态生成Word文档并将数据填入Word表格中
- [C#]用C#动态生成Word文档并将数据填入Word表格中
- C#获取Word文档页数,并跳转到指定的页面获取内容,且插入分页符
- 利用c#在Word文档中插入多个表格,包含光标定位,插入分节符,设置页边距等
- C#采用OpenXml给Word文档添加表格
- C#对Word文档的创建、插入表格、设置样式等操作实例
- 用C#动态生成Word文档并将数据填入Word表格中
- C#利用 Aspose.Words在文档指定位置插入doc文档