2005(C#),练习第一步整理出来的
ClssControlWord类:
using
System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Word;
namespace WordControl
... {
public class ClssControlWord
...{
属性#region 属性
object missing = System.Reflection.Missing.Value;
private string strWordFullFilePath; //文件全路径
private bool bWordVisible = false; //Word文件操作是否可见
Word.Application WordApp = new Word.Application();
Word.Document WordDoc;
/**//// <summary>
/// word文件全路径
/// </summary>
public string WordFullFilePat
...{
set ...{ strWordFullFilePath = value; }
get ...{ return strWordFullFilePath; }
}
/**//// <summary>
/// word文件操作是否可见
/// </summary>
public bool WordVisible
...{
set ...{ bWordVisible = value; }
get ...{ return bWordVisible; }
}
#endregion
构造函数#region 构造函数
/**//// <summary>
/// 构造ClssControlWord
/// </summary>
public ClssControlWord()
...{ }
/**//// <summary>
/// 构造ClssControlWord
/// </summary>
/// <param name="strFullFilePath">word文件全路径</param>
public ClssControlWord(string strFullFilePath)
...{
strWordFullFilePath = strFullFilePath;
}
#endregion
方法#region 方法
/**//// <summary>
/// 打开
/// </summary>
public void Open()
...{
WordApp.Visible = bWordVisible; //word是否可见
object objWordVisible = bWordVisible; //打开的文件是否可见
object objFullFilePath = strWordFullFilePath;
//这样是打开已存在的文档
WordDoc = WordApp.Documents.Open(ref objFullFilePath,
ref missing, ref objWordVisible, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref objWordVisible,
ref missing, ref missing, ref missing, ref missing);
}
/**//// <summary>
/// 保存当前word文档
/// </summary>
public void Save()
...{
WordApp.ActiveDocument.Save();
}
/**//// <summary>
/// 关闭
/// </summary>
public void Close()
...{
try
...{
object bSave = false; //保存改变
if (WordDoc != null)
...{
WordDoc.Close(ref bSave, ref missing, ref missing);
WordDoc = null;
}
if (WordApp != null)
...{
WordApp.Application.Quit(ref bSave, ref missing, ref missing);
WordApp = null;
}
}
catch
...{ }
finally
...{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
/**//// <summary>
/// 新建word文件
/// </summary>
/// <param name="strFullFilePath">word文件路径</param>
public void CreateWord(string strFullFilePath)
...{
//验证路径
if (!Directory.Exists(Path.GetDirectoryName(strFullFilePath))) return;
if (Path.GetExtension(strFullFilePath).ToLower() != ".doc") return;
CreateWordFile(strFullFilePath);
}
/**//// <summary>
/// 新建word模板文件
/// </summary>
/// <param name="strFullFilePath">word文件路径</param>
public void CreateWordTemplate(string strFullFilePath)
...{
//验证路径
if (!Directory.Exists(Path.GetDirectoryName(strFullFilePath))) return;
if (Path.GetExtension(strFullFilePath).ToLower() != ".dot") return;
CreateWordFile(strFullFilePath);
}
/**//// <summary>
/// 新建Word文件
/// </summary>
/// <param name="strFullFilePath">word文件路径</param>
private void CreateWordFile(string strFullFilePath)
...{
try
...{
object objFullFilePath = strFullFilePath;
WordDoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
if (File.Exists(strFullFilePath)) File.Delete(strFullFilePath);
WordApp.ActiveDocument.SaveAs(ref objFullFilePath,
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);
return;
}
catch
...{
return;
}
}
/**//// <summary>
/// 从模版创建word文件
/// </summary>
/// <param name="strDotPath">word模版路径</param>
/// <param name="strDocPath">word文件路径</param>
public void CreateFileFromDot(string strDotPath, string strDocPath)
...{
object objDotPath = strDotPath;
object objDocPath = strDocPath;
if (File.Exists(strDocPath))//删除已存在的文件
...{
File.Delete(strDocPath);
}
WordApp.Documents.Add(ref objDotPath, ref missing, ref missing, ref missing);
WordApp.ActiveDocument.SaveAs(ref objDocPath,
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);
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using Word;
namespace WordControl
... {
public class ClssControlWord
...{
属性#region 属性
object missing = System.Reflection.Missing.Value;
private string strWordFullFilePath; //文件全路径
private bool bWordVisible = false; //Word文件操作是否可见
Word.Application WordApp = new Word.Application();
Word.Document WordDoc;
/**//// <summary>
/// word文件全路径
/// </summary>
public string WordFullFilePat
...{
set ...{ strWordFullFilePath = value; }
get ...{ return strWordFullFilePath; }
}
/**//// <summary>
/// word文件操作是否可见
/// </summary>
public bool WordVisible
...{
set ...{ bWordVisible = value; }
get ...{ return bWordVisible; }
}
#endregion
构造函数#region 构造函数
/**//// <summary>
/// 构造ClssControlWord
/// </summary>
public ClssControlWord()
...{ }
/**//// <summary>
/// 构造ClssControlWord
/// </summary>
/// <param name="strFullFilePath">word文件全路径</param>
public ClssControlWord(string strFullFilePath)
...{
strWordFullFilePath = strFullFilePath;
}
#endregion
方法#region 方法
/**//// <summary>
/// 打开
/// </summary>
public void Open()
...{
WordApp.Visible = bWordVisible; //word是否可见
object objWordVisible = bWordVisible; //打开的文件是否可见
object objFullFilePath = strWordFullFilePath;
//这样是打开已存在的文档
WordDoc = WordApp.Documents.Open(ref objFullFilePath,
ref missing, ref objWordVisible, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref objWordVisible,
ref missing, ref missing, ref missing, ref missing);
}
/**//// <summary>
/// 保存当前word文档
/// </summary>
public void Save()
...{
WordApp.ActiveDocument.Save();
}
/**//// <summary>
/// 关闭
/// </summary>
public void Close()
...{
try
...{
object bSave = false; //保存改变
if (WordDoc != null)
...{
WordDoc.Close(ref bSave, ref missing, ref missing);
WordDoc = null;
}
if (WordApp != null)
...{
WordApp.Application.Quit(ref bSave, ref missing, ref missing);
WordApp = null;
}
}
catch
...{ }
finally
...{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
/**//// <summary>
/// 新建word文件
/// </summary>
/// <param name="strFullFilePath">word文件路径</param>
public void CreateWord(string strFullFilePath)
...{
//验证路径
if (!Directory.Exists(Path.GetDirectoryName(strFullFilePath))) return;
if (Path.GetExtension(strFullFilePath).ToLower() != ".doc") return;
CreateWordFile(strFullFilePath);
}
/**//// <summary>
/// 新建word模板文件
/// </summary>
/// <param name="strFullFilePath">word文件路径</param>
public void CreateWordTemplate(string strFullFilePath)
...{
//验证路径
if (!Directory.Exists(Path.GetDirectoryName(strFullFilePath))) return;
if (Path.GetExtension(strFullFilePath).ToLower() != ".dot") return;
CreateWordFile(strFullFilePath);
}
/**//// <summary>
/// 新建Word文件
/// </summary>
/// <param name="strFullFilePath">word文件路径</param>
private void CreateWordFile(string strFullFilePath)
...{
try
...{
object objFullFilePath = strFullFilePath;
WordDoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
if (File.Exists(strFullFilePath)) File.Delete(strFullFilePath);
WordApp.ActiveDocument.SaveAs(ref objFullFilePath,
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);
return;
}
catch
...{
return;
}
}
/**//// <summary>
/// 从模版创建word文件
/// </summary>
/// <param name="strDotPath">word模版路径</param>
/// <param name="strDocPath">word文件路径</param>
public void CreateFileFromDot(string strDotPath, string strDocPath)
...{
object objDotPath = strDotPath;
object objDocPath = strDocPath;
if (File.Exists(strDocPath))//删除已存在的文件
...{
File.Delete(strDocPath);
}
WordApp.Documents.Add(ref objDotPath, ref missing, ref missing, ref missing);
WordApp.ActiveDocument.SaveAs(ref objDocPath,
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);
}
#endregion
}
}
应用的练习代码:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WordControl
... {
public partial class Form1 : Form
...{
private ClssControlWord ccw;
public Form1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
openFileDialog1.ShowDialog();
}
private void btSearch_Click(object sender, EventArgs e)
...{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
tbFilePath.Text = openFileDialog1.FileName;
}
private void btOpen_Click(object sender, EventArgs e)
...{
ccw = new ClssControlWord(tbFilePath.Text);
ccw.WordVisible = true;
ccw.Open();
}
private void btClose_Click(object sender, EventArgs e)
...{
ccw.Close();
}
private void btNewWord_Click(object sender, EventArgs e)
...{
saveFileDialog1.Filter = "Word文件|*.doc";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
...{
ClssControlWord cc = new ClssControlWord();
cc.CreateWord(saveFileDialog1.FileName);
cc.Close();
}
}
private void btNewWordTemplate_Click(object sender, EventArgs e)
...{
saveFileDialog1.Filter = "Word模板文件|*.dot";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
...{
ClssControlWord cc = new ClssControlWord();
cc.CreateWordTemplate(saveFileDialog1.FileName);
cc.Close();
}
}
private void btDotToDoc_Click(object sender, EventArgs e)
...{
openFileDialog1.Filter = "Word模板文件|*.dot";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
...{
saveFileDialog1.Filter = "Word文件|*.doc";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
...{
ClssControlWord cc = new ClssControlWord();
cc.CreateFileFromDot(openFileDialog1.FileName, saveFileDialog1.FileName);
cc.Close();
}
}
}
private void btSave_Click(object sender, EventArgs e)
...{
ccw.Save();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WordControl
... {
public partial class Form1 : Form
...{
private ClssControlWord ccw;
public Form1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
openFileDialog1.ShowDialog();
}
private void btSearch_Click(object sender, EventArgs e)
...{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
tbFilePath.Text = openFileDialog1.FileName;
}
private void btOpen_Click(object sender, EventArgs e)
...{
ccw = new ClssControlWord(tbFilePath.Text);
ccw.WordVisible = true;
ccw.Open();
}
private void btClose_Click(object sender, EventArgs e)
...{
ccw.Close();
}
private void btNewWord_Click(object sender, EventArgs e)
...{
saveFileDialog1.Filter = "Word文件|*.doc";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
...{
ClssControlWord cc = new ClssControlWord();
cc.CreateWord(saveFileDialog1.FileName);
cc.Close();
}
}
private void btNewWordTemplate_Click(object sender, EventArgs e)
...{
saveFileDialog1.Filter = "Word模板文件|*.dot";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
...{
ClssControlWord cc = new ClssControlWord();
cc.CreateWordTemplate(saveFileDialog1.FileName);
cc.Close();
}
}
private void btDotToDoc_Click(object sender, EventArgs e)
...{
openFileDialog1.Filter = "Word模板文件|*.dot";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
...{
saveFileDialog1.Filter = "Word文件|*.doc";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
...{
ClssControlWord cc = new ClssControlWord();
cc.CreateFileFromDot(openFileDialog1.FileName, saveFileDialog1.FileName);
cc.Close();
}
}
}
private void btSave_Click(object sender, EventArgs e)
...{
ccw.Save();
}
}
}