Word的操作在生活中,经常用到。这里简单介绍一下如何用c#操作Word。
(1) 在开始编程之前,要导入word的组件。方法如下:
右击解决方案----》添加引用------》.net----》 选择“Microsoft.Office.Interop.Word 11.0.0.0.。。。”(如下图,这是11.0.0.0版本,12.0.0.0版本我尝试过,但不行)
(2) 添加命名空间:
using Microsoft.Office.Interop.Word;
(3)该写代码了,这里我直接贴出来(这里为了方便讲解,有些代码会冗余,请见谅!),不打算详细讲解,因为已经有详细的注释了。在后面我会讲一下一些注意事项。最后附录该实例下载地址。
return path.FileName.ToString();
}
/// <summary>
/// 返回保存文件路径
/// </summary>
/// <returns></returns>
private string SavePath()
{
SaveFileDialog path = new SaveFileDialog();
path.ShowDialog();
return path.FileName.ToString();
}
//判断文件是否已存在
if (System.IO.File.Exists((string)strFileName+".doc"))
{ //提示是否覆盖
if (DialogResult.No == MessageBox.Show("该文件已经存在,是否覆盖?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
flag = false;
}
else
{ //删除已有文件
System.IO.File.Delete((string)strFileName);
}
}
if (flag && (string)strFileName != "")
{
//word对象实例化
myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//把TextBox内容写入Word写入
string strContent = textWrite.Text; ;
myWordDoc.Paragraphs.Last.Range.Text = strContent;
//将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);
//记得一定要加上这句,否则每运行一次,就产生一个word进程。
myWordDoc = null;
//关闭WordApp组件对象
myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
MessageBox.Show("创建成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("创建失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//判断路径是否非空
if ((string)strFileName != "")
{
myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
//打开指定word文档
myWordDoc = myWordApp.Documents.Open(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);
//读取word内容到textbox
textRead.Text = myWordDoc.Content.Text;
//关闭,保存
myWordDoc.Save();
myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
//判断路径是否非空
if ((string)strFileName != "")
{
myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
//打开指定word文档
myWordDoc = myWordApp.Documents.Open(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);
//移动焦点并换行,防止重叠
object count = 14; //视具体情况,大小可调整
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
myWordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
myWordApp.Selection.TypeParagraph();//插入段落
//创建一个表格
Microsoft.Office.Interop.Word.Table table1 = myWordDoc.Tables.Add(myWordApp.Selection.Range, 5, 3, ref Nothing, ref Nothing);
//填充表格内容:小标初始位不同于数组的习惯,从1开始,即(2,2)表示第2行第2列
table1.Cell(2, 2).Range.Text = textFull.Text ;
//插入图片
string strPictureName = textPath.Text.Trim();
if (strPictureName != "" && System.IO.File.Exists(strPictureName))
{
//移动焦点并换行,防止重叠
count = 14;//视具体情况,大小可调整
WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
myWordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
myWordApp.Selection.TypeParagraph();//插入段落
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = myWordDoc.Application.Selection.Range;
//插入
myWordDoc.Application.ActiveDocument.InlineShapes.AddPicture(strPictureName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
//设置图片宽度和高度
myWordDoc.Application.ActiveDocument.InlineShapes[1].Width = 200f;
myWordDoc.Application.ActiveDocument.InlineShapes[1].Height = 200f;
}
//关闭,保存
myWordDoc.Save();
myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
最后,讲下一些注意事项:
(1)关于创建一个新word文档时,每运行一次都会产生一个word的进程。这是因为word对象没有回收成功。关闭代码如下:
(2)设置Microsoft.Office.Interop.Word.Application myWordApp为全局变量时,运行一次后,再运行会报错,如下:
这是因为,word对象仍在使用,没有关闭。建议,设为局部变量。
(3)编译时,会提示警告,如下内容:
这是方法不确定性引起的歧义。我也还没找到解决方法,找到的朋友,麻烦告诉声。
(4)word的操作还有很多,例如,表格的处理(合并,拆分,样式,对齐方式等),页眉页脚,图片文字环绕等等。如果,有空,我会继续补充。
(5)你或许疑问,我为什么引用Word类时,总要加上“Microsoft.Office.Interop.Word.”,不是繁琐吗?其实,我也是迫于无奈。因为不加上,我vs08提示缺少using,找不到该类库。不知道,你们会否出现该问题。
(6)该实例运行图:
http://download.csdn.net/source/2202277
希望对大家有用。有疑问,可以q我。