开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 4738|回复: 4

用C#给word填充表格

[复制链接]

0

精华

99

贡献

36

赞扬

帖子
79
软币
769
在线时间
66 小时
注册时间
2013-6-19
发表于 2013-6-20 20:44:36 | 显示全部楼层 |阅读模式
利用Automation,可以完成对Word文档的各种复杂操作,包括文档的生成、修改、统计字数等等等等。在MSDN里面可以参考“Working with Microsoft Word Objects”一文。



private void menuItem2_Click(object sender, System.EventArgs e)
{
        Object Nothing=System.Reflection.Missing.Value;
        object filename=@"c:\test.doc";

        Word.Application wordApp=new Word.ApplicationClass();
        Word.Document wordDoc=wordApp.Documents.Open(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
       
        this.textBox1.Text+="\r\n"+wordDoc.Paragraphs.Last.Range.Text.ToString();
        this.textBox1.Text+="\r\n"+wordDoc.Tables.Item(1).Cell(1,1).Range.Text.ToString();

        wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
        wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}

private void menuItem3_Click(object sender, System.EventArgs e)
{
        Object Nothing=System.Reflection.Missing.Value;
        object filename=@"c:\test.doc";

        Word.Application wordApp=new Word.ApplicationClass();
        Word.Document wordDoc=wordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
       
        Word.Table table=wordDoc.Tables.Add(wordApp.Selection.Range,2,3,ref Nothing,ref Nothing);
        table.Cell(1,1).Range.Text="1892730987098";
        wordDoc.Paragraphs.Last.Range.Text="Hello";

        wordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
        wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
        wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}

在这段例子里面,menuItem3_Click()函数新建了一个Word文档,并在里面插入了一个表格和一段文字。表格的大小是两行三列,最左上的cell里面的内容是"1892730987098",后面一段文字的内容是"Hello"。其大致如下:

+---------------+--------------+--------------+
| 1892730987098 |              |              |
+---------------+--------------+--------------+
|               |              |              |
+---------------+--------------+--------------+
Hello

上面的例子代码中,menuItem2_Click()完成的工作就是打开上面创建的Word文档,并读取表格的第一个cell的内容以及下面一段文字的内容,然后将其显示在this.textBox1中。

您可以试试看上面这段例子代码,运行前需要在项目的Reference里面添加Microsoft Word 10.0 Object Library。

评分

参与人数 4贡献 +3 赞扬 +4 收起 理由
DevilHero + 1 很给力
onebug + 1 赞一个
威廉乔克斯 + 2 赞一个
羽叶 + 3 赞一个

查看全部评分

回复

使用道具 举报

0

精华

1057

贡献

1935

赞扬

正版授权组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
320
软币
15508
在线时间
1532 小时
注册时间
2013-6-9
发表于 2013-6-20 21:18:43 | 显示全部楼层
能完美的显示word么?一直想找个方法完美的加载word,支持vba
回复

使用道具 举报

0

精华

74

贡献

722

赞扬

正版授权组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
183
软币
3861
在线时间
327 小时
注册时间
2013-6-7
发表于 2013-6-20 21:41:08 | 显示全部楼层
我现在是,只要能不用 OLE Automation 就尽量不用, 感觉太不稳定,
操纵 Word 我肯定选择 Aspose 这样的第三方控件.
回复

使用道具 举报

0

精华

2887

贡献

2127

赞扬

正版授权组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
200
软币
12391
在线时间
2044 小时
注册时间
2013-6-22

胡吹海聊神经正常

发表于 2013-6-22 18:50:22 | 显示全部楼层
我正准备做做word方面,可以简化平时办公的重复操作,谢谢。
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
12
软币
92
在线时间
1 小时
注册时间
2019-4-23
发表于 2019-4-24 10:13:22 | 显示全部楼层
强强强强强强强强!
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-30 03:04

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表