惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
Project Zero
Project Zero
L
LINUX DO - 最新话题
C
Cisco Blogs
P
Privacy International News Feed
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security @ Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
Webroot Blog
Webroot Blog
K
Kaspersky official blog
Help Net Security
Help Net Security
博客园_首页
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
雷峰网
雷峰网
The Last Watchdog
The Last Watchdog
WordPress大学
WordPress大学
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
A
Arctic Wolf
I
Intezer
V
V2EX
博客园 - 【当耐特】
Latest news
Latest news
T
Tenable Blog
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
Cyberwarzone
Cyberwarzone
量子位
G
GRAHAM CLULEY
T
Troy Hunt's Blog
博客园 - Franky
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 三生石上(FineUI控件)
TaoSecurity Blog
TaoSecurity Blog
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
Jina AI
Jina AI
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Scott Helme
Scott Helme

博客园 - EricZhen

【ExtJS实践】之七 :禁止Grid、Treegrid列排序和列菜单 【ExtJS实践】之六 :Combobox从后台获取JSON格式的数据 【ExtJS实践】之五 :常用语句及脚本备忘 【ExtJS实践】之四 :关于ExtJS的createDelegate 【ExtJS实践】之三 :页面布局应用 【ExtJS实践】之二 :TreeGrid显示复选框 【ExtJS实践】之一 :TreeGrid异步加载数据 ASP.Net下使用ExtJS报“Ext未定义”错误的原因 关于在Windows2008里配置AjaxPro.2 【备忘】转:.net的数据库连接字符串 【备忘】转:模态窗口缓存问题的解决 【备忘】红旗Linux下安装VMWare Tools的方法 关于Cookie的一个小问题 [备忘]各类数据库连接字符串 [备忘]autorun专杀工具 [备忘].cll文件的MIME类型 [备忘]方正字库中英文对照表 [SharePoint2007]使用自定义数据库的几个问题 [SharePoint]不同页面间的多个数据视图间建立关联
C#在Word文档指定位置处理表格
EricZhen · 2007-08-15 · via 博客园 - EricZhen

    正在做的项目里,需要开发一个小工具,将需要的数据插入到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, 54ref 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
            
{
                
object tmp = "t1";
                Word.Range startRange 
= oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;

                
//删除指定书签位置后的第一个表格
                Word.Table tbl = startRange.Tables[1];
                tbl.Delete();

                
//添加表格
                oDoc.Tables.Add(startRange, 54ref 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;

            }

            
catch
            
{
                
//异常处理
            }


            
object bSaveChange = true;
            oDoc.Close(
ref bSaveChange, ref missingValue, ref missingValue);
            oDoc 
= null;
            oWord 
= null;