前提:
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:



创建新Word
[转]C#操作Word            object oMissing = System.Reflection.Missing.Value;
[转]C#操作Word            Word._Application oWord;
[转]C#操作Word            Word._Document oDoc;
[转]C#操作Word            oWord 
= new Word.Application();
[转]C#操作Word            oWord.Visible 
= true;
[转]C#操作Word            oDoc 
= oWord.Documents.Add(ref oMissing, ref oMissing,
[转]C#操作Word                
ref oMissing, ref oMissing);

打开文档:
[转]C#操作Word            object oMissing = System.Reflection.Missing.Value;
[转]C#操作Word            Word._Application oWord;
[转]C#操作Word            Word._Document oDoc;
[转]C#操作Word            oWord 
= new Word.Application();
[转]C#操作Word            oWord.Visible 
= true;
[转]C#操作Word            
object fileName = @"E:\CCCXCXX\TestDoc.doc";
[转]C#操作Word            oDoc 
= oWord.Documents.Open(ref fileName,
[转]C#操作Word            
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
[转]C#操作Word            
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
[转]C#操作Word            
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

导入模板
[转]C#操作Word            object oMissing = System.Reflection.Missing.Value;
[转]C#操作Word            Word._Application oWord;
[转]C#操作Word            Word._Document oDoc;
[转]C#操作Word            oWord 
= new Word.Application();
[转]C#操作Word            oWord.Visible 
= true;
[转]C#操作Word            
object fileName = @"E:\XXXCCX\Test.doc";
[转]C#操作Word            oDoc 
= oWord.Documents.Add(ref fileName, ref oMissing,
[转]C#操作Word                            
ref oMissing, ref oMissing);


.添加新表
[转]C#操作Word            object oMissing = System.Reflection.Missing.Value;
[转]C#操作Word            Word._Application oWord;
[转]C#操作Word            Word._Document oDoc;
[转]C#操作Word            oWord 
= new Word.Application();
[转]C#操作Word            oWord.Visible 
= true;
[转]C#操作Word            oDoc 
= oWord.Documents.Add(ref oMissing, ref oMissing,
[转]C#操作Word                
ref oMissing, ref oMissing);
[转]C#操作Word
[转]C#操作Word            
object start = 0;
[转]C#操作Word            
object end = 0;
[转]C#操作Word            Word.Range tableLocation 
= oDoc.Range(ref start, ref end);
[转]C#操作Word            oDoc.Tables.Add(tableLocation, 
34ref oMissing, ref oMissing);

.表插入行
[转]C#操作Word            object oMissing = System.Reflection.Missing.Value;
[转]C#操作Word            Word._Application oWord;
[转]C#操作Word            Word._Document oDoc;
[转]C#操作Word            oWord 
= new Word.Application();
[转]C#操作Word            oWord.Visible 
= true;
[转]C#操作Word            oDoc 
= oWord.Documents.Add(ref oMissing, ref oMissing,
[转]C#操作Word                
ref oMissing, ref oMissing);
[转]C#操作Word
[转]C#操作Word            
object start = 0;
[转]C#操作Word            
object end = 0;
[转]C#操作Word            Word.Range tableLocation 
= oDoc.Range(ref start, ref end);
[转]C#操作Word            oDoc.Tables.Add(tableLocation, 
34ref oMissing, ref oMissing);
[转]C#操作Word
[转]C#操作Word            Word.Table newTable 
= oDoc.Tables[1];
[转]C#操作Word            
object beforeRow = newTable.Rows[1];
[转]C#操作Word            newTable.Rows.Add(
ref beforeRow);

.单元格合并
[转]C#操作Word            object oMissing = System.Reflection.Missing.Value;
[转]C#操作Word            Word._Application oWord;
[转]C#操作Word            Word._Document oDoc;
[转]C#操作Word            oWord 
= new Word.Application();
[转]C#操作Word            oWord.Visible 
= true;
[转]C#操作Word            oDoc 
= oWord.Documents.Add(ref oMissing, ref oMissing,
[转]C#操作Word                
ref oMissing, ref oMissing);
[转]C#操作Word
[转]C#操作Word            
object start = 0;
[转]C#操作Word            
object end = 0;
[转]C#操作Word            Word.Range tableLocation 
= oDoc.Range(ref start, ref end);
[转]C#操作Word            oDoc.Tables.Add(tableLocation, 
34ref oMissing, ref oMissing);
[转]C#操作Word
[转]C#操作Word            Word.Table newTable 
= oDoc.Tables[1];
[转]C#操作Word            
object beforeRow = newTable.Rows[1];
[转]C#操作Word            newTable.Rows.Add(
ref beforeRow);
[转]C#操作Word
[转]C#操作Word            Word.Cell cell 
= newTable.Cell(11);
[转]C#操作Word            cell.Merge(newTable.Cell(
12));

.单元格分离
[转]C#操作Word            object oMissing = System.Reflection.Missing.Value;
[转]C#操作Word            Word._Application oWord;
[转]C#操作Word            Word._Document oDoc;
[转]C#操作Word            oWord 
= new Word.Application();
[转]C#操作Word            oWord.Visible 
= true;
[转]C#操作Word            oDoc 
= oWord.Documents.Add(ref oMissing, ref oMissing,
[转]C#操作Word                
ref oMissing, ref oMissing);
[转]C#操作Word
[转]C#操作Word            
object start = 0;
[转]C#操作Word            
object end = 0;
[转]C#操作Word            Word.Range tableLocation 
= oDoc.Range(ref start, ref end);
[转]C#操作Word            oDoc.Tables.Add(tableLocation, 
34ref oMissing, ref oMissing);
[转]C#操作Word
[转]C#操作Word            Word.Table newTable 
= oDoc.Tables[1];
[转]C#操作Word            
object beforeRow = newTable.Rows[1];
[转]C#操作Word            newTable.Rows.Add(
ref beforeRow);
[转]C#操作Word
[转]C#操作Word            Word.Cell cell 
= newTable.Cell(11);
[转]C#操作Word            cell.Merge(newTable.Cell(
12));
[转]C#操作Word
[转]C#操作Word            
object Rownum = 2;
[转]C#操作Word            
object Columnnum = 2;
[转]C#操作Word            cell.Split(
ref Rownum, ref  Columnnum);
[转]C#操作Word
="color:>

通过段落控制插入
[转]C#操作Word            object oMissing = System.Reflection.Missing.Value;
            oPara1.Range.InsertParagraphAfter();

引自http://tmsoft.lsxy.com/index.php?load=read&id=334

相关文章: