zoukankan      html  css  js  c++  java
  • C#操作Word

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

     

    创建新Word

    1. object oMissing = System.Reflection.Missing.Value;  
    2.             Word._Application oWord;  
    3.             Word._Document oDoc;  
    4.             oWord = new Word.Application();  
    5.             oWord.Visible = true;  
    6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,  
    7.                 ref oMissing, ref oMissing);  


    打开文档:

    1. object oMissing = System.Reflection.Missing.Value;  
    2.            Word._Application oWord;  
    3.            Word._Document oDoc;  
    4.            oWord = new Word.Application();  
    5.            oWord.Visible = true;  
    6.            object fileName = @"E:CCCXCXXTestDoc.doc";  
    7.            oDoc = oWord.Documents.Open(ref fileName,  
    8.            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
    9.            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
    10.            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  


    导入模板

    1. object oMissing = System.Reflection.Missing.Value;  
    2.             Word._Application oWord;  
    3.             Word._Document oDoc;  
    4.             oWord = new Word.Application();  
    5.             oWord.Visible = true;  
    6.             object fileName = @"E:XXXCCXTest.doc";  
    7.             oDoc = oWord.Documents.Add(ref fileName, ref oMissing,  
    8.                             ref oMissing, ref oMissing);  


    .添加新表

    1. object oMissing = System.Reflection.Missing.Value;  
    2.             Word._Application oWord;  
    3.             Word._Document oDoc;  
    4.             oWord = new Word.Application();  
    5.             oWord.Visible = true;  
    6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,  
    7.                 ref oMissing, ref oMissing);  
    8.   
    9.             object start = 0;  
    10.             object end = 0;  
    11.             Word.Range tableLocation = oDoc.Range(ref start, ref end);  
    12.             oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);  


    表插入行

    1. object oMissing = System.Reflection.Missing.Value;  
    2.             Word._Application oWord;  
    3.             Word._Document oDoc;  
    4.             oWord = new Word.Application();  
    5.             oWord.Visible = true;  
    6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,  
    7.                 ref oMissing, ref oMissing);  
    8.   
    9.             object start = 0;  
    10.             object end = 0;  
    11.             Word.Range tableLocation = oDoc.Range(ref start, ref end);  
    12.             oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);  
    13.   
    14.             Word.Table newTable = oDoc.Tables[1];  
    15.             object beforeRow = newTable.Rows[1];  
    16.             newTable.Rows.Add(ref beforeRow);  


    单元格合并

    1. object oMissing = System.Reflection.Missing.Value;  
    2.             Word._Application oWord;  
    3.             Word._Document oDoc;  
    4.             oWord = new Word.Application();  
    5.             oWord.Visible = true;  
    6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,  
    7.                 ref oMissing, ref oMissing);  
    8.   
    9.             object start = 0;  
    10.             object end = 0;  
    11.             Word.Range tableLocation = oDoc.Range(ref start, ref end);  
    12.             oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);  
    13.   
    14.             Word.Table newTable = oDoc.Tables[1];  
    15.             object beforeRow = newTable.Rows[1];  
    16.             newTable.Rows.Add(ref beforeRow);  
    17.   
    18.             Word.Cell cell = newTable.Cell(1, 1);  
    19.             cell.Merge(newTable.Cell(1, 2));  


    .单元格分离

     

    1. object oMissing = System.Reflection.Missing.Value;  
    2.             Word._Application oWord;  
    3.             Word._Document oDoc;  
    4.             oWord = new Word.Application();  
    5.             oWord.Visible = true;  
    6.             oDoc = oWord.Documents.Add( oMissing,  
    7.                 ref oMissing, ref oMissing);  
    8.   
    9.             object start = 0;  
    10.             object end = 0;  
    11.             Word.Range tableLocation = oDoc.Range(ref start, ref end);  
    12.             oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);  
    13.   
    14.             Word.Table newTable = oDoc.Tables[1];  
    15.             object beforeRow = newTable.Rows[1];  
    16.             newTable.Rows.Add(ref beforeRow);  
    17.   
    18.             Word.Cell cell = newTable.Cell(1, 1);  
    19.             cell.Merge(newTable.Cell(1, 2));  
    20.   
    21.             object Rownum = 2;  
    22.             object Columnnum = 2;  
    23.             cell.Split(ref Rownum, ref Columnnum);  


    通过段落控制插入

    1. object oMissing = System.Reflection.Missing.Value;  
    2.             object oEndOfDoc = "\endofdoc"
    3. /**//* endofdoc is a predefined bookmark */  
    4.   
    5.             //Start Word and create a new document.  
    6.             Word._Application oWord;  
    7.             Word._Document oDoc;  
    8.             oWord = new Word.Application();  
    9.             oWord.Visible = true;  
    10.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,  
    11.                 ref oMissing, ref oMissing);  
    12.   
    13.             //Insert a paragraph at the beginning of the document.  
    14.             Word.Paragraph oPara1;  
    15.             oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);  
    16.             oPara1.Range.Text = "Heading 1";  
    17.             oPara1.Range.Font.Bold = 1;  
    18.             oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.  
    19.             oPara1.Range.InsertParagraphAfter();  

    添加表格行
    doc.Content.Tables[0].Rows.Add(ref beforeRow);

    2.11、添加表格列
    doc.Content.Tables[0].Columns.Add(ref beforeColumn);

    2.12、文本居中

    1. WordApp.Selection.ParagraphFormat.Alignment =  
    2.   
    3. Word.WdParagraphAlignment.wdAlignParagraphCenter;  
    4. WordApp.Selection.Cells.VerticalAlignment =   
    5.       Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//选中单元格文字垂直居中  


    保存Word

    1. doc.SaveAs(ref filename,ref missing ,ref missing ,ref missing ,ref missing,
    2. ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,
    3. ref missing ,ref missing ,ref missing ,ref missing ,ref missing );  


    结束Word进程

    1. myDoc.Close (ref missing,ref missing ,ref missing);  
    2.   
    3.     myWord.Quit (ref missing,ref missing ,ref missing );

    作者:wpf之家
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    forceStopPackage与killBackgroundProcesses方法
    github上十二款最著名的Android播放器开源项目
    AndroidStudio编译错误:Error: null value in entry: blameLogFolder=null
    Vue相关开源项目库汇总 http://www.opendigg.com/tags/front-vue
    Android Drawable 那些不为人知的高效用法
    Android数据存储
    touch事件的分发机制
    Hybrid 开发
    关于Http协议
    设计原理+设计模式
  • 原文地址:https://www.cnblogs.com/wpf123/p/2115633.html
Copyright © 2011-2022 走看看