public class CCWordApp 
{ 
//it's a reference to the COM object of Microsoft Word Application 
private Word.ApplicationClass oWordApplic; 
// it's a reference to the document in use 
private Word.Document oWordDoc; 
// Activate the interface with the COM object of Microsoft Word 
public CCWordApp(); 
// Open an existing file or open a new file based on a template 
public void Open( string strFileName); 
// Open a new document 
public void Open( ); 
// Deactivate the interface with the COM object of Microsoft Word 
public void Quit( ); 
// Save the document 
public void Save( ); 
//Save the document with a new name as HTML document 
public void SaveAs(string strFileName ); 
// Save the document in HTML format 
public void SaveAsHtml(string strFileName ); 
// Insert Text 
public void InsertText( string strText); 
// Insert Line Break 
public void InsertLineBreak( ); 
// Insert multiple Line Break 
public void InsertLineBreak( int nline); 
// Set the paragraph alignment 
// Possible values of strType :"Centre", "Right", "Left", "Justify" 
public void SetAlignment(string strType ); 
// Set the font style 
// Possible values of strType :"Bold","Italic,"Underlined" 
public void SetFont( string strType ); 
// Disable all the style 
public void SetFont( ); 
// Set the font name 
public void SetFontName( string strType ); 
// Set the font dimension 
public void SetFontSize( int nSize ); 
// Insert a page break 
public void InsertPagebreak(); 
// Go to a predefined bookmark 
public void GotoBookMark( string strBookMarkName); 
// Go to the end of document 
public void GoToTheEnd( ); 
// Go to the beginning of document 
public void GoToTheBeginning( ); 
这样,打开一个已有的文件的操作就是:
CCWordApp test ;
test = new CCWordApp();
test.Open ("c:\\database\\test.doc");
test.InsertText("This is the text");
test.InsertLineBreak;
test.Save ();
test.Quit();
细节
示例项目包括:
CCWordApp.cs - the class
CreateDocModel.aspx: 创建一个基于模板的文档和使用书签的例子。
CreateNewDoc.aspx: 创建一个文档和插入一些文本的例子。
ModifyDocument.aspx: 打开一个已有文档然后在后面添加一些文本的例子。
template\template1.dot: 一个模板的例子(在CreateDocModel.aspx中使用)。
记住一点,你保存文件的目录必须是可写的。请查看Web.config文件来修改路径。
引用
Microsoft Word Objects
Converting Microsoft Office VBA Macros to Visual Basic .NET and C#
HOWTO: Automate Microsoft Word to Perform a Mail Merge from Visual Basic .NET
A Primer to the Office XP Primary Interop Assemblies
HOWTO: Find and Use Office Object Model Documentation
Creating and Opening Microsoft Word Documents from .NET using C#
您可能感兴趣的文章:
