C#操作Word文档(Office 2007)

yanglilibaobao 2007-01-11 07:10:48
首先引入类库,Microsoft.Office.Interop.Word,然后进行编程。代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace WordTest
{
public partial class Form1 : Form
{
object strFileName;
Object Nothing;
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Document myWordDoc;
string strContent = "";

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
createWord();
//openWord();
}

private void createWord()
{
strFileName = System.Windows.Forms.Application.StartupPath + "test.doc";
if (System.IO.File.Exists((string)strFileName))
System.IO.File.Delete((string)strFileName);
Object Nothing = System.Reflection.Missing.Value;
myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

#region 将数据库中读取得数据写入到word文件中

strContent = "你好\n\n\r";
myWordDoc.Paragraphs.Last.Range.Text = strContent;

strContent = "这是测试程序";
myWordDoc.Paragraphs.Last.Range.Text = strContent;


#endregion

//将WordDoc文档对象的内容保存为DOC文档
myWordDoc.SaveAs(ref strFileName, 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文档对象
myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

this.richTextBox1.Text = strFileName + "\r\n" + "创建成功";

}
private void openWord()
{
fontDialog1.ShowDialog();
System.Drawing.Font font = fontDialog1.Font;
object filepath = "D:\\asp.docx";
object oMissing = System.Reflection.Missing.Value;
myWordDoc = myWordApp.Documents.Open(ref filepath, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
myWordDoc.Content.Font.Size = font.Size;
myWordDoc.Content.Font.Name = font.Name;
myWordDoc.Save();
richTextBox1.Text = myWordDoc.Content.Text;


myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
}

}

转http://develop.csai.cn/c/200612201000341746.htm
...全文
11029 97 打赏 收藏 转发到动态 举报
写回复
用AI写文章
97 条回复
切换为时间正序
请发表友善的回复…
发表回复
cslixu0394 2012-03-16
  • 打赏
  • 举报
回复
好东西 学习了
EMMET_C 2011-12-03
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
馨香紫韵 2011-12-03
  • 打赏
  • 举报
回复
[Quote=引用 73 楼 diasuler 的回复:]
/// <summary>
/// 更改某表的某个单元格的内容
/// </summary>
/// <param name= "tableID "> table id </param>
/// <par……
[/Quote]


请问,在插入图表哪里,,Graph.XlChartType 这是引用那个命名空间 呢??
馨香紫韵 2011-12-03
  • 打赏
  • 举报
回复
在插入图表哪里,,Graph.XlChartType 这是引用那个命名空间 呢??
沙客 2011-07-15
  • 打赏
  • 举报
回复
为什么写入word 后文档就没用了呢
bglbglxy 2011-04-13
  • 打赏
  • 举报
回复
谢谢楼主分享
originsoft 2010-07-14
  • 打赏
  • 举报
回复
学习了
Yyzhen 2009-12-08
  • 打赏
  • 举报
回复
otising 2009-01-13
  • 打赏
  • 举报
回复
好文,学习中!
guobingke 2007-12-13
  • 打赏
  • 举报
回复
学习,收藏
yuyyu2016 2007-09-21
  • 打赏
  • 举报
回复
mark
deathbee 2007-06-22
  • 打赏
  • 举报
回复


(以下签名由MyCSDN回复工具生成)
http://passport.csdn.net/UserLogin.aspx
eyuannet 2007-06-11
  • 打赏
  • 举报
回复
mark
90 2007-04-17
  • 打赏
  • 举报
回复
?
ren0594 2007-01-30
  • 打赏
  • 举报
回复
mark
zzfjfhwfnhti 2007-01-29
  • 打赏
  • 举报
回复
记号
xu_2007 2007-01-28
  • 打赏
  • 举报
回复
akbar808 2007-01-28
  • 打赏
  • 举报
回复
如果有,关于word文字校对(spellcheck)方面的代码,或编成思路文档的话分享以下好吗。
jinglong6511 2007-01-27
  • 打赏
  • 举报
回复
收藏了
zs174 2007-01-26
  • 打赏
  • 举报
回复
收藏了
加载更多回复(77)

110,502

社区成员

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

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

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