.Net操作Word
1: public class WordHelp
2: {
3: private Word.ApplicationClass oWordApplic; // Word程序引用
4: private Word.Document oDoc; // Word文档引用
5: object missing = System.Reflection.Missing.Value;
6:
7: public Word.ApplicationClass WordApplication
8: {
9: get { return oWordApplic; }
10: }
11:
12: public WordHelp()
13: {
14: // activate the interface with the COM object of Microsoft Word
15: oWordApplic = new Word.ApplicationClass();
16: }
17:
18: public WordHelp(Word.ApplicationClass wordapp)
19: {
20: oWordApplic = wordapp;
21: }
22:
23: /// <summary>
24: /// 打开word文件
25: /// </summary>
26: /// <param name="strFileName">输入参数:word文件位置</param>
27: /// <param name="strContent">输出参数:word内容</param>
28: public void Open(string strFileName, ref string strContent)
29: {
30: object fileName = strFileName;
31: object readOnly = false;
32: object isVisible = true;
33:
34: oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
35: ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
36: ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
37:
38: //选择文档的范围
39: oDoc.ActiveWindow.Selection.WholeStory();
40: //复制
41: oDoc.ActiveWindow.Selection.Copy();
42: //获取复制板的内容
43: IDataObject data = Clipboard.GetDataObject();
44: //获取word的内容
45: strContent = data.GetData(DataFormats.Text).ToString();
46: oDoc.Activate();
47: }
48:
49: /// <summary>
50: /// 写Word文件
51: /// </summary>
52: /// <param name="strFileName">输入参数:word模板位置</param>
53: /// <param name="toFileName">输入参数:word保存位置</param>
54: /// <param name="strWrite">输入参数:word的内容</param>
55: public void WriteWord(string strFileName,string toFileName,string strWrite )
56: {
57: try
58: {
59:
60: object fileName = strFileName;
61: object toFileName2 = toFileName;
62: object readOnly = false;
63: object isVisible = true;
64:
65: oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
66: ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
67: ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
68:
69: //选择文档的范围
70: oDoc.ActiveWindow.Selection.WholeStory();
71: Word.Range rng = oDoc.Content;
72: int lenght = oDoc.Characters.Count - 1;
73: object start = lenght;
74: object end = lenght;
75: rng = oDoc.Range(ref start, ref end);
76: rng.Text = strWrite;
77: oDoc.SaveAs(ref toFileName2, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
78: ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
79: ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
80: ref Nothing);
81: Quit();
82: }
83: catch (Exception ex)
84: {
85: }
86:
87: }
88:
89: /// <summary>
90: /// 关闭Word
91: /// </summary>
92: public void Quit()
93: {
94: oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
95: }
96: private static object Nothing = System.Reflection.Missing.Value;
97:
98:
99: /// <summary>
100: /// 创建一个word文档
101: /// </summary>
102: /// <param name="FileName">输入参数:word的位置</param>
103: /// <param name="HideWin">输入参数:是否打开文件</param>
104: /// <returns></returns>
105: public Word.Document CreateWordDocument(string FileName, bool HideWin)
106: {
107: if (FileName == "") return null;
108:
109: oWordApplic.Visible = HideWin;
110: oWordApplic.Caption = "";
111: oWordApplic.Options.CheckSpellingAsYouType = false;
112: oWordApplic.Options.CheckGrammarAsYouType = false;
113:
114: Object filename = FileName;
115: Object ConfirmConversions = false;
116: Object ReadOnly = true;
117: Object AddToRecentFiles = false;
118:
119: Object PasswordDocument = System.Type.Missing;
120: Object PasswordTemplate = System.Type.Missing;
121: Object Revert = System.Type.Missing;
122: Object WritePasswordDocument = System.Type.Missing;
123: Object WritePasswordTemplate = System.Type.Missing;
124: Object Format = System.Type.Missing;
125: Object Encoding = System.Type.Missing;
126: Object Visible = System.Type.Missing;
127: Object OpenAndRepair = System.Type.Missing;
128: Object DocumentDirection = System.Type.Missing;
129: Object NoEncodingDialog = System.Type.Missing;
130: Object XMLTransform = System.Type.Missing;
131: try
132: {
133: Word.Document wordDoc = oWordApplic.Documents.Open(ref filename, ref ConfirmConversions,
134: ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
135: ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
136: ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
137: ref NoEncodingDialog, ref XMLTransform);
138: return wordDoc;
139:
140: }
141: catch (Exception ex)
142: {
143: MessageBox.Show(ex.Message);
144: return null;
145: }
146: }
147:
148:
149: }
注意首先要添加引用
using Word;
对应的DLL为:Interop.Word.dll