社区
C#
帖子详情
高分求助:C#怎么对WORD文档进行操作啊??
sharesoft21
2003-10-30 08:48:23
我的程序中有一个文档管理,我想对WORD文档进行操作应该怎么做呢?
...全文
22
8
打赏
收藏
高分求助:C#怎么对WORD文档进行操作啊??
我的程序中有一个文档管理,我想对WORD文档进行操作应该怎么做呢?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
sharesoft21
2003-10-31
打赏
举报
回复
我有一些电子文档,通过C#写的程序来管理,要在里面进行修改等,
如果这样的话是不是比较麻烦呢?C#有没有控件可以操作?
chanook
2003-10-30
打赏
举报
回复
可参考 Microsoft Visual Studio .NET 帮助文档的Visual Studio 示例>Visual C# 示例>通用示例>AutoWord 示例
storm97
2003-10-30
打赏
举报
回复
你看一下,下面用C#给Word田家一个表格:
利用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。
liduke
2003-10-30
打赏
举报
回复
也是差不多,引入dll后,用它的类就行了
tgliu
2003-10-30
打赏
举报
回复
UP
do_ve
2003-10-30
打赏
举报
回复
关注!
sharesoft21
2003-10-30
打赏
举报
回复
用C#的怎么做啊 ??
dibov
2003-10-30
打赏
举报
回复
Look This:
Dim objWd As Word.Application
Dim objDoc As Word.Document
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim objWord As Object
objWd = CreateObject("Word.Application")
'objWd.Visible = False
objWd.Documents.Add(VB6.GetPath & "\test.dot", False)
objDoc = objWd.ActiveDocument
objDoc.Activate()
objWd.Visible = True
objDoc.Variables.Item("zi").Value = IIf(zi.Text = "", " ", zi.Text)
objDoc.Variables.Item("hao").Value = IIf(hao.Text = "", " ", hao.Text)
objDoc.Variables.Item("pifu").Value = IIf(pifu.Text = "", " ", pifu.Text)
objDoc.Variables.Item("titou").Value = IIf(titou.Text = "", " ", titou.Text)
objDoc.Variables.Item("neirong").Value = IIf(neirong.Text = "", " ", neirong.Text)
objDoc.Variables.Item("attch1").Value = IIf(attch1.Text = "", " ", attch1.Text)
objDoc.Variables.Item("attch2").Value = IIf(attch2.Text = "", " ", attch2.Text)
objDoc.Variables.Item("month").Value = IIf(tmonth.Text = "", " ", tmonth.Text)
objDoc.Variables.Item("day").Value = IIf(tday.Text = "", " ", tday.Text)
objDoc.Variables.Item("fuzhu").Value = IIf(fuzhu.Text = "", " ", fuzhu.Text)
objDoc.Variables.Item("zhutici").Value = IIf(zhutici.Text = "", " ", zhutici.Text)
objDoc.Variables.Item("chaosong").Value = IIf(chaosong.Text = "", " ", chaosong.Text)
objDoc.Variables.Item("month1").Value = IIf(month1.Text = "", " ", month1.Text)
objDoc.Variables.Item("day1").Value = IIf(day1.Text = "", " ", day1.Text)
objDoc.Fields.Update()
objDoc.SaveAs((VB6.GetPath & "\test1.doc"))
objDoc.Close()
objDoc = Nothing
objWord = Nothing
End Sub
2024软考信息系统项目管理师--论文写作精讲论文范文
软考信息系统项目管理师论文视频培训课程:分别对十大领域各领域论文
进行
讲解,每个视频都是一篇范文,教你如果书写摘要、正文、总结,用短的时间,让你学会论文的写作,
高分
通过。
CSDN回帖得分大全(近两年)
√vs2005调用dll的时候Initialize()函数返回错误 [VC/MFC 基础类] 40 ylongwu 05-21 20:486 ylongwu06-28 13:42管理√为什么我创建登陆框之后,然后获取登陆框的数据时候总是出现非法
操作
! [VC/MFC 界面] 40 chencheng8095 05-23 00:2911 xianglitian06-10 11:51管理√CFileFind::FindFile 支持通配符么? [VC/MFC 基础类] 100 wysbk002 05-22
VC++中可能遇到的问题收藏
VC++中可能遇到的问题收藏 | 旧一篇: 在VC中动态加载ODBC时的一些小细节 function StorePage(){d=document;t=d.selection?(d.selection.type!=None?d.selection.createRange().text:):(d.getSelection?d.getSelection():);
Oracle官方文档及个人书签地址
Oracle官方文档及个人书签地址 Oracle 资料下载地址:https://edelivery.oracle.com/&...
WEB开发文档2 总结
转自:http://blog.donews.com/lvjiyong/archive/2006/06/29/931071.aspx怎样将后台生成的在内存中的图象显示到客户端Microsoft IE WebControls下载地址如何在DATAGRID中使用JAVASCRIPT脚本控制DataGrid中连接到下一页显示数据下载中文名文件时保存文件名乱码问题关于用ASP.net绘图的问题,请大虾指教那
C#
110,499
社区成员
642,576
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章