原文出处 http://www.blue1000.com/bkhtml/2008-06/57215.htm
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HTML Controls;
using Microsoft.office ;
using Microsoft.office .Core;
using Microsoft.office .Interop;
using Microsoft.office .Interop.word ;
打开word 2003文档,项目中的文档模板实际是doc的文档,dot的很不好用.
做doc文档模板的时候不要用空格留空白,使用段落缩进的方式.标签的添加等文档格式完全编辑好再添加.
对于要预留空白的内容,通过插入1个只有1个单元格(无边框)的方式来进行.这样保证单元格以后内容位置固定,不会因为插入了文字内容而移动位置,这队固定格式公文很有好处.
打开word 模板和word文件#region 打开word模板和word文件
public static void Openword Dot()
{
}
private void Openword Doc(string FileName, ref Microsoft.office .Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
if (FileName == "") return;
Microsoft.office .Interop.word .Document thisDocument = null;
Microsoft.office .Interop.word .FormFields formFields = null;
Microsoft.office .Interop.word .Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
thisApplication.Visible = true;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object Password Document = System.Type.Missing;
Object Password Template = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePassword Document = System.Type.Missing;
Object WritePassword Template = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XML Transform = System.Type.Missing;
try
{
Microsoft.office .Interop.word .Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref Password Document, ref PasswordTemplate,
ref Revert, ref WritePassword Document, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XML Transform);
thisDocument = word Doc;
wDoc = word Doc;
WApp = thisApplication;
formFields = word Doc.FormFields;
}
catch (Exception ex)
{
}
}
#endregion
读取文档中标签数据处理方法
1 获取文档中的标签列表.把文档对象的书签列表读到IEnumerator中.
2 通过枚举推进的方式读取每个书签.
3 关闭文档
//获取标签数据
public string GetDocumentBookmarkData(string FileName)
{
Microsoft.office .Interop.word .Document wDoc = null;
Microsoft.office .Interop.word .Application wApp = null;
this.Openword Doc(FileName,ref wDoc, ref wApp);
object oEndOfDoc = "//endofdoc";
object missing = System.Reflection.Missing.Value;
string str="";
System.Collections.IEnumerator enu=wApp.ActiveDocument.Bookmarks.GetEnumerator();
while(enu.MoveNext())
{
Microsoft.office .Interop.word .Bookmark bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current;
str += "{"+bk.Name.ToString() + ":"+bk.Range.Text+"}";
}
object o = null;
wDoc.Close(ref missing, ref missing, ref missing);
wApp.Quit(ref missing, ref missing, ref missing);
if (wDoc!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
wDoc = null;
}
if (wApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
wApp = null;
}
GC.Collect();
return str;
}
往书签中写入数据
1 把书签的名字通过枚举的方式读出来,写到数组里(图/表格等特殊数据书签要处理掉)
2 读取数据库 数据表内容写入书签初.注意技巧.
a 检查文档书签集合中存在书签
b 获取文档书签,并选择他,写入数据到selection
c 移动书签的end到合适位置,否则读书签数据永远只读到书签定义处的字符.
d 对于图/表格等的插入需要特殊处理.
e 扫尾 另存.不要覆盖原来模板哦
//设定标签的数据
public string SetDocumentBookmarkData(string FileName,System.Data.DataTable dt)
{
//打开文档
Microsoft.office .Interop.word .Document wDoc = null;
Microsoft.office .Interop.word .Application wApp = null;
this.Openword Doc(FileName, ref wDoc, ref wApp);
object oEndOfDoc = "//endofdoc";
object missing = System.Reflection.Missing.Value;
//设定标签数据
System.Collections.IEnumerator enu = wApp.ActiveDocument.Bookmarks.GetEnumerator();
string[] strbook=new string[dt.Columns.Count-1];
int i=0;
Microsoft.office .Interop.word .Bookmark bk=null;
while (enu.MoveNext())
{
bk = (Microsoft.office .Interop.word .Bookmark)enu.Current;
if (bk.Name.ToString().Trim()!="Table")
{
strbook[i] = bk.Name.ToString();
i++;
}
}
object tempobject=null;
int length = 0;
for (i = 0; i < strbook.Length;i++ )
{
tempobject = strbook[i].ToString();
if (wApp.ActiveDocument.Bookmarks.Exists(strbook[i].ToString()))
{
wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).Select();
wApp.Selection.Text = dt.Rows[0][strbook[i]].ToString();
length = dt.Rows[0][strbook[i]].ToString().Length;
wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).End = wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).End + length;
}
}
InsertTabletoBookmark("Table",ref wDoc, ref wApp);
//收尾工作
object o = null;
string guid = System.Guid.NewGuid().ToString();
object sFileName = "D:/Test/office /word /" + guid + ".doc";
if (wDoc.SaveFormat == (int)Microsoft.office .Interop.word .WdSaveFormat.wdFormatDocument)
{
wDoc.Application.ActiveDocument.SaveAs(ref sFileName, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing,
ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
}
wDoc.Close(ref missing, ref missing, ref missing);
wApp.Quit(ref missing, ref missing, ref missing);
if (wDoc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
wDoc = null;
}
if (wApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
wApp = null;
}
GC.Collect();
return guid + ".doc";
}
表格的插入方法
1 表格的插入很简单,图表可参照
2 表格插入后如何获取表格请注意.用书签的Range对象的Tables集合
插入表格数据#region 插入表格数据
private bool InsertTabletoBookmark(string objTable,ref Microsoft.office .Interop.word .Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
object oEndOfDoc = "//endofdoc";
object missing = System.Reflection.Missing.Value;
object objBookmark = objTable;
WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Select();
Microsoft.office .Interop.word .Table table = wDoc.Tables.Add(WApp.Selection.Range, 3, 4, ref missing, ref missing);
table.Cell(1, 1).Range.Text = "表:" + WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Range.Tables[1].Rows.Count;
return true;
}
结束语
由于生成文档用户修改后往往会丢掉书签,数据就读不到了,所以生成文档还不能提交给用户修改保存,那位有办法的欢迎指教.