高分求助:C#怎么对WORD文档进行操作啊??

sharesoft21 2003-10-30 08:48:23
我的程序中有一个文档管理,我想对WORD文档进行操作应该怎么做呢?
...全文
22 8 打赏 收藏 转发到动态 举报
写回复
用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

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧