1.添加引用Microsoft.Office.Interop.Word.dll。若找不到则添加MIscrosoft word 11.0 Object Library 引用(在com组件中,具体的版本可能不同)
一、C#打开一个word文档
页面添加Word引用:using Microsoft.Office.Interop.Word.
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document myWordDoc=new Document (); //以下将主要用到此处所建立的对象
object filepath = @"F://test2//wordtest.doc"; //将要打开的Word文档路径
if (!System.IO.File.Exists((string)strFileName))
{
Response.Write("<script language='javascript'>alert('没有找到该目录下的文件!');</script>");
return ;
}
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 = 24.0f;//可省略
myWordDoc.Content.Font.Name = "Arial Unicode MS";//可省略
myWordDoc.Save();
richTextBox1.Text = myWordDoc.Content.Text;
myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
二、创建一个Word文档
strFileName = @"F://test2//wordtest.doc"; // 将要创建的Word文档
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" + "创建成功";
三、替换Word文档的内容
1.在Word模板中添加标签"ashen331"(可以添加n个),下面例子以一个标签"ashen331"为例.
object omissing = System.Reflection.Missing.Value;
object isreadonly = false;
object template = @"F://test2//wordtest.doc";//将要打开的Word文档
object savetoword = @"F://test2//saveword.doc";//作变动后,要保存到的目标word文档(相当另存为)
try
{
// open
myWordDoc = myWordApp.Documents.Open(ref template, ref
omissing, ref isreadonly, 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);
// change
object name = "ashen331";
if (myWordApp.ActiveDocument.Bookmarks.Exists(name.ToString()))
{
myWordDoc.Bookmarks.get_Item(ref name).Range.Text =
"我的博客";//将覆盖ashen331标签,如果选择文字插入标签的,则原文字将被覆盖,修改过后的文件将不存在此标签。如果没有选择文字插入标签
的,则将在标签位置插入文字,修改过后的文件依然存在此标签。
myWordDoc.Bookmarks.get_Item(ref name).Select();
}
//safe
myWordDoc.SaveAs(ref savetoword, 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);
}
catch (Exception ex)
{
throw new Exception("Write word document failed", ex);
}
finally
{
if (myWordDoc != null)
{
// Object saveChanges = myWordApp.Options.BackgroundSave;
myWordDoc.Close(ref omissing, ref omissing, ref omissing);
}
if (myWordApp != null)
{
myWordApp.Quit(ref omissing, ref omissing, ref omissing);
}
}

1168

被折叠的 条评论
为什么被折叠?



