|
在你使用的摸版里,你可以定義一些記號,自動化處理將向這些位置填充文本,如下:
object oBookMark = "MyBookmark";
oWordDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
使用摸版的另一個優(yōu)點是你可以創(chuàng)建和保存那些在運行過程中你想要的格式化樣式,如下:
object oStyleName = "MyStyle";
oWordDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);
[使用CCWordApp類]
在工程中包含了CCWordApp.cs這個文件,我不想總是在寫象插入文本,打開文檔這樣的代碼。
所以,我決定把一些最重要的功能封裝到CCWordApp類里去。
下面代碼簡要描述了這個類和他的功能:
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();
[細(xì)節(jié)]
演示工程包含:
CCWordApp.cs - 上面使用的類
CreateDocModel.aspx - 建立基于使用書簽的摸版的新文檔的例子。
CreateNewDoc.aspx - 建立新文檔,并向其中添加一寫文本。
ModifyDocument.aspx - 打開一個存在的文檔,并在末尾追加一些文本。
template\template1.dot - 摸版的例子(在CreateDocModel.aspx中使用到)
注意你用來保存文檔的目錄,應(yīng)該是可重寫的。
可以在 Web.config 里修改這個路徑。
|
|
【收藏】【打印】【進(jìn)入論壇】 |
|
|
|
|
|
|
|